Slightly better write subtitle tests.
[libdcp.git] / test / test.cc
1 /*
2     Copyright (C) 2012-2015 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 #define BOOST_TEST_DYN_LINK
21 #define BOOST_TEST_MODULE libdcp_test
22 #include "util.h"
23 #include "test.h"
24 #include <libxml++/libxml++.h>
25 #include <boost/test/unit_test.hpp>
26
27 using std::string;
28 using std::list;
29
30 boost::filesystem::path private_test;
31
32 struct TestConfig
33 {
34         TestConfig()
35         {
36                 dcp::init ();
37                 if (boost::unit_test::framework::master_test_suite().argc >= 2) {
38                         private_test = boost::unit_test::framework::master_test_suite().argv[1];
39                 }
40         }
41 };
42
43 void
44 check_xml (xmlpp::Element* ref, xmlpp::Element* test, list<string> ignore)
45 {
46         BOOST_CHECK_EQUAL (ref->get_name (), test->get_name ());
47         BOOST_CHECK_EQUAL (ref->get_namespace_prefix (), test->get_namespace_prefix ());
48
49         if (find (ignore.begin(), ignore.end(), ref->get_name()) != ignore.end ()) {
50                 return;
51         }
52
53         xmlpp::Element::NodeList ref_children = ref->get_children ();
54         xmlpp::Element::NodeList test_children = test->get_children ();
55         BOOST_REQUIRE_EQUAL (ref_children.size (), test_children.size ());
56
57         xmlpp::Element::NodeList::iterator k = ref_children.begin ();
58         xmlpp::Element::NodeList::iterator l = test_children.begin ();
59         while (k != ref_children.end ()) {
60
61                 /* XXX: should be doing xmlpp::EntityReference, xmlpp::XIncludeEnd, xmlpp::XIncludeStart */
62
63                 xmlpp::Element* ref_el = dynamic_cast<xmlpp::Element*> (*k);
64                 xmlpp::Element* test_el = dynamic_cast<xmlpp::Element*> (*l);
65                 BOOST_CHECK ((ref_el && test_el) || (!ref_el && !test_el));
66                 if (ref_el && test_el) {
67                         check_xml (ref_el, test_el, ignore);
68                 }
69
70                 xmlpp::ContentNode* ref_cn = dynamic_cast<xmlpp::ContentNode*> (*k);
71                 xmlpp::ContentNode* test_cn = dynamic_cast<xmlpp::ContentNode*> (*l);
72                 BOOST_CHECK ((ref_cn && test_cn) || (!ref_cn && !test_cn));
73                 if (ref_cn && test_cn) {
74                         BOOST_CHECK_EQUAL (ref_cn->get_content(), test_cn->get_content ());
75                 }
76
77                 ++k;
78                 ++l;
79         }
80
81         xmlpp::Element::AttributeList ref_attributes = ref->get_attributes ();
82         xmlpp::Element::AttributeList test_attributes = test->get_attributes ();
83         BOOST_CHECK_EQUAL (ref_attributes.size(), test_attributes.size ());
84
85         xmlpp::Element::AttributeList::const_iterator m = ref_attributes.begin();
86         xmlpp::Element::AttributeList::const_iterator n = test_attributes.begin();
87         while (m != ref_attributes.end ()) {
88                 BOOST_CHECK_EQUAL ((*m)->get_name(), (*n)->get_name());
89                 BOOST_CHECK_EQUAL ((*m)->get_value(), (*n)->get_value());
90
91                 ++m;
92                 ++n;
93         }
94 }
95
96 void
97 check_xml (string ref, string test, list<string> ignore)
98 {
99         xmlpp::DomParser* ref_parser = new xmlpp::DomParser ();
100         ref_parser->parse_memory (ref);
101         xmlpp::Element* ref_root = ref_parser->get_document()->get_root_node ();
102         xmlpp::DomParser* test_parser = new xmlpp::DomParser ();
103         test_parser->parse_memory (test);
104         xmlpp::Element* test_root = test_parser->get_document()->get_root_node ();
105
106         check_xml (ref_root, test_root, ignore);
107 }
108
109 BOOST_GLOBAL_FIXTURE (TestConfig);