#include #include #include #include #include #include class NodeTest: public CppUnit::TestFixture { public: void constructorName() { xml::Node t("test"); CPPUNIT_ASSERT_EQUAL(std::string("test"), t.name()); } void clone() { xml::Node t("test"); std::auto_ptr p(t.clone()); CPPUNIT_ASSERT_EQUAL(std::string("test"), p->name()); } void shift() { xml::Node t("test"); t<"), ss.str()); } { std::stringstream ss; t<\n\t\n"), ss.str()); } } void operatorParenses() { xml::Node t("test"); t< p(t.clone()); CPPUNIT_ASSERT_EQUAL(std::string("test"), p->name()); } void shift() { xml::String t("test"); CPPUNIT_ASSERT_THROW(t<"), ss.str()); } { std::stringstream ss; (t="ABC").out(ss); CPPUNIT_ASSERT_EQUAL(std::string("ABC"), ss.str()); } } void operatorParenses() { xml::String t("test"); CPPUNIT_ASSERT_EQUAL(false, t("zxy")); } void operatorBrackets() { xml::String t("test"); CPPUNIT_ASSERT_THROW(t["zxy"], xml::access_error); } void text() { xml::String t("test"); t.text("Hallo Welt"); CPPUNIT_ASSERT_EQUAL(std::string("Hallo Welt"), t.text()); } void textOut() { xml::String t("test"); t="yxc"; CPPUNIT_ASSERT_EQUAL(std::string("yxc"), t.text()); } void dereference() { xml::String t("test"); t="dfg"; CPPUNIT_ASSERT_EQUAL(t.text(), *t); } void assign() { xml::String t1("test"), t2(t1); t1="Hallo Welt"; t2.text("Hallo Welt"); CPPUNIT_ASSERT_EQUAL(t1.text(), t2.text()); } CPPUNIT_TEST_SUITE(StringTest); CPPUNIT_TEST(constructorName); CPPUNIT_TEST(clone); CPPUNIT_TEST(operatorBrackets); CPPUNIT_TEST(shift); CPPUNIT_TEST(out); CPPUNIT_TEST(operatorParenses); CPPUNIT_TEST(text); CPPUNIT_TEST(textOut); CPPUNIT_TEST(dereference); CPPUNIT_TEST(assign); CPPUNIT_TEST_SUITE_END(); }; CPPUNIT_TEST_SUITE_REGISTRATION(StringTest); class FunTest: public CppUnit::TestFixture { public: void playing() { std::string contents("\n\t\n\t\t\n\t</head>\n" "\t<body>\n\t\t<h1 class=\"main\">Title</h1>" "\n\t\t<p>First Paragraf.</p>\n\t\t<p/>\n" "\t\t<h2/>\n\t\t<p/>\n\t\t<p/>\n\t</body>\n</html>"); xml::Attributes attr; attr["class"]="main"; xml::Node test(xml::Node("html") // example <<(xml::Node("head") <<xml::String("title")) <<(xml::Node("body") <<(xml::String("h1") <<(xml::Attributes() <<(xml::Attr("class")="main"))) <<xml::String("p") <<xml::String("p") <<xml::String("h2") <<xml::String("p") <<xml::String("p"))); test["body"]["h1"] = "Title"; test["body"]["p"] = "First Paragraf."; CPPUNIT_ASSERT_EQUAL(std::string("main"), test["body"]["h1"].attr("class")); std::stringstream ss; test.out(ss); CPPUNIT_ASSERT_EQUAL(contents, ss.str()); xml::Factory factory(xml::Node("html") // template <<(xml::Node("head") <<xml::String("title")) <<(xml::Node("body") <<xml::String("h1") <<xml::String("h2") <<xml::String("p"))); std::auto_ptr<xml::Node> read(factory.read(ss)); // read back the example std::stringstream ss2; read->out(ss2); CPPUNIT_ASSERT_EQUAL(contents, ss2.str()); } void project() { xml::Factory applications(xml::Node("applications") <<(xml::Node("application") .attr("id", xml::mandatory) .attr("os", xml::optional) <<xml::String("title") <<xml::String("icon") <<xml::String("info") <<(xml::String("prog") .attr("os", xml::optional)) <<(xml::Node("args") .attr("os", xml::optional) <<xml::String("arg")) <<(xml::Node("env") .attr("os", xml::optional) <<(xml::Node("var") .attr("name", xml::mandatory) .attr("value", xml::optional))) <<(xml::Node("runtime") .attr("os", xml::optional) <<(xml::Node("copy") .attr("from", xml::mandatory) .attr("to", xml::mandatory) .attr("os", xml::optional))) <<(xml::Node("buildtime") <<(xml::Node("copy") .attr("from", xml::mandatory) .attr("to", xml::mandatory))))); xml::Factory edition(xml::Node("edition") <<xml::String("userfriendly-name") <<xml::String("update-url") <<xml::String("startpage") <<xml::String("startsplash") <<xml::String("endsplash") <<(xml::String("termination") .attr("terminate-on-media-removal", xml::optional) .attr("terminate-children", xml::optional) .attr("cleanup-files", xml::optional)) <<(xml::Node("startactions") .attr("os", xml::optional) <<(xml::Node("application") .attr("id", xml::mandatory) .attr("os", xml::optional) <<xml::String("splash") <<xml::String("type"))) <<(xml::Node("stopactions") .attr("os", xml::optional) <<(xml::Node("application") .attr("id", xml::mandatory) .attr("os", xml::optional) <<xml::String("splash"))) <<(xml::Node("tree") <<(xml::String("application") .attr("id", xml::mandatory) .attr("os", xml::optional)) <<(xml::Node("folder") .attr("open", xml::optional) .attr("os", xml::optional) <<xml::String("title") <<xml::String("icon") <<xml::String("info") <<(xml::String("application") .attr("id", xml::mandatory) .attr("os", xml::optional)))) <<(xml::Node("toolbar") .attr("os", xml::optional) <<(xml::String("application") .attr("id", xml::mandatory) .attr("os", xml::optional))) <<(xml::Node("tray") .attr("os", xml::optional) <<xml::String("icon") <<(xml::String("application") .attr("id", xml::mandatory) .attr("os", xml::optional)))); std::cout<<std::endl <<*applications<<std::endl <<*edition<<std::endl; } CPPUNIT_TEST_SUITE(FunTest); CPPUNIT_TEST(playing); CPPUNIT_TEST(project); CPPUNIT_TEST_SUITE_END(); }; CPPUNIT_TEST_SUITE_REGISTRATION(FunTest); int main() try { CppUnit::TextUi::TestRunner runner; runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest()); return runner.run() ? 0 : 1; } catch (std::exception& e) { std::cerr<<"***Exception: "<<e.what()<<std::endl; return 1; }