Fix tests on platforms that statically-link boost.
[libcxml.git] / test / tests.cc
1 /*
2     Copyright (C) 2012-2016 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libcxml.
5
6     libcxml is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     libcxml is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with libcxml.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include <cmath>
22 #include <boost/filesystem.hpp>
23 #include <libxml++/libxml++.h>
24 #include "cxml.h"
25
26 #define BOOST_TEST_MODULE libcxml_test
27 #include <boost/test/unit_test.hpp>
28
29 using std::shared_ptr;
30 using std::string;
31 using std::vector;
32
33 BOOST_AUTO_TEST_CASE (test)
34 {
35         cxml::Document document ("A");
36         document.read_file ("test/ref/a.xml");
37
38         BOOST_CHECK_EQUAL (document.string_child("B"), "42");
39         BOOST_CHECK_EQUAL (document.number_child<int>("B"), 42);
40         BOOST_CHECK_EQUAL (document.number_child<float>("B"), 42);
41         BOOST_CHECK_EQUAL (document.string_child("C"), "fred");
42         BOOST_CHECK_EQUAL (document.number_child<double>("D"), 42.9);
43         BOOST_CHECK_EQUAL (document.string_child("E"), "yes");
44         BOOST_CHECK_EQUAL (document.bool_child("E"), true);
45         BOOST_CHECK_THROW (document.bool_child("F"), cxml::Error);
46
47         BOOST_CHECK (document.optional_string_child("B"));
48         BOOST_CHECK_EQUAL (document.optional_string_child("B").get(), "42");
49         BOOST_CHECK (document.optional_number_child<int>("B"));
50         BOOST_CHECK_EQUAL (document.optional_number_child<int>("B").get(), 42);
51         BOOST_CHECK (document.optional_number_child<float>("B"));
52         BOOST_CHECK_EQUAL (document.optional_number_child<float>("B").get(), 42);
53         BOOST_CHECK (document.optional_string_child("C"));
54         BOOST_CHECK_EQUAL (document.optional_string_child("C").get(), "fred");
55         BOOST_CHECK (document.optional_number_child<double>("D"));
56         BOOST_CHECK_EQUAL (document.optional_number_child<double>("D").get(), 42.9);
57         BOOST_CHECK (document.optional_string_child("E"));
58         BOOST_CHECK_EQUAL (document.optional_string_child("E").get(), "yes");
59         BOOST_CHECK (document.optional_bool_child("E"));
60         BOOST_CHECK_EQUAL (document.optional_bool_child("E").get(), true);
61         BOOST_CHECK_THROW (document.optional_bool_child("F"), cxml::Error);
62
63         BOOST_CHECK_EQUAL (document.node_children("F").size(), 2);
64         BOOST_CHECK_EQUAL (document.node_children("F").front()->content(), "1");
65         BOOST_CHECK_EQUAL (document.node_children("F").back()->content(), "2");
66
67         BOOST_CHECK (!document.optional_bool_child("G"));
68
69         vector<shared_ptr<cxml::Node>> h = document.node_children ("H");
70         BOOST_CHECK_EQUAL (h.size(), 1);
71         BOOST_CHECK_EQUAL (h.front()->node_children("I").size(), 2);
72         BOOST_CHECK_EQUAL (h.front()->node_children("I").front()->content(), "testing");
73         BOOST_CHECK_EQUAL (h.front()->node_children("I").back()->content(), "more testing");
74
75         BOOST_CHECK_EQUAL (document.node_children("J").size(), 1);
76         BOOST_CHECK_EQUAL (document.node_children("J").front()->node_children("K").size(), 1);
77         BOOST_CHECK_EQUAL (document.node_children("J").front()->node_children("K").front()->content(), "jim");
78 }