libmoost
|
00001 /* vim:set ts=3 sw=3 sts=3 et: */ 00028 #include <boost/test/unit_test.hpp> 00029 #include <boost/test/test_tools.hpp> 00030 #include <boost/lexical_cast.hpp> 00031 00032 #include <map> 00033 #include <string> 00034 00035 #include "../../include/moost/testing/test_directory_creator.hpp" 00036 00037 // to be changed to <moost/xml/simple_parser.hpp>!!! 00038 #include "../../include/moost/xml/simple_parser.hpp" 00039 00040 using namespace moost; 00041 using namespace moost::xml; 00042 00043 // ----------------------------------------------------------------------------- 00044 00045 BOOST_AUTO_TEST_SUITE( simple_parser_test ) 00046 00047 #define TESTPATH "SimpleParserTest_Directory" 00048 00049 struct Fixture 00050 { 00051 00052 Fixture() 00053 : m_tdc(TESTPATH) 00054 { 00055 } 00056 ~Fixture() 00057 { 00058 } 00059 00060 moost::testing::test_directory_creator m_tdc; 00061 simple_parser m_parser; 00062 }; 00063 00064 // ----------------------------------------------------------------------------- 00065 00066 namespace 00067 { 00068 00069 char const NO_CHILDREN_XML[] = { 00070 "<base>\n" 00071 " <foo>1</foo>\n" 00072 " <bar>2</bar>\n" 00073 "</base>\n" 00074 }; 00075 00076 char const ONE_LEVEL_CHILD_XML[] = { 00077 "<base>\n" 00078 " <node>\n" 00079 " <foo>1</foo>\n" 00080 " <bar>2</bar>\n" 00081 " </node>\n" 00082 " <node>\n" 00083 " <foo>3</foo>\n" 00084 " <bar>4</bar>\n" 00085 " </node>\n" 00086 "</base>\n" 00087 }; 00088 00089 char const NOT_CLOSED_TAG_XML[] = { 00090 "<base>\n" 00091 " <node>\n" 00092 " <foo>1\n" // here! 00093 " <bar>2</bar>\n" 00094 " </node>\n" 00095 "</base>\n" 00096 }; 00097 00098 char const CLOSING_SLASH_MISSING_XML[] = { 00099 "<base>\n" 00100 " <node>\n" 00101 " <foo>1<foo>\n" // here 00102 " <bar>2</bar>\n" 00103 " </node>\n" 00104 "</base>\n" 00105 }; 00106 00107 char const WRONG_CLOSING_TAG_XML[] = { 00108 "<base>\n" 00109 " <node>\n" 00110 " <foo>1</somethingelse>\n" // here 00111 " <bar>2</bar>\n" 00112 " </node>\n" 00113 "</base>\n" 00114 }; 00115 00116 char const COMMENT_XML[] = { 00117 "<base>\n" 00118 " <node>\n" 00119 " <foo>1</foo>\n" // here 00120 " <!-- <bar>2</bar> \n" 00121 " hello hello\n" 00122 " <commented_out>10</commented_out>\n" 00123 " end of commend here -->\n" 00124 " </node>\n" 00125 "</base>\n" 00126 }; 00127 00128 char const TO_LOWERCASE_XML[] = { 00129 "<BASE>\n" 00130 " <FOO>one</FOO>\n" 00131 " <BAR>two</BAR>\n" 00132 "</BASE>\n" 00133 }; 00134 } 00135 00136 // ----------------------------------------------------------------------------- 00137 // ----------------------------------------------------------------------------- 00138 00139 BOOST_FIXTURE_TEST_CASE( test_file_not_found, Fixture ) 00140 { 00141 BOOST_CHECK_THROW( m_parser.load("unexisting_file.xml"), std::exception ); 00142 } 00143 00144 // ----------------------------------------------------------------------------- 00145 00146 BOOST_FIXTURE_TEST_CASE( test_no_children, Fixture ) 00147 { 00148 std::string const& sProxyFilename = m_tdc.GetFilePath("parser_test_no_children.xml"); 00149 std::string sCfg(NO_CHILDREN_XML); 00150 { 00151 std::ofstream ofs(sProxyFilename.c_str()); 00152 ofs << sCfg; 00153 } 00154 00155 m_parser.load(sProxyFilename); 00156 const simple_parser::tree_branch_t& root = m_parser.get_root(); 00157 00158 BOOST_REQUIRE( root.size() == 1 ); // the head 00159 00160 BOOST_CHECK_EQUAL( root.front()->header, "base" ); 00161 00162 simple_parser::tree_branch_t& leaves = root.front()->leaves; 00163 00164 //std::vector<simple_parser::tree_node*>& leaves = root.front()->leaves; 00165 BOOST_REQUIRE( leaves.size() == 2 ); // must contain foo and bar 00166 00167 BOOST_CHECK_EQUAL( leaves[0]->header, "foo" ); 00168 BOOST_CHECK_EQUAL( leaves[0]->value, "1" ); 00169 BOOST_CHECK( leaves[0]->leaves.empty() ); 00170 00171 BOOST_CHECK_EQUAL( leaves[1]->header, "bar" ); 00172 BOOST_CHECK_EQUAL( leaves[1]->value, "2" ); 00173 BOOST_CHECK( leaves[1]->leaves.empty() ); 00174 } 00175 00176 // ----------------------------------------------------------------------------- 00177 00178 BOOST_FIXTURE_TEST_CASE( test_one_level_child, Fixture ) 00179 { 00180 std::string const& sProxyFilename = m_tdc.GetFilePath("parser_test_one_level.xml"); 00181 std::string sCfg(ONE_LEVEL_CHILD_XML); 00182 { 00183 std::ofstream ofs(sProxyFilename.c_str()); 00184 ofs << sCfg; 00185 } 00186 00187 m_parser.load(sProxyFilename); 00188 const simple_parser::tree_branch_t& root = m_parser.get_root(); 00189 00190 BOOST_REQUIRE( root.size() == 1 ); // the head 00191 BOOST_CHECK_EQUAL( root.front()->header, "base" ); 00192 00193 simple_parser::tree_branch_t& nodes = root.front()->leaves; 00194 BOOST_REQUIRE( nodes.size() == 2 ); // must contain two nodes 00195 00196 BOOST_CHECK_EQUAL( nodes[0]->header, "node" ); 00197 BOOST_CHECK( nodes[0]->value.empty() ); 00198 BOOST_CHECK_EQUAL( nodes[1]->header, "node" ); 00199 BOOST_CHECK( nodes[1]->value.empty() ); 00200 00201 for ( int i = 0; i < 2; ++i ) 00202 { 00203 simple_parser::tree_branch_t& node_leaves = nodes[i]->leaves; 00204 BOOST_CHECK_EQUAL( node_leaves[0]->header, "foo" ); 00205 BOOST_CHECK_EQUAL( node_leaves[0]->value, boost::lexical_cast<std::string>(2*i + 1) ); 00206 BOOST_CHECK( node_leaves[0]->leaves.empty() ); 00207 00208 BOOST_CHECK_EQUAL( node_leaves[1]->header, "bar" ); 00209 BOOST_CHECK_EQUAL( node_leaves[1]->value, boost::lexical_cast<std::string>(2*i + 2) ); 00210 BOOST_CHECK( node_leaves[1]->leaves.empty() ); 00211 } 00212 } 00213 00214 // ----------------------------------------------------------------------------- 00215 00216 BOOST_FIXTURE_TEST_CASE( test_no_closed_tags, Fixture ) 00217 { 00218 std::string const& sProxyFilename = m_tdc.GetFilePath("parser_test_no_closed_tags.xml"); 00219 std::string sCfg(NOT_CLOSED_TAG_XML); 00220 { 00221 std::ofstream ofs(sProxyFilename.c_str()); 00222 ofs << sCfg; 00223 } 00224 00225 BOOST_CHECK_THROW( m_parser.load(sProxyFilename), std::exception ); 00226 } 00227 00228 // ----------------------------------------------------------------------------- 00229 00230 BOOST_FIXTURE_TEST_CASE( test_slash_missing, Fixture ) 00231 { 00232 std::string const& sProxyFilename = m_tdc.GetFilePath("parser_test_slash_missing.xml"); 00233 std::string sCfg(CLOSING_SLASH_MISSING_XML); 00234 { 00235 std::ofstream ofs(sProxyFilename.c_str()); 00236 ofs << sCfg; 00237 } 00238 00239 BOOST_CHECK_THROW( m_parser.load(sProxyFilename), std::exception ); 00240 } 00241 00242 // ----------------------------------------------------------------------------- 00243 00244 BOOST_FIXTURE_TEST_CASE( test_wrong_closing, Fixture ) 00245 { 00246 std::string const& sProxyFilename = m_tdc.GetFilePath("parser_test_wrong_closing.xml"); 00247 std::string sCfg(WRONG_CLOSING_TAG_XML); 00248 { 00249 std::ofstream ofs(sProxyFilename.c_str()); 00250 ofs << sCfg; 00251 } 00252 00253 BOOST_CHECK_THROW( m_parser.load(sProxyFilename), std::exception ); 00254 } 00255 00256 // ----------------------------------------------------------------------------- 00257 00258 BOOST_FIXTURE_TEST_CASE( test_comment, Fixture ) 00259 { 00260 std::string const& sProxyFilename = m_tdc.GetFilePath("test_comment.xml"); 00261 std::string sCfg(COMMENT_XML); 00262 { 00263 std::ofstream ofs(sProxyFilename.c_str()); 00264 ofs << sCfg; 00265 } 00266 00267 m_parser.load(sProxyFilename); 00268 const simple_parser::tree_branch_t& root = m_parser.get_root(); 00269 00270 BOOST_REQUIRE( root.size() == 1 ); // the head 00271 BOOST_CHECK_EQUAL( root.front()->header, "base" ); 00272 00273 simple_parser::tree_branch_t& nodes = root.front()->leaves; 00274 BOOST_REQUIRE( nodes.size() == 1 ); // must contain one node 00275 00276 BOOST_CHECK_EQUAL( nodes[0]->header, "node" ); 00277 BOOST_CHECK( nodes[0]->value.empty() ); 00278 00279 simple_parser::tree_branch_t& node_leaves = nodes[0]->leaves; 00280 BOOST_CHECK_EQUAL( node_leaves[0]->header, "foo" ); 00281 BOOST_CHECK_EQUAL( node_leaves[0]->value, "1" ); 00282 BOOST_CHECK( node_leaves[0]->leaves.empty() ); 00283 } 00284 00285 // ----------------------------------------------------------------------------- 00286 00287 BOOST_FIXTURE_TEST_CASE( test_to_lowercase_1, Fixture ) 00288 { 00289 std::string const& sProxyFilename = m_tdc.GetFilePath("parser_test_to_lowercase_1.xml"); 00290 std::string sCfg(TO_LOWERCASE_XML); 00291 { 00292 std::ofstream ofs(sProxyFilename.c_str()); 00293 ofs << sCfg; 00294 } 00295 00296 m_parser.load(sProxyFilename); 00297 00298 const simple_parser::tree_branch_t& root = m_parser.get_root(); 00299 00300 BOOST_REQUIRE( root.size() == 1 ); // the head 00301 BOOST_CHECK_EQUAL( root.front()->header, "BASE" ); 00302 00303 simple_parser::tree_branch_t& leaves = root.front()->leaves; 00304 BOOST_REQUIRE( leaves.size() == 2 ); // must contain foo and bar 00305 00306 BOOST_CHECK_EQUAL( leaves[0]->header, "FOO" ); 00307 BOOST_CHECK_EQUAL( leaves[0]->value, "one" ); 00308 BOOST_CHECK_EQUAL( leaves[1]->header, "BAR" ); 00309 BOOST_CHECK_EQUAL( leaves[1]->value, "two" ); 00310 } 00311 00312 // ----------------------------------------------------------------------------- 00313 00314 BOOST_FIXTURE_TEST_CASE( test_to_lowercase_2, Fixture ) 00315 { 00316 std::string const& sProxyFilename = m_tdc.GetFilePath("parser_test_to_lowercase_2.xml"); 00317 std::string sCfg(TO_LOWERCASE_XML); 00318 { 00319 std::ofstream ofs(sProxyFilename.c_str()); 00320 ofs << sCfg; 00321 } 00322 00323 m_parser.load(sProxyFilename, true); 00324 00325 const simple_parser::tree_branch_t& root = m_parser.get_root(); 00326 00327 BOOST_REQUIRE( root.size() == 1 ); // the head 00328 BOOST_CHECK_EQUAL( root.front()->header, "base" ); 00329 00330 simple_parser::tree_branch_t& leaves = root.front()->leaves; 00331 BOOST_REQUIRE( leaves.size() == 2 ); // must contain foo and bar 00332 00333 BOOST_CHECK_EQUAL( leaves[0]->header, "foo" ); 00334 BOOST_CHECK_EQUAL( leaves[0]->value, "one" ); 00335 BOOST_CHECK_EQUAL( leaves[1]->header, "bar" ); 00336 BOOST_CHECK_EQUAL( leaves[1]->value, "two" ); 00337 } 00338 00339 // ----------------------------------------------------------------------------- 00340 00341 BOOST_FIXTURE_TEST_CASE( test_fill_from_branch, Fixture ) 00342 { 00343 std::string const& sProxyFilename = m_tdc.GetFilePath("parser_test_fill_from_branch.xml"); 00344 std::string sCfg(NO_CHILDREN_XML); 00345 { 00346 std::ofstream ofs(sProxyFilename.c_str()); 00347 ofs << sCfg; 00348 } 00349 00350 m_parser.load(sProxyFilename); 00351 const simple_parser::tree_branch_t& root = m_parser.get_root(); 00352 00353 std::map<std::string, std::string> localMap; 00354 root.front()->leaves2map(localMap); 00355 00356 BOOST_CHECK_EQUAL( localMap.size(), 2 ); 00357 BOOST_CHECK_EQUAL( localMap["foo"], "1" ); 00358 BOOST_CHECK_EQUAL( localMap["bar"], "2" ); 00359 } 00360 00361 // ----------------------------------------------------------------------------- 00362 00363 BOOST_AUTO_TEST_SUITE_END()