diff options
| author | Carl Hetherington <cth@carlh.net> | 2014-10-10 19:13:01 +0100 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2014-10-10 19:13:01 +0100 |
| commit | ed60523d354af3b18150da1e183e80dc52ee851a (patch) | |
| tree | 82e4467cbb3f4f410423ee3a9b9483658edddfe5 /test | |
| parent | 80a970c3f0c2265f8df0643947ed38134a46a85f (diff) | |
Various API tidying.
Diffstat (limited to 'test')
| -rw-r--r-- | test/ref/a.xml | 6 | ||||
| -rw-r--r-- | test/ref/b.xml | 6 | ||||
| -rw-r--r-- | test/tests.cc | 186 |
3 files changed, 74 insertions, 124 deletions
diff --git a/test/ref/a.xml b/test/ref/a.xml index 10900aa..8e397ce 100644 --- a/test/ref/a.xml +++ b/test/ref/a.xml @@ -3,9 +3,9 @@ <B>42</B> <C>fred</C> <D>42.9</D> - <E>yes</E> - <F>1</F> - <F>2</F> + <E foo="bar">yes</E> + <F fred="4">1</F> + <F jim="yes">2</F> <H> <I>testing</I> <I>more testing</I> diff --git a/test/ref/b.xml b/test/ref/b.xml index d191755..d0a416c 100644 --- a/test/ref/b.xml +++ b/test/ref/b.xml @@ -3,9 +3,9 @@ <B>42</B> <C>fred</C> <D>42.9</D> - <E>1</E> - <F>1</F> - <F>2</F> + <E foo="bar">1</E> + <F fred="4">1</F> + <F jim="1">2</F> <H> <I>testing</I> <I>more testing</I> diff --git a/test/tests.cc b/test/tests.cc index bd6e4c9..8b32498 100644 --- a/test/tests.cc +++ b/test/tests.cc @@ -33,130 +33,80 @@ using std::vector; using std::list; using boost::shared_ptr; - -void -check_xml (xmlpp::Element* ref, xmlpp::Element* test, list<string> ignore) -{ - BOOST_CHECK_EQUAL (ref->get_name (), test->get_name ()); - BOOST_CHECK_EQUAL (ref->get_namespace_prefix (), test->get_namespace_prefix ()); - - if (find (ignore.begin(), ignore.end(), ref->get_name()) != ignore.end ()) { - return; - } - - xmlpp::Element::NodeList ref_children = ref->get_children (); - xmlpp::Element::NodeList test_children = test->get_children (); - BOOST_CHECK_EQUAL (ref_children.size (), test_children.size ()); - - xmlpp::Element::NodeList::iterator k = ref_children.begin (); - xmlpp::Element::NodeList::iterator l = test_children.begin (); - while (k != ref_children.end ()) { - - /* XXX: should be doing xmlpp::EntityReference, xmlpp::XIncludeEnd, xmlpp::XIncludeStart */ - - xmlpp::Element* ref_el = dynamic_cast<xmlpp::Element*> (*k); - xmlpp::Element* test_el = dynamic_cast<xmlpp::Element*> (*l); - BOOST_CHECK ((ref_el && test_el) || (!ref_el && !test_el)); - if (ref_el && test_el) { - check_xml (ref_el, test_el, ignore); - } - - xmlpp::ContentNode* ref_cn = dynamic_cast<xmlpp::ContentNode*> (*k); - xmlpp::ContentNode* test_cn = dynamic_cast<xmlpp::ContentNode*> (*l); - BOOST_CHECK ((ref_cn && test_cn) || (!ref_cn && !test_cn)); - if (ref_cn && test_cn) { - BOOST_CHECK_EQUAL (ref_cn->get_content(), test_cn->get_content ()); - } - - xmlpp::Attribute* ref_at = dynamic_cast<xmlpp::Attribute*> (*k); - xmlpp::Attribute* test_at = dynamic_cast<xmlpp::Attribute*> (*l); - BOOST_CHECK ((ref_at && test_at) || (!ref_at && !test_at)); - if (ref_at && test_at) { - BOOST_CHECK_EQUAL (ref_at->get_name(), test_at->get_name ()); - BOOST_CHECK_EQUAL (ref_at->get_value(), test_at->get_value ()); - } - - ++k; - ++l; - } -} - -void -check_xml (boost::filesystem::path ref, boost::filesystem::path test, list<string> ignore) -{ - xmlpp::DomParser* ref_parser = new xmlpp::DomParser (ref.string ()); - xmlpp::Element* ref_root = ref_parser->get_document()->get_root_node (); - xmlpp::DomParser* test_parser = new xmlpp::DomParser (test.string ()); - xmlpp::Element* test_root = test_parser->get_document()->get_root_node (); - - check_xml (ref_root, test_root, ignore); -} - BOOST_AUTO_TEST_CASE (read_test) { - cxml::Document document; - document.read_file ("test/ref/a.xml"); - document.check_root_name ("A"); - - BOOST_CHECK_EQUAL (document.string_child("B"), "42"); - BOOST_CHECK_EQUAL (document.number_child<int>("B"), 42); - BOOST_CHECK_EQUAL (document.number_child<float>("B"), 42); - BOOST_CHECK_EQUAL (document.string_child("C"), "fred"); - BOOST_CHECK_EQUAL (document.number_child<double>("D"), 42.9); - BOOST_CHECK_EQUAL (document.string_child("E"), "yes"); - BOOST_CHECK_EQUAL (document.bool_child("E"), true); - BOOST_CHECK_THROW (document.bool_child("F"), cxml::Error); - - BOOST_CHECK (document.optional_string_child("B")); - BOOST_CHECK_EQUAL (document.optional_string_child("B").get(), "42"); - BOOST_CHECK (document.optional_number_child<int>("B")); - BOOST_CHECK_EQUAL (document.optional_number_child<int>("B").get(), 42); - BOOST_CHECK (document.optional_number_child<float>("B")); - BOOST_CHECK_EQUAL (document.optional_number_child<float>("B").get(), 42); - BOOST_CHECK (document.optional_string_child("C")); - BOOST_CHECK_EQUAL (document.optional_string_child("C").get(), "fred"); - BOOST_CHECK (document.optional_number_child<double>("D")); - BOOST_CHECK_EQUAL (document.optional_number_child<double>("D").get(), 42.9); - BOOST_CHECK (document.optional_string_child("E")); - BOOST_CHECK_EQUAL (document.optional_string_child("E").get(), "yes"); - BOOST_CHECK (document.optional_bool_child("E")); - BOOST_CHECK_EQUAL (document.optional_bool_child("E").get(), true); - BOOST_CHECK_THROW (document.optional_bool_child("F"), cxml::Error); - - BOOST_CHECK_EQUAL (document.node_children("F").size(), 2); - BOOST_CHECK_EQUAL (document.node_children("F").front()->content(), "1"); - BOOST_CHECK_EQUAL (document.node_children("F").back()->content(), "2"); - - BOOST_CHECK (!document.optional_bool_child("G")); - - list<shared_ptr<cxml::Node> > h = document.node_children ("H"); + cxml::NodePtr document = cxml::read_file ("test/ref/a.xml"); + + BOOST_CHECK_EQUAL (document->string_child("B"), "42"); + BOOST_CHECK_EQUAL (document->number_child<int>("B"), 42); + BOOST_CHECK_EQUAL (document->number_child<float>("B"), 42); + BOOST_CHECK_EQUAL (document->string_child("C"), "fred"); + BOOST_CHECK_EQUAL (document->number_child<double>("D"), 42.9); + BOOST_CHECK_EQUAL (document->string_child("E"), "yes"); + BOOST_CHECK_EQUAL (document->bool_child("E"), true); + BOOST_CHECK_THROW (document->bool_child("F"), cxml::Error); + + BOOST_CHECK (document->optional_string_child("B")); + BOOST_CHECK_EQUAL (document->optional_string_child("B").get(), "42"); + BOOST_CHECK (document->optional_number_child<int>("B")); + BOOST_CHECK_EQUAL (document->optional_number_child<int>("B").get(), 42); + BOOST_CHECK (document->optional_number_child<float>("B")); + BOOST_CHECK_EQUAL (document->optional_number_child<float>("B").get(), 42); + BOOST_CHECK (document->optional_string_child("C")); + BOOST_CHECK_EQUAL (document->optional_string_child("C").get(), "fred"); + BOOST_CHECK (document->optional_number_child<double>("D")); + BOOST_CHECK_EQUAL (document->optional_number_child<double>("D").get(), 42.9); + BOOST_CHECK_THROW (document->child("D")->string_attribute ("foo"), cxml::Error); + BOOST_CHECK (document->optional_string_child("E")); + BOOST_CHECK_EQUAL (document->optional_string_child("E").get(), "yes"); + BOOST_CHECK_EQUAL (document->child("E")->string_attribute ("foo"), "bar"); + BOOST_CHECK (document->optional_bool_child("E")); + BOOST_CHECK_EQUAL (document->optional_bool_child("E").get(), true); + BOOST_CHECK_THROW (document->optional_bool_child("F"), cxml::Error); + + BOOST_CHECK_EQUAL (document->children("F").size(), 2); + BOOST_CHECK_EQUAL (document->children("F").front()->content(), "1"); + BOOST_CHECK_EQUAL (document->children("F").front()->number_attribute<int>("fred"), 4); + BOOST_CHECK_EQUAL (document->children("F").back()->content(), "2"); + BOOST_CHECK_EQUAL (document->children("F").back()->bool_attribute("jim"), true); + + BOOST_CHECK (!document->optional_bool_child("G")); + + list<shared_ptr<cxml::Node> > h = document->children ("H"); BOOST_CHECK_EQUAL (h.size(), 1); - BOOST_CHECK_EQUAL (h.front()->node_children("I").size(), 2); - BOOST_CHECK_EQUAL (h.front()->node_children("I").front()->content(), "testing"); - BOOST_CHECK_EQUAL (h.front()->node_children("I").back()->content(), "more testing"); + BOOST_CHECK_EQUAL (h.front()->children("I").size(), 2); + BOOST_CHECK_EQUAL (h.front()->children("I").front()->content(), "testing"); + BOOST_CHECK_EQUAL (h.front()->children("I").back()->content(), "more testing"); - BOOST_CHECK_EQUAL (document.node_children("J").size(), 1); - BOOST_CHECK_EQUAL (document.node_children("J").front()->node_children("K").size(), 1); - BOOST_CHECK_EQUAL (document.node_children("J").front()->node_children("K").front()->content(), "jim"); + BOOST_CHECK_EQUAL (document->children("J").size(), 1); + BOOST_CHECK_EQUAL (document->children("J").front()->children("K").size(), 1); + BOOST_CHECK_EQUAL (document->children("J").front()->children("K").front()->content(), "jim"); } BOOST_AUTO_TEST_CASE (write_test) { - cxml::Document document; - document.set_name ("A"); - - document.add_number_child<int> ("B", 42); - document.add_string_child ("C", "fred"); - document.add_number_child<double> ("D", 42.9); - document.add_bool_child ("E", true); - document.add_number_child<int> ("F", 1); - document.add_number_child<int> ("F", 2); - cxml::NodePtr h = document.add_child ("H"); - h->add_string_child ("I", "testing"); - h->add_string_child ("I", "more testing"); - document.add_child ("J")->add_string_child ("K", "jim"); - - document.write_to_file_formatted ("build/test/b.xml"); - - check_xml ("test/ref/b.xml", "build/test/b.xml", list<string> ()); + cxml::NodePtr document (new cxml::Node); + document->set_name ("A"); + + document->add_child("B")->set_content ("42"); + document->add_child("C")->set_content ("fred"); + document->add_child("D")->set_content ("42.9"); + cxml::NodePtr E = document->add_child("E"); + E->set_content ("1"); + E->set_attribute ("foo", "bar"); + cxml::NodePtr F1 = document->add_child("F"); + F1->set_content ("1"); + F1->set_attribute ("fred", "4"); + cxml::NodePtr F2 = document->add_child("F"); + F2->set_content ("2"); + F2->set_attribute ("jim", "1"); + cxml::NodePtr h = document->add_child ("H"); + h->add_child("I")->set_content ("testing"); + h->add_child("I")->set_content ("more testing"); + document->add_child("J")->add_child("K")->set_content ("jim"); + + write_to_file_formatted (document, "build/test/b.xml"); + + int r = system ("diff -u test/ref/b.xml build/test/b.xml"); + BOOST_CHECK_EQUAL (WEXITSTATUS (r), 0); } |
