Use std::vector instead of std::list.
[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_DYN_LINK
27 #define BOOST_TEST_MODULE libcxml_test
28 #include <boost/test/unit_test.hpp>
29
30 using std::shared_ptr;
31 using std::string;
32 using std::vector;
33
34 BOOST_AUTO_TEST_CASE (test)
35 {
36         cxml::Document document ("A");
37         document.read_file ("test/ref/a.xml");
38
39         BOOST_CHECK_EQUAL (document.string_child("B"), "42");
40         BOOST_CHECK_EQUAL (document.number_child<int>("B"), 42);
41         BOOST_CHECK_EQUAL (document.number_child<float>("B"), 42);
42         BOOST_CHECK_EQUAL (document.string_child("C"), "fred");
43         BOOST_CHECK_EQUAL (document.number_child<double>("D"), 42.9);
44         BOOST_CHECK_EQUAL (document.string_child("E"), "yes");
45         BOOST_CHECK_EQUAL (document.bool_child("E"), true);
46         BOOST_CHECK_THROW (document.bool_child("F"), cxml::Error);
47
48         BOOST_CHECK (document.optional_string_child("B"));
49         BOOST_CHECK_EQUAL (document.optional_string_child("B").get(), "42");
50         BOOST_CHECK (document.optional_number_child<int>("B"));
51         BOOST_CHECK_EQUAL (document.optional_number_child<int>("B").get(), 42);
52         BOOST_CHECK (document.optional_number_child<float>("B"));
53         BOOST_CHECK_EQUAL (document.optional_number_child<float>("B").get(), 42);
54         BOOST_CHECK (document.optional_string_child("C"));
55         BOOST_CHECK_EQUAL (document.optional_string_child("C").get(), "fred");
56         BOOST_CHECK (document.optional_number_child<double>("D"));
57         BOOST_CHECK_EQUAL (document.optional_number_child<double>("D").get(), 42.9);
58         BOOST_CHECK (document.optional_string_child("E"));
59         BOOST_CHECK_EQUAL (document.optional_string_child("E").get(), "yes");
60         BOOST_CHECK (document.optional_bool_child("E"));
61         BOOST_CHECK_EQUAL (document.optional_bool_child("E").get(), true);
62         BOOST_CHECK_THROW (document.optional_bool_child("F"), cxml::Error);
63
64         BOOST_CHECK_EQUAL (document.node_children("F").size(), 2);
65         BOOST_CHECK_EQUAL (document.node_children("F").front()->content(), "1");
66         BOOST_CHECK_EQUAL (document.node_children("F").back()->content(), "2");
67
68         BOOST_CHECK (!document.optional_bool_child("G"));
69
70         vector<shared_ptr<cxml::Node>> h = document.node_children ("H");
71         BOOST_CHECK_EQUAL (h.size(), 1);
72         BOOST_CHECK_EQUAL (h.front()->node_children("I").size(), 2);
73         BOOST_CHECK_EQUAL (h.front()->node_children("I").front()->content(), "testing");
74         BOOST_CHECK_EQUAL (h.front()->node_children("I").back()->content(), "more testing");
75
76         BOOST_CHECK_EQUAL (document.node_children("J").size(), 1);
77         BOOST_CHECK_EQUAL (document.node_children("J").front()->node_children("K").size(), 1);
78         BOOST_CHECK_EQUAL (document.node_children("J").front()->node_children("K").front()->content(), "jim");
79 }