Sort out xmlns stuff.
[libcxml.git] / test / tests.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 #include <iostream>
21 #include <cmath>
22 #include <boost/filesystem.hpp>
23 #include <libxml++/libxml++.h>
24 #include "cxml.h"
25
26 #define BOOST_TEST_DYN_LINK
27 #define BOOST_TEST_MODULE libdcp_test
28 #include <boost/test/unit_test.hpp>
29
30 using std::string;
31 using std::cout;
32 using std::vector;
33 using std::list;
34 using boost::shared_ptr;
35
36 BOOST_AUTO_TEST_CASE (read_test)
37 {
38         cxml::NodePtr document = cxml::read_file ("test/ref/a.xml");
39
40         BOOST_CHECK_EQUAL (document->string_child("B"), "42");
41         BOOST_CHECK_EQUAL (document->number_child<int>("B"), 42);
42         BOOST_CHECK_EQUAL (document->number_child<float>("B"), 42);
43         BOOST_CHECK_EQUAL (document->string_child("C"), "fred");
44         BOOST_CHECK_EQUAL (document->number_child<double>("D"), 42.9);
45         BOOST_CHECK_EQUAL (document->string_child("E"), "yes");
46         BOOST_CHECK_EQUAL (document->bool_child("E"), true);
47         BOOST_CHECK_THROW (document->bool_child("F"), cxml::Error);
48
49         BOOST_CHECK (document->optional_string_child("B"));
50         BOOST_CHECK_EQUAL (document->optional_string_child("B").get(), "42");
51         BOOST_CHECK (document->optional_number_child<int>("B"));
52         BOOST_CHECK_EQUAL (document->optional_number_child<int>("B").get(), 42);
53         BOOST_CHECK (document->optional_number_child<float>("B"));
54         BOOST_CHECK_EQUAL (document->optional_number_child<float>("B").get(), 42);
55         BOOST_CHECK (document->optional_string_child("C"));
56         BOOST_CHECK_EQUAL (document->optional_string_child("C").get(), "fred");
57         BOOST_CHECK (document->optional_number_child<double>("D"));
58         BOOST_CHECK_EQUAL (document->optional_number_child<double>("D").get(), 42.9);
59         BOOST_CHECK_THROW (document->child("D")->string_attribute ("foo"), cxml::Error);
60         BOOST_CHECK (document->optional_string_child("E"));
61         BOOST_CHECK_EQUAL (document->optional_string_child("E").get(), "yes");
62         BOOST_CHECK_EQUAL (document->child("E")->string_attribute ("foo"), "bar");
63         BOOST_CHECK (document->optional_bool_child("E"));
64         BOOST_CHECK_EQUAL (document->optional_bool_child("E").get(), true);
65         BOOST_CHECK_THROW (document->optional_bool_child("F"), cxml::Error);
66
67         BOOST_CHECK_EQUAL (document->children("F").size(), 2);
68         BOOST_CHECK_EQUAL (document->children("F").front()->content(), "1");
69         BOOST_CHECK_EQUAL (document->children("F").front()->number_attribute<int>("fred"), 4);
70         BOOST_CHECK_EQUAL (document->children("F").back()->content(), "2");
71         BOOST_CHECK_EQUAL (document->children("F").back()->bool_attribute("jim"), true);
72
73         BOOST_CHECK (!document->optional_bool_child("G"));
74
75         list<shared_ptr<cxml::Node> > h = document->children ("H");
76         BOOST_CHECK_EQUAL (h.size(), 1);
77         BOOST_CHECK_EQUAL (h.front()->children("I").size(), 2);
78         BOOST_CHECK_EQUAL (h.front()->children("I").front()->content(), "testing");
79         BOOST_CHECK_EQUAL (h.front()->children("I").back()->content(), "more testing");
80
81         BOOST_CHECK_EQUAL (document->children("J").size(), 1);
82         BOOST_CHECK_EQUAL (document->children("J").front()->children("K").size(), 1);
83         BOOST_CHECK_EQUAL (document->children("J").front()->children("K").front()->content(), "jim");
84 }
85
86 BOOST_AUTO_TEST_CASE (write_test)
87 {
88         cxml::NodePtr document (new cxml::Node);
89         document->set_name ("A");
90
91         document->add_child("B")->set_content ("42");
92         document->add_child("C")->set_content ("fred");
93         document->add_child("D")->set_content ("42.9");
94         cxml::NodePtr E = document->add_child("E");
95         E->set_content ("1");
96         E->set_attribute ("foo", "bar");
97         cxml::NodePtr F1 = document->add_child("F");
98         F1->set_content ("1");
99         F1->set_attribute ("fred", "4");
100         cxml::NodePtr F2 = document->add_child("F");
101         F2->set_content ("2");
102         F2->set_attribute ("jim", "1");
103         cxml::NodePtr h = document->add_child ("H");
104         h->add_child("I")->set_content ("testing");
105         h->add_child("I")->set_content ("more testing");
106         document->add_child("J")->add_child("K")->set_content ("jim");
107
108         write_to_file_formatted (document, "build/test/b.xml");
109
110         int r = system ("diff -u test/ref/b.xml build/test/b.xml");
111         BOOST_CHECK_EQUAL (WEXITSTATUS (r), 0);
112 }
113
114 BOOST_AUTO_TEST_CASE (write2_test)
115 {
116         cxml::NodePtr document (new cxml::Node);
117         document->set_name ("A");
118         document->add_child ("B");
119         document->set_namespace_declaration ("http://my.namespace");
120         document->set_namespace_declaration ("http://my.other.namespace", "foo");
121         write_to_file_formatted (document, "build/test/c.xml");
122
123         int r = system ("diff -u test/ref/c.xml build/test/c.xml");
124         BOOST_CHECK_EQUAL (WEXITSTATUS (r), 0);
125 }