#ifndef test_TREEWALKER_HPP #define test_TREEWALKER_HPP #include "../CppUnit/framework/TestCase.h" #include "../CppUnit/framework/TestSuite.h" #include "../CppUnit/framework/TestCaller.h" #include #include #include template class TreeWalkerTest : public TestCase { typedef string_adaptor SA; public: TreeWalkerTest(std::string name) : TestCase(name) { } // TreeWalkerTest void setUp() { } // setUp Arabica::DOM::Document parse(string_type str) { std::stringstream ss; ss << SA::asStdString(str); Arabica::SAX::InputSource is(ss); Arabica::SAX::CatchErrorHandler eh; Arabica::SAX2DOM::Parser parser; parser.setErrorHandler(eh); parser.parse(is); //if(eh.errorsReported()) // throw std::runtime_error(eh.errors()); return parser.getDocument(); } // parse void test1() { Arabica::DOM::Document d = parse(SA::construct_from_utf8("")); Arabica::DOM::Element elem = d.getDocumentElement(); Arabica::DOM::Traversal::DocumentTraversal traversal = d.createDocumentTraversal(); Arabica::DOM::Traversal::TreeWalker walker = traversal.createTreeWalker(d.getDocumentElement(), Arabica::DOM::Traversal::SHOW_ALL, true); assert(walker.getWhatToShow() == Arabica::DOM::Traversal::SHOW_ALL); assert(walker.getFilter() == 0); assert(walker.getCurrentNode() == elem); assert(walker.nextNode() == 0); } // test1 void test2() { Arabica::DOM::Document d = parse(SA::construct_from_utf8("")); Arabica::DOM::Element elem = d.getDocumentElement(); Arabica::DOM::Traversal::DocumentTraversal traversal = d.createDocumentTraversal(); Arabica::DOM::Traversal::TreeWalker walker = traversal.createTreeWalker(d.getDocumentElement(), Arabica::DOM::Traversal::SHOW_ALL, true); assert(walker.getCurrentNode() == elem); assert(walker.nextNode() == elem.getFirstChild()); assert(walker.nextNode() == elem.getLastChild()); assert(walker.nextNode() == 0); } // test2 void test3() { Arabica::DOM::Document d = parse(SA::construct_from_utf8("hellomother")); Arabica::DOM::Element elem = d.getDocumentElement(); Arabica::DOM::Traversal::DocumentTraversal traversal = d.createDocumentTraversal(); Arabica::DOM::Traversal::TreeWalker walker = traversal.createTreeWalker(d.getDocumentElement(), Arabica::DOM::Traversal::SHOW_ALL, true); assert(walker.getCurrentNode() == elem); assert(walker.nextNode() == elem.getFirstChild()); assert(walker.nextNode() == elem.getFirstChild().getFirstChild()); assert(walker.nextNode() == elem.getLastChild()); assert(walker.nextNode() == elem.getLastChild().getFirstChild()); assert(walker.nextNode() == 0); } // test3 void test4() { Arabica::DOM::Document d = parse(SA::construct_from_utf8("hellomother")); Arabica::DOM::Element elem = d.getDocumentElement(); Arabica::DOM::Traversal::DocumentTraversal traversal = d.createDocumentTraversal(); Arabica::DOM::Traversal::TreeWalker walker = traversal.createTreeWalker(d.getDocumentElement(), Arabica::DOM::Traversal::SHOW_ELEMENT, true); assert(walker.getCurrentNode() == elem); assert(walker.nextNode() == elem.getFirstChild()); assert(walker.nextNode() == elem.getLastChild()); assert(walker.nextNode() == 0); } // test4 void test5() { Arabica::DOM::Document d = parse(SA::construct_from_utf8("hellomother")); Arabica::DOM::Element elem = d.getDocumentElement(); Arabica::DOM::Traversal::DocumentTraversal traversal = d.createDocumentTraversal(); Arabica::DOM::Traversal::TreeWalker walker = traversal.createTreeWalker(d.getDocumentElement(), Arabica::DOM::Traversal::SHOW_TEXT, true); assert(walker.getCurrentNode() == elem); assert(walker.nextNode() == elem.getFirstChild().getFirstChild()); assert(walker.nextNode() == elem.getLastChild().getFirstChild()); assert(walker.nextNode() == 0); } // test5 void test6() { Arabica::DOM::Document d = parse(SA::construct_from_utf8("hellomother")); Arabica::DOM::Element elem = d.getDocumentElement(); Arabica::DOM::Traversal::DocumentTraversal traversal = d.createDocumentTraversal(); Arabica::DOM::Traversal::TreeWalker walker = traversal.createTreeWalker(d.getDocumentElement(), Arabica::DOM::Traversal::SHOW_ALL, true); assert(walker.getCurrentNode() == elem); assert(walker.nextNode() == elem.getFirstChild()); assert(walker.nextNode() == elem.getFirstChild().getFirstChild()); assert(walker.nextNode() == elem.getLastChild()); assert(walker.nextNode() == elem.getLastChild().getFirstChild()); assert(walker.nextNode() == 0); } // test6 void test7() { Arabica::DOM::Document d = parse(SA::construct_from_utf8("hellomother")); Arabica::DOM::Element elem = d.getDocumentElement(); Arabica::DOM::Traversal::DocumentTraversal traversal = d.createDocumentTraversal(); Arabica::DOM::Traversal::TreeWalker walker = traversal.createTreeWalker(d.getDocumentElement(), Arabica::DOM::Traversal::SHOW_COMMENT, true); assert(walker.getCurrentNode() == elem); assert(walker.nextNode() == elem.getLastChild().getFirstChild()); assert(walker.nextNode() == 0); } // test7 void test8() { Arabica::DOM::Document d = parse(SA::construct_from_utf8("hellomother")); Arabica::DOM::Element elem = d.getDocumentElement(); Arabica::DOM::Traversal::DocumentTraversal traversal = d.createDocumentTraversal(); Arabica::DOM::Traversal::TreeWalker walker = traversal.createTreeWalker(d.getDocumentElement(), Arabica::DOM::Traversal::SHOW_PROCESSING_INSTRUCTION, true); assert(walker.getCurrentNode() == elem); assert(walker.nextNode() == elem.getLastChild().getLastChild()); assert(walker.nextNode() == 0); } // test8 class TestFilter : public Arabica::DOM::Traversal::NodeFilter { typedef Arabica::DOM::Traversal::NodeFilter base_t; public: typedef typename Arabica::DOM::Traversal::NodeFilter::Result Result; virtual Result acceptNode(const Arabica::DOM::Node& node) const { if(node.getFirstChild().getNodeType() == Arabica::DOM::Node_base::COMMENT_NODE) return base_t::FILTER_REJECT; return base_t::FILTER_ACCEPT; } // acceptNode }; // TestFilter void test9() { Arabica::DOM::Document d = parse(SA::construct_from_utf8("hellomother")); Arabica::DOM::Element elem = d.getDocumentElement(); Arabica::DOM::Traversal::DocumentTraversal traversal = d.createDocumentTraversal(); TestFilter filter; Arabica::DOM::Traversal::TreeWalker walker = traversal.createTreeWalker(d.getDocumentElement(), Arabica::DOM::Traversal::SHOW_ELEMENT, filter, true); assert(walker.getCurrentNode() == elem); assert(walker.nextNode() == elem.getFirstChild()); assert(walker.nextNode() == 0); } // test9 }; template TestSuite* TreeWalkerTest_suite() { TestSuite *suiteOfTests = new TestSuite; suiteOfTests->addTest(new TestCaller >("test1", &TreeWalkerTest::test1)); suiteOfTests->addTest(new TestCaller >("test2", &TreeWalkerTest::test2)); suiteOfTests->addTest(new TestCaller >("test3", &TreeWalkerTest::test3)); suiteOfTests->addTest(new TestCaller >("test4", &TreeWalkerTest::test4)); suiteOfTests->addTest(new TestCaller >("test5", &TreeWalkerTest::test5)); suiteOfTests->addTest(new TestCaller >("test6", &TreeWalkerTest::test6)); suiteOfTests->addTest(new TestCaller >("test7", &TreeWalkerTest::test7)); suiteOfTests->addTest(new TestCaller >("test8", &TreeWalkerTest::test8)); suiteOfTests->addTest(new TestCaller >("test9", &TreeWalkerTest::test9)); return suiteOfTests; } // TreeWalkerTest_suite #endif