9d07e7c19a7e6be7772327f344b4d317d6abb01c
[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 (test)
37 {
38         cxml::Document document ("A");
39         document.read_file ("test/ref/a.xml");
40
41         BOOST_CHECK_EQUAL (document.string_child("B"), "42");
42         BOOST_CHECK_EQUAL (document.number_child<int>("B"), 42);
43         BOOST_CHECK_EQUAL (document.number_child<float>("B"), 42);
44         BOOST_CHECK_EQUAL (document.string_child("C"), "fred");
45         BOOST_CHECK_EQUAL (document.number_child<double>("D"), 42.9);
46         BOOST_CHECK_EQUAL (document.string_child("E"), "yes");
47         BOOST_CHECK_EQUAL (document.bool_child("E"), true);
48         BOOST_CHECK_THROW (document.bool_child("F"), cxml::Error);
49
50         BOOST_CHECK (document.optional_string_child("B"));
51         BOOST_CHECK_EQUAL (document.optional_string_child("B").get(), "42");
52         BOOST_CHECK (document.optional_number_child<int>("B"));
53         BOOST_CHECK_EQUAL (document.optional_number_child<int>("B").get(), 42);
54         BOOST_CHECK (document.optional_number_child<float>("B"));
55         BOOST_CHECK_EQUAL (document.optional_number_child<float>("B").get(), 42);
56         BOOST_CHECK (document.optional_string_child("C"));
57         BOOST_CHECK_EQUAL (document.optional_string_child("C").get(), "fred");
58         BOOST_CHECK (document.optional_number_child<double>("D"));
59         BOOST_CHECK_EQUAL (document.optional_number_child<double>("D").get(), 42.9);
60         BOOST_CHECK (document.optional_string_child("E"));
61         BOOST_CHECK_EQUAL (document.optional_string_child("E").get(), "yes");
62         BOOST_CHECK (document.optional_bool_child("E"));
63         BOOST_CHECK_EQUAL (document.optional_bool_child("E").get(), true);
64         BOOST_CHECK_THROW (document.optional_bool_child("F"), cxml::Error);
65
66         BOOST_CHECK_EQUAL (document.node_children("F").size(), 2);
67         BOOST_CHECK_EQUAL (document.node_children("F").front()->content(), "1");
68         BOOST_CHECK_EQUAL (document.node_children("F").back()->content(), "2");
69
70         BOOST_CHECK (!document.optional_bool_child("G"));
71
72         list<shared_ptr<cxml::Node> > h = document.node_children ("H");
73         BOOST_CHECK_EQUAL (h.size(), 1);
74         BOOST_CHECK_EQUAL (h.front()->node_children("I").size(), 2);
75         BOOST_CHECK_EQUAL (h.front()->node_children("I").front()->content(), "testing");
76         BOOST_CHECK_EQUAL (h.front()->node_children("I").back()->content(), "more testing");
77
78         BOOST_CHECK_EQUAL (document.node_children("J").size(), 1);
79         BOOST_CHECK_EQUAL (document.node_children("J").front()->node_children("K").size(), 1);
80         BOOST_CHECK_EQUAL (document.node_children("J").front()->node_children("K").front()->content(), "jim");
81 }