OpenWalnut  1.5.0dev
WStructuredTextParser_test.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WSTRUCTUREDTEXTPARSER_TEST_H
26 #define WSTRUCTUREDTEXTPARSER_TEST_H
27 
28 #include <iostream>
29 #include <string>
30 #include <vector>
31 
32 #include <cxxtest/TestSuite.h>
33 
34 #include "../exceptions/WParseError.h"
35 #include "../exceptions/WFileNotFound.h"
36 #include "../exceptions/WNotFound.h"
37 #include "../exceptions/WTypeMismatch.h"
38 #include "../WStructuredTextParser.h"
39 
40 /**
41  * Test parsing and query functionality.
42  */
43 class WStructuredTextParserTest: public CxxTest::TestSuite
44 {
45 public:
46  /**
47  * Test whether the parser loads the file and handles invalid files
48  */
50  {
51  using WStructuredTextParser::parseFromFile;
52 
53  // try to parse the fixture file
54  TS_ASSERT_THROWS_NOTHING( parseFromFile(
55  boost::filesystem::path( W_FIXTURE_PATH + "WStructuredTextParser_test.txt" )
56  ) );
57 
58  TS_ASSERT_THROWS( parseFromFile(
59  boost::filesystem::path( W_FIXTURE_PATH + "WStructuredTextParser_test_invalid.txt" )
60  ), const WParseError& );
61 
62  TS_ASSERT_THROWS( parseFromFile(
63  boost::filesystem::path( W_FIXTURE_PATH + "WStructuredTextParser_test_doesnotexist.txt" )
64  ), const WFileNotFound& );
65 
66  // NOTE: we do not test parseFromString as both use the same backend functionality
67  }
68 
69  /**
70  * This method test the basic empty and count features of WStructuredTextParser::StructuredValueTree.
71  */
73  {
74  using WStructuredTextParser::StructuredValueTree;
75 
76  // load some data. Please see core/common/test/fixtures/WStructuredTextParser_test.txt for details
77  StructuredValueTree t( boost::filesystem::path( W_FIXTURE_PATH + "WStructuredTextParser_test.txt" ) );
78 
79  // check StructuredValueTree::exists
80  TS_ASSERT( t.exists( "level0/level1/somekv" ) );
81  // check also for existence of a object:
82  TS_ASSERT( t.exists( "level0/level1/level2" ) );
83  // not exists
84  TS_ASSERT( !t.exists( "level0/level1/levelNotExists" ) );
85  // needs to support check of not unique object names
86  TS_ASSERT( t.exists( "level0/notuniquelevel1" ) );
87  // check existence of key-value pair ONLY
88  TS_ASSERT( t.exists( "level0/level1/somekv", true ) );
89  TS_ASSERT( t.exists( "level0/level1", true ) ); // NOTE: level1 is an object AND kv pair
90  TS_ASSERT( !t.exists( "level0/notuniquelevel1", true ) );
91  TS_ASSERT( t.exists( "level0/notuniquelevel1/unique", true ) );
92 
93  // exists the other level0 object?
94  TS_ASSERT( t.exists( "anotherlevel0" ) );
95  TS_ASSERT( t.exists( "anotherlevel0/avalue" ) );
96 
97  // exists the file level kv pair?
98  TS_ASSERT( t.exists( "fileLevelValue", true ) );
99  TS_ASSERT( t.exists( "filelevelvalue", true ) );
100 
101  // object names with spaces
102  TS_ASSERT( t.exists( "Name With Spaces" ) );
103  TS_ASSERT( t.exists( "Name With Spaces/akey", true ) );
104 
105  // check StructuredValueTree::count
106  TS_ASSERT( t.count( "level0/level1/somekv" ) == 1 );
107  // check also for existence of a object:
108  TS_ASSERT( t.count( "level0/level1/level2" ) == 1 );
109  TS_ASSERT( t.count( "level0/level1/level2", true ) == 0 );
110 
111  // not exists
112  TS_ASSERT( t.count( "level0/level1/levelNotExists" ) == 0 );
113  // needs to support check of not unique object names
114  TS_ASSERT( t.count( "level0/notuniquelevel1" ) == 2 );
115  // check existence of key-value pair ONLY
116  TS_ASSERT( t.count( "level0/level1", true ) == 1 );
117  TS_ASSERT( t.count( "level0/level1" ) == 2 ); // NOTE: level1 is an object AND kv pair
118  TS_ASSERT( t.count( "level0/notuniquelevel1" ) == 2 );
119  TS_ASSERT( t.count( "level0/notuniquelevel1", true ) == 0 );
120 
121  // to ensure case sensitivity:
122  TS_ASSERT( t.count( "filelevelvalue", true ) == 1 );
123  TS_ASSERT( t.count( "fileLevelValue", true ) == 1 );
124 
125  // object names with spaces
126  TS_ASSERT( t.count( "Name With Spaces" ) == 1 );
127  TS_ASSERT( t.count( "Name With Spaces/akey", true ) == 1 );
128  }
129 
130  /**
131  * This method tests the basic query features of WStructuredTextParser::StructuredValueTree.
132  */
133  void testQuery()
134  {
135  using WStructuredTextParser::StructuredValueTree;
136 
137  // load some data. Please see core/common/test/fixtures/WStructuredTextParser_test.txt for details
138  StructuredValueTree t( boost::filesystem::path( W_FIXTURE_PATH + "WStructuredTextParser_test.txt" ) );
139 
140  // query only values here.
141 
142  // this MUST return the first found value
143  TS_ASSERT_EQUALS( t.getValue< std::string >( "level0/notuniquekv", "default" ), "hello hallo" );
144 
145  TS_ASSERT( t.getValue< std::string >( "level0/notuniquelevel1/somekv", "default" ) == "abc" );
146  TS_ASSERT_EQUALS( t.getValue< std::string >( "level0/uniquekv", "default" ), "hello" );
147 
148  // even if the object name is not unique, it needs to find the unique key value pair
149  TS_ASSERT( t.getValue< std::string >( "level0/notuniquelevel1/unique", "default" ) == "yes" );
150 
151  // return default if no valid key was found
152  TS_ASSERT( t.getValue< std::string >( "level0/notexists", "default" ) == "default" );
153 
154  // check if we can find not unique names which represent a class and a kv pair
155  TS_ASSERT( t.getValue< std::string >( "level0/level1", "default" ) == "something" );
156 
157  // check if we can find the other level0 object
158  TS_ASSERT( t.getValue< std::string >( "anotherlevel0/avalue", "default" ) == "hey" );
159 
160  // now check getValues which should return a list of matching values
161  std::vector< std::string > defs;
162 
163  TS_ASSERT( t.getValues< std::string >( "level0/notuniquelevel1/somekv", defs ).size() == 2 );
164  TS_ASSERT( ( *t.getValues< std::string >( "level0/notuniquelevel1/somekv", defs ).begin() ) == "abc" );
165  TS_ASSERT( ( *( t.getValues< std::string >( "level0/notuniquelevel1/somekv", defs ).begin() + 1 ) ) == "def" );
166 
167  // check the return of a default
168  TS_ASSERT( t.getValues< std::string >( "level0/notexists", defs ).size() == 0 );
169  // and the empty default
170  TS_ASSERT( t.getValues< std::string >( "level0/notexists" ).size() == 0 );
171 
172  // check operator[] (it uses getValue internally. So we only check for the WNotFound exception)
173  TS_ASSERT_THROWS( t.operator[]< std::string >( "level0/notexists" ), const WNotFound& );
174 
175  // check type conversion
176  // this is valid for getValue, getValues and [] as they utilize the same function
177  TS_ASSERT_THROWS( t.operator[]< size_t >( "level0/notuniquekv" ), const WTypeMismatch& );
178  TS_ASSERT( t.operator[]< size_t >( "level0/level1/somekv" ) == 123 );
179 
180  // to ensure case sensitivity:
181  TS_ASSERT( t.getValues< std::string >( "filelevelvalue", defs ).size() == 1 );
182  TS_ASSERT( t.getValues< std::string >( "fileLevelValue", defs ).size() == 1 );
183 
184  // access to names with spaces
185  TS_ASSERT( t.getValue< std::string >( "Name With Spaces/akey", "nooo" ) == "value" );
186  }
187 
188  /**
189  * Test the getSubTree functionality
190  */
192  {
193  using WStructuredTextParser::StructuredValueTree;
194 
195  // load some data. Please see core/common/test/fixtures/WStructuredTextParser_test.txt for details
196  StructuredValueTree t( boost::filesystem::path( W_FIXTURE_PATH + "WStructuredTextParser_test.txt" ) );
197 
198  // get this object ("level0")
199  StructuredValueTree level0;
200  TS_ASSERT_THROWS_NOTHING( level0 = *( t.getSubTrees( "level0" ).begin() ) );
201  TS_ASSERT( level0.count( "uniquekv", true ) == 1 );
202 
203  std::vector< StructuredValueTree > v = level0.getSubTrees( "notuniquelevel1" );
204  TS_ASSERT( v.size() == 2 );
205  }
206 };
207 
208 #endif // WSTRUCTUREDTEXTPARSER_TEST_H
209 
Thrown whenever a file was not found.
Definition: WFileNotFound.h:37
Indicates invalid value which could not be found.
Definition: WNotFound.h:36
Indicates invalid input in a parser.
Definition: WParseError.h:36
Test parsing and query functionality.
void testSubTreeQuery()
Test the getSubTree functionality.
void testQuery()
This method tests the basic query features of WStructuredTextParser::StructuredValueTree.
void testEmptyAndCount()
This method test the basic empty and count features of WStructuredTextParser::StructuredValueTree.
void testParseFromFile()
Test whether the parser loads the file and handles invalid files.
Indicates invalid type of something.
Definition: WTypeMismatch.h:37