From fa3f59083cf39ef4e92fb4493e28ff3724cdf5ef Mon Sep 17 00:00:00 2001 From: jez Date: Sat, 13 Sep 2008 18:36:46 +0100 Subject: [PATCH] Added tests looking at the behaviour of XPath when dealing with text nodes. I want XPath the evaluate as if the DOM had been normalised, even if it hasn't. That is, consecutive text nodes should be treated as a single node. --- tests/XPath/Makefile.am | 1 + tests/XPath/text_node_test.hpp | 105 +++++++++++++++++++++++++++++++ tests/XPath/xpath_test_suite.hpp | 2 + 3 files changed, 108 insertions(+) create mode 100755 tests/XPath/text_node_test.hpp diff --git a/tests/XPath/Makefile.am b/tests/XPath/Makefile.am index 6e38b9f0..08a2d183 100755 --- a/tests/XPath/Makefile.am +++ b/tests/XPath/Makefile.am @@ -24,6 +24,7 @@ test_sources = arithmetic_test.hpp \ parse_test.hpp \ relational_test.hpp \ step_test.hpp \ + text_node_test.hpp \ value_test.hpp \ xpath_test_suite.hpp diff --git a/tests/XPath/text_node_test.hpp b/tests/XPath/text_node_test.hpp new file mode 100755 index 00000000..f38bfa70 --- /dev/null +++ b/tests/XPath/text_node_test.hpp @@ -0,0 +1,105 @@ +#ifndef TEXT_NODE_TEST_HPP +#define TEXT_NODE_TEST_HPP + +#include "../CppUnit/framework/TestCase.h" +#include "../CppUnit/framework/TestSuite.h" +#include "../CppUnit/framework/TestCaller.h" + +#include +#include + +template +class TextNodeTest : public TestCase +{ + typedef string_adaptor SA; + + Arabica::DOM::DOMImplementation factory_; + Arabica::DOM::Document document_; + + Arabica::DOM::Element root_; + + Arabica::DOM::Text text1_; + Arabica::DOM::Text text2_; + Arabica::DOM::Text text3_; + + Arabica::XPath::XPath parser_; + + typedef Arabica::XPath::XPathExpression XPathExpression_t; + typedef Arabica::XPath::XPathValue XPathValue_t; + typedef Arabica::DOM::Node Node_t; + + +public: + TextNodeTest(const std::string& name) : TestCase(name) + { + } // TextNodeTest + + void setUp() + { + factory_ = Arabica::SimpleDOM::DOMImplementation::getDOMImplementation(); + document_ = factory_.createDocument(SA::construct_from_utf8(""), SA::construct_from_utf8("root"), 0); + root_ = document_.getDocumentElement(); + + text1_ = document_.createTextNode(SA::construct_from_utf8("one")); + text2_ = document_.createTextNode(SA::construct_from_utf8("two")); + text3_ = document_.createTextNode(SA::construct_from_utf8("three")); + + root_.appendChild(text1_); + root_.appendChild(text2_); + root_.appendChild(text3_); + } // setUp + + void test1() + { + Node_t node = parser_.evaluate(SA::construct_from_utf8("/root"), document_).asNodeSet()[0]; + assertTrue(node == root_); + } // test1 + + void test2() + { + XPathValue_t nodes = parser_.evaluate(SA::construct_from_utf8("/root/text()"), document_); + assertEquals(1, nodes.asNodeSet().size()); + } // test2 + + void test3() + { + string_type value = parser_.evaluate(SA::construct_from_utf8("/root/text()"), document_).asString(); + assertEquals("onetwothree", SA::asStdString(value)); + } // test3 + + void test4() + { + string_type value = parser_.evaluate(SA::construct_from_utf8("/root/node()"), document_).asString(); + assertEquals("onetwothree", SA::asStdString(value)); + } // test4 + + void test5() + { + string_type value = parser_.evaluate(SA::construct_from_utf8("/root"), document_).asString(); + assertEquals("onetwothree", SA::asStdString(value)); + } // test5 + + void test6() + { + XPathValue_t nodes = parser_.evaluate(SA::construct_from_utf8("/root/node()"), document_); + assertEquals(1, nodes.asNodeSet().size()); + } // test6 + +}; // class TextNodeTest + +template +TestSuite* TextNodeTest_suite() +{ + TestSuite* suiteOfTests = new TestSuite; + + suiteOfTests->addTest(new TestCaller >("test1", &TextNodeTest::test1)); + suiteOfTests->addTest(new TestCaller >("test2", &TextNodeTest::test2)); + suiteOfTests->addTest(new TestCaller >("test3", &TextNodeTest::test3)); + suiteOfTests->addTest(new TestCaller >("test4", &TextNodeTest::test4)); + suiteOfTests->addTest(new TestCaller >("test5", &TextNodeTest::test5)); + suiteOfTests->addTest(new TestCaller >("test6", &TextNodeTest::test6)); + + return suiteOfTests; +} // TextNodeTest + +#endif diff --git a/tests/XPath/xpath_test_suite.hpp b/tests/XPath/xpath_test_suite.hpp index bcd447c3..2ea10e01 100644 --- a/tests/XPath/xpath_test_suite.hpp +++ b/tests/XPath/xpath_test_suite.hpp @@ -17,6 +17,7 @@ #include "expression_test.hpp" #include "match_test.hpp" #include "attr_value_test.hpp" +#include "text_node_test.hpp" template bool XPath_test_suite(int argc, const char** argv) @@ -35,6 +36,7 @@ bool XPath_test_suite(int argc, const char** argv) runner.addTest("ExpressionTest", ExpressionTest_suite()); runner.addTest("MatchTest", MatchTest_suite()); runner.addTest("AttributeValueTest", AttributeValueTest_suite()); + runner.addTest("TextNodeTest", TextNodeTest_suite()); return runner.run(argc, argv); } // XPath_test_suite