OpenWalnut  1.4.0
WStructuredTextParser_test.h
00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #ifndef WSTRUCTUREDTEXTPARSER_TEST_H
00026 #define WSTRUCTUREDTEXTPARSER_TEST_H
00027 
00028 #include <iostream>
00029 #include <string>
00030 #include <vector>
00031 
00032 #include <cxxtest/TestSuite.h>
00033 
00034 #include "../exceptions/WParseError.h"
00035 #include "../exceptions/WFileNotFound.h"
00036 #include "../exceptions/WNotFound.h"
00037 #include "../exceptions/WTypeMismatch.h"
00038 #include "../WStructuredTextParser.h"
00039 
00040 /**
00041  * Test parsing and query functionality.
00042  */
00043 class WStructuredTextParserTest: public CxxTest::TestSuite
00044 {
00045 public:
00046     /**
00047      * Test whether the parser loads the file and handles invalid files
00048      */
00049     void testParseFromFile()
00050     {
00051         using WStructuredTextParser::parseFromFile;
00052 
00053         // try to parse the fixture file
00054         TS_ASSERT_THROWS_NOTHING( parseFromFile(
00055             boost::filesystem::path( W_FIXTURE_PATH + "WStructuredTextParser_test.txt" )
00056         ) );
00057 
00058         TS_ASSERT_THROWS( parseFromFile(
00059             boost::filesystem::path( W_FIXTURE_PATH + "WStructuredTextParser_test_invalid.txt" )
00060         ), WParseError );
00061 
00062         TS_ASSERT_THROWS( parseFromFile(
00063             boost::filesystem::path( W_FIXTURE_PATH + "WStructuredTextParser_test_doesnotexist.txt" )
00064         ), WFileNotFound );
00065 
00066         // NOTE: we do not test parseFromString as both use the same backend functionality
00067     }
00068 
00069     /**
00070      * This method test the basic empty and count features of WStructuredTextParser::StructuredValueTree.
00071      */
00072     void testEmptyAndCount()
00073     {
00074         using WStructuredTextParser::StructuredValueTree;
00075 
00076         // load some data. Please see core/common/test/fixtures/WStructuredTextParser_test.txt for details
00077         StructuredValueTree t( boost::filesystem::path( W_FIXTURE_PATH + "WStructuredTextParser_test.txt" ) );
00078 
00079         // check StructuredValueTree::exists
00080         TS_ASSERT( t.exists( "level0/level1/somekv" ) );
00081         // check also for existence of a object:
00082         TS_ASSERT( t.exists( "level0/level1/level2" ) );
00083         // not exists
00084         TS_ASSERT( !t.exists( "level0/level1/levelNotExists" ) );
00085         // needs to support check of not unique object names
00086         TS_ASSERT( t.exists( "level0/notuniquelevel1" ) );
00087         // check existence of key-value pair ONLY
00088         TS_ASSERT( t.exists( "level0/level1/somekv", true ) );
00089         TS_ASSERT( t.exists( "level0/level1", true ) ); // NOTE: level1 is an object AND kv pair
00090         TS_ASSERT( !t.exists( "level0/notuniquelevel1", true ) );
00091         TS_ASSERT( t.exists( "level0/notuniquelevel1/unique", true ) );
00092 
00093         // exists the other level0 object?
00094         TS_ASSERT( t.exists( "anotherlevel0" ) );
00095         TS_ASSERT( t.exists( "anotherlevel0/avalue" ) );
00096 
00097         // exists the file level kv pair?
00098         TS_ASSERT( t.exists( "fileLevelValue", true ) );
00099         TS_ASSERT( t.exists( "filelevelvalue", true ) );
00100 
00101         // object names with spaces
00102         TS_ASSERT( t.exists( "Name With Spaces" ) );
00103         TS_ASSERT( t.exists( "Name With Spaces/akey", true ) );
00104 
00105         // check StructuredValueTree::count
00106         TS_ASSERT( t.count( "level0/level1/somekv" ) == 1 );
00107         // check also for existence of a object:
00108         TS_ASSERT( t.count( "level0/level1/level2" ) == 1 );
00109         TS_ASSERT( t.count( "level0/level1/level2", true ) == 0 );
00110 
00111         // not exists
00112         TS_ASSERT( t.count( "level0/level1/levelNotExists" ) == 0 );
00113         // needs to support check of not unique object names
00114         TS_ASSERT( t.count( "level0/notuniquelevel1" ) == 2 );
00115         // check existence of key-value pair ONLY
00116         TS_ASSERT( t.count( "level0/level1", true ) == 1 );
00117         TS_ASSERT( t.count( "level0/level1" ) == 2 ); // NOTE: level1 is an object AND kv pair
00118         TS_ASSERT( t.count( "level0/notuniquelevel1" ) == 2 );
00119         TS_ASSERT( t.count( "level0/notuniquelevel1", true ) == 0 );
00120 
00121         // to ensure case sensitivity:
00122         TS_ASSERT( t.count( "filelevelvalue", true ) == 1 );
00123         TS_ASSERT( t.count( "fileLevelValue", true ) == 1 );
00124 
00125         // object names with spaces
00126         TS_ASSERT( t.count( "Name With Spaces" ) == 1 );
00127         TS_ASSERT( t.count( "Name With Spaces/akey", true ) == 1 );
00128     }
00129 
00130     /**
00131      * This method tests the basic query features of WStructuredTextParser::StructuredValueTree.
00132      */
00133     void testQuery()
00134     {
00135         using WStructuredTextParser::StructuredValueTree;
00136 
00137         // load some data. Please see core/common/test/fixtures/WStructuredTextParser_test.txt for details
00138         StructuredValueTree t( boost::filesystem::path( W_FIXTURE_PATH + "WStructuredTextParser_test.txt" ) );
00139 
00140         // query only values here.
00141 
00142         // this MUST return the first found value
00143         TS_ASSERT_EQUALS( t.getValue< std::string >( "level0/notuniquekv", "default" ), "hello hallo" );
00144 
00145         TS_ASSERT( t.getValue< std::string >( "level0/notuniquelevel1/somekv", "default" ) == "abc" );
00146         TS_ASSERT_EQUALS( t.getValue< std::string >( "level0/uniquekv", "default" ), "hello" );
00147 
00148         // even if the object name is not unique, it needs to find the unique key value pair
00149         TS_ASSERT( t.getValue< std::string >( "level0/notuniquelevel1/unique", "default" ) == "yes" );
00150 
00151         // return default if no valid key was found
00152         TS_ASSERT( t.getValue< std::string >( "level0/notexists", "default" ) == "default" );
00153 
00154         // check if we can find not unique names which represent a class and a kv pair
00155         TS_ASSERT( t.getValue< std::string >( "level0/level1", "default" ) == "something" );
00156 
00157         // check if we can find the other level0 object
00158         TS_ASSERT( t.getValue< std::string >( "anotherlevel0/avalue", "default" ) == "hey" );
00159 
00160         // now check getValues which should return a list of matching values
00161         std::vector< std::string > defs;
00162 
00163         TS_ASSERT( t.getValues< std::string >( "level0/notuniquelevel1/somekv", defs ).size() == 2 );
00164         TS_ASSERT( ( *t.getValues< std::string >( "level0/notuniquelevel1/somekv", defs ).begin() ) == "abc" );
00165         TS_ASSERT( ( *( t.getValues< std::string >( "level0/notuniquelevel1/somekv", defs ).begin() + 1 ) ) == "def" );
00166 
00167         // check the return of a default
00168         TS_ASSERT( t.getValues< std::string >( "level0/notexists", defs ).size() == 0 );
00169         // and the empty default
00170         TS_ASSERT( t.getValues< std::string >( "level0/notexists" ).size() == 0 );
00171 
00172         // check operator[] (it uses getValue internally. So we only check for the WNotFound exception)
00173         TS_ASSERT_THROWS( t.operator[]< std::string >( "level0/notexists" ), WNotFound );
00174 
00175         // check type conversion
00176         // this is valid for getValue, getValues and [] as they utilize the same function
00177         TS_ASSERT_THROWS( t.operator[]< size_t >( "level0/notuniquekv" ), WTypeMismatch );
00178         TS_ASSERT( t.operator[]< size_t >( "level0/level1/somekv" ) == 123 );
00179 
00180         // to ensure case sensitivity:
00181         TS_ASSERT( t.getValues< std::string >( "filelevelvalue", defs ).size() == 1 );
00182         TS_ASSERT( t.getValues< std::string >( "fileLevelValue", defs ).size() == 1 );
00183 
00184         // access to names with spaces
00185         TS_ASSERT( t.getValue< std::string >( "Name With Spaces/akey", "nooo" ) == "value" );
00186     }
00187 
00188     /**
00189      * Test the getSubTree functionality
00190      */
00191     void testSubTreeQuery()
00192     {
00193         using WStructuredTextParser::StructuredValueTree;
00194 
00195         // load some data. Please see core/common/test/fixtures/WStructuredTextParser_test.txt for details
00196         StructuredValueTree t( boost::filesystem::path( W_FIXTURE_PATH + "WStructuredTextParser_test.txt" ) );
00197 
00198         // get this object ("level0")
00199         StructuredValueTree level0;
00200         TS_ASSERT_THROWS_NOTHING( level0 = *( t.getSubTrees( "level0" ).begin() ) );
00201         TS_ASSERT( level0.count( "uniquekv", true ) == 1 );
00202 
00203         std::vector< StructuredValueTree > v = level0.getSubTrees( "notuniquelevel1" );
00204         TS_ASSERT( v.size() == 2 );
00205     }
00206 };
00207 
00208 #endif  // WSTRUCTUREDTEXTPARSER_TEST_H
00209