2 Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <boost/filesystem.hpp>
23 #include <libxml++/libxml++.h>
26 #define BOOST_TEST_DYN_LINK
27 #define BOOST_TEST_MODULE libdcp_test
28 #include <boost/test/unit_test.hpp>
34 using boost::shared_ptr;
38 check_xml (xmlpp::Element* ref, xmlpp::Element* test, list<string> ignore)
40 BOOST_CHECK_EQUAL (ref->get_name (), test->get_name ());
41 BOOST_CHECK_EQUAL (ref->get_namespace_prefix (), test->get_namespace_prefix ());
43 if (find (ignore.begin(), ignore.end(), ref->get_name()) != ignore.end ()) {
47 xmlpp::Element::NodeList ref_children = ref->get_children ();
48 xmlpp::Element::NodeList test_children = test->get_children ();
49 BOOST_CHECK_EQUAL (ref_children.size (), test_children.size ());
51 xmlpp::Element::NodeList::iterator k = ref_children.begin ();
52 xmlpp::Element::NodeList::iterator l = test_children.begin ();
53 while (k != ref_children.end ()) {
55 /* XXX: should be doing xmlpp::EntityReference, xmlpp::XIncludeEnd, xmlpp::XIncludeStart */
57 xmlpp::Element* ref_el = dynamic_cast<xmlpp::Element*> (*k);
58 xmlpp::Element* test_el = dynamic_cast<xmlpp::Element*> (*l);
59 BOOST_CHECK ((ref_el && test_el) || (!ref_el && !test_el));
60 if (ref_el && test_el) {
61 check_xml (ref_el, test_el, ignore);
64 xmlpp::ContentNode* ref_cn = dynamic_cast<xmlpp::ContentNode*> (*k);
65 xmlpp::ContentNode* test_cn = dynamic_cast<xmlpp::ContentNode*> (*l);
66 BOOST_CHECK ((ref_cn && test_cn) || (!ref_cn && !test_cn));
67 if (ref_cn && test_cn) {
68 BOOST_CHECK_EQUAL (ref_cn->get_content(), test_cn->get_content ());
71 xmlpp::Attribute* ref_at = dynamic_cast<xmlpp::Attribute*> (*k);
72 xmlpp::Attribute* test_at = dynamic_cast<xmlpp::Attribute*> (*l);
73 BOOST_CHECK ((ref_at && test_at) || (!ref_at && !test_at));
74 if (ref_at && test_at) {
75 BOOST_CHECK_EQUAL (ref_at->get_name(), test_at->get_name ());
76 BOOST_CHECK_EQUAL (ref_at->get_value(), test_at->get_value ());
85 check_xml (boost::filesystem::path ref, boost::filesystem::path test, list<string> ignore)
87 xmlpp::DomParser* ref_parser = new xmlpp::DomParser (ref.string ());
88 xmlpp::Element* ref_root = ref_parser->get_document()->get_root_node ();
89 xmlpp::DomParser* test_parser = new xmlpp::DomParser (test.string ());
90 xmlpp::Element* test_root = test_parser->get_document()->get_root_node ();
92 check_xml (ref_root, test_root, ignore);
95 BOOST_AUTO_TEST_CASE (read_test)
97 cxml::Document document;
98 document.read_file ("test/ref/a.xml");
99 document.check_root_name ("A");
101 BOOST_CHECK_EQUAL (document.string_child("B"), "42");
102 BOOST_CHECK_EQUAL (document.number_child<int>("B"), 42);
103 BOOST_CHECK_EQUAL (document.number_child<float>("B"), 42);
104 BOOST_CHECK_EQUAL (document.string_child("C"), "fred");
105 BOOST_CHECK_EQUAL (document.number_child<double>("D"), 42.9);
106 BOOST_CHECK_EQUAL (document.string_child("E"), "yes");
107 BOOST_CHECK_EQUAL (document.bool_child("E"), true);
108 BOOST_CHECK_THROW (document.bool_child("F"), cxml::Error);
110 BOOST_CHECK (document.optional_string_child("B"));
111 BOOST_CHECK_EQUAL (document.optional_string_child("B").get(), "42");
112 BOOST_CHECK (document.optional_number_child<int>("B"));
113 BOOST_CHECK_EQUAL (document.optional_number_child<int>("B").get(), 42);
114 BOOST_CHECK (document.optional_number_child<float>("B"));
115 BOOST_CHECK_EQUAL (document.optional_number_child<float>("B").get(), 42);
116 BOOST_CHECK (document.optional_string_child("C"));
117 BOOST_CHECK_EQUAL (document.optional_string_child("C").get(), "fred");
118 BOOST_CHECK (document.optional_number_child<double>("D"));
119 BOOST_CHECK_EQUAL (document.optional_number_child<double>("D").get(), 42.9);
120 BOOST_CHECK (document.optional_string_child("E"));
121 BOOST_CHECK_EQUAL (document.optional_string_child("E").get(), "yes");
122 BOOST_CHECK (document.optional_bool_child("E"));
123 BOOST_CHECK_EQUAL (document.optional_bool_child("E").get(), true);
124 BOOST_CHECK_THROW (document.optional_bool_child("F"), cxml::Error);
126 BOOST_CHECK_EQUAL (document.node_children("F").size(), 2);
127 BOOST_CHECK_EQUAL (document.node_children("F").front()->content(), "1");
128 BOOST_CHECK_EQUAL (document.node_children("F").back()->content(), "2");
130 BOOST_CHECK (!document.optional_bool_child("G"));
132 list<shared_ptr<cxml::Node> > h = document.node_children ("H");
133 BOOST_CHECK_EQUAL (h.size(), 1);
134 BOOST_CHECK_EQUAL (h.front()->node_children("I").size(), 2);
135 BOOST_CHECK_EQUAL (h.front()->node_children("I").front()->content(), "testing");
136 BOOST_CHECK_EQUAL (h.front()->node_children("I").back()->content(), "more testing");
138 BOOST_CHECK_EQUAL (document.node_children("J").size(), 1);
139 BOOST_CHECK_EQUAL (document.node_children("J").front()->node_children("K").size(), 1);
140 BOOST_CHECK_EQUAL (document.node_children("J").front()->node_children("K").front()->content(), "jim");
143 BOOST_AUTO_TEST_CASE (write_test)
145 cxml::Document document;
146 document.set_name ("A");
148 document.add_number<int> ("B", 42);
149 document.add_string ("C", "fred");
150 document.add_number<double> ("D", 42.9);
151 document.add_bool ("E", true);
152 document.add_number<int> ("F", 1);
153 document.add_number<int> ("F", 2);
154 cxml::NodePtr h = document.add ("H");
155 h->add_string ("I", "testing");
156 h->add_string ("I", "more testing");
157 document.add ("J")->add_string ("K", "jim");
159 document.write_to_file_formatted ("build/test/b.xml");
161 check_xml ("test/ref/a.xml", "build/test/b.xml", list<string> ());