File: xpath-test.cpp

package info (click to toggle)
libzeep 5.1.8-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 3,596 kB
  • sloc: cpp: 27,393; xml: 7,798; javascript: 180; sh: 37; makefile: 8
file content (181 lines) | stat: -rw-r--r-- 3,785 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <iostream>
#include <string>
#include <list>
#include <fstream>
#include <filesystem>

#include <zeep/xml/document.hpp>
#include <zeep/exception.hpp>
#include <zeep/xml/parser.hpp>
#include <zeep/xml/xpath.hpp>

using namespace std;
using namespace zeep;
namespace fs = std::filesystem;

#define foreach BOOST_FOREACH

int VERBOSE;

ostream& operator<<(ostream& os, const xml::node& n)
{
	n.write(os , {});
	return os;
}


bool run_test(const xml::element& test)
{
	if (VERBOSE)
	{
		cout << "----------------------------------------------------------" << endl
			<< "ID: " << test.get_attribute("ID")
			<< endl
			<< "xpath: " << test.get_attribute("xpath") << endl
	//		 << "data: " << test.content() << endl
	//		 << "expected-size: " << test.attr("expected-size") << endl
			<< endl;
	}

	fs::path data_file = fs::current_path() / test.get_attribute("data");
	if (not fs::exists(data_file))
		throw zeep::exception("file does not exist");
	
	std::ifstream file(data_file, ios::binary);

	xml::document doc;
	file >> doc;
	
	if (VERBOSE)
		cout << "test doc:" << endl << doc << endl;
	
	xml::xpath xp(test.get_attribute("xpath"));

	xml::context context;
	for (const xml::element* e: test.find("var"))
		context.set(e->get_attribute("name"), e->get_attribute("value"));
	
	xml::node_set ns = xp.evaluate<xml::node>(*doc.root(), context);

	if (VERBOSE)
	{
		int nr = 1;
		for (const xml::node* n: ns)
			cout << nr++ << ">> " << *n << endl;
	}
	
	bool result = true;
	
	if (ns.size() != boost::lexical_cast<unsigned int>(test.get_attribute("expected-size")))
	{
		cout << "incorrect number of nodes in returned node-set" << endl
			 << "expected: " << test.get_attribute("expected-size") << endl;

		result = false;
	}

	string test_attr_name = test.get_attribute("test-name");
	string attr_test = test.get_attribute("test-attr");

	if (not attr_test.empty())
	{
		if (VERBOSE)
			cout << "testing attribute " << test_attr_name << " for " << attr_test << endl;
		
		for (const xml::node* n: ns)
		{
			const xml::element* e = dynamic_cast<const xml::element*>(n);
			if (e == NULL)
				continue;
			
			if (e->get_attribute(test_attr_name) != attr_test)
			{
				cout << "expected attribute content is not found for node " << e->get_qname() << endl;
				result = false;
			}
		}
	}
	
	if (VERBOSE)
	{
		if (result)
			cout << "Test passed" << endl;
		else
		{
			cout << "Test failed" << endl;
			
			int nr = 1;
			for (const xml::node* n: ns)
				cout << nr++ << ") " << *n << endl;
		}
	}
	
	return result;
}

void run_tests(const fs::path& file)
{
	if (not fs::exists(file))
		throw zeep::exception("test file does not exist");
	
	std::ifstream input(file, ios::binary);

	xml::document doc;
	input >> doc;

	fs::path dir = fs::absolute(file).parent_path().parent_path();
	if (not dir.empty())
		fs::current_path(dir);

	string base = doc.front().get_attribute("xml:base");
	if (not base.empty())
		fs::current_path(base);
	
	int nr_of_tests = 0, failed_nr_of_tests = 0;
	
	for (const xml::element* test: doc.find("//xpath-test"))
	{
		++nr_of_tests;
		if (run_test(*test) == false)
			++failed_nr_of_tests;
	}
	
	cout << endl;
	if (failed_nr_of_tests == 0)
		cout << "*** No errors detected" << endl;
	else
	{
		cout << failed_nr_of_tests << " out of " << nr_of_tests << " failed" << endl;
		if (not VERBOSE)
			cout << "Run with --verbose to see the errors" << endl;
	}
}

int main(int argc, char* argv[])
{
	using namespace std::literals;
	fs::path xmlconfFile("XPath-Test-Suite/xpath-tests.xml");

	for (int i = 1; i < argc; ++i)
	{
		if (argv[i] == "-v"s)
		{
			++VERBOSE;
			continue;
		}

		xmlconfFile = argv[i];
	}

	try
	{
		run_tests(xmlconfFile);
	}
	catch (std::exception& e)
	{
		cout << "exception: " << e.what() << endl;
		return 1;
	}
	
	return 0;
}