1/*
2    Copyright (C) 2015 Johan Mattsson
3
4    This library is free software; you can redistribute it and/or modify
5    it under the terms of the GNU Lesser General Public License as
6    published by the Free Software Foundation; either version 3 of the
7    License, or (at your option) any later version.
8
9    This library is distributed in the hope that it will be useful, but
10    WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12    Lesser General Public License for more details.
13*/
14namespace B {
15
16class Test : GLib.Object {
17	XmlTree parser;
18	string data;
19
20	public Test (string xml_data) {
21		this.data = xml_data;
22	}
23
24	public bool validate () {
25		parser = new XmlTree (data);
26		return parser.validate ();
27	}
28
29	public void test (string values) {
30		string content = get_content ();
31		bool pass = content == values;
32
33		if (!pass) {
34			print (@"$content != $values\n");
35			assert (pass);
36		}
37	}
38
39	public void benchmark (string task_name) {
40		double start_time, stop_time, t;
41
42		start_time = GLib.get_real_time ();
43		get_content ();
44		stop_time = GLib.get_real_time ();
45
46		t = (stop_time - start_time) / 1000000.0;
47
48		print (task_name + @" took $t seconds.\n");
49	}
50
51	public string get_content () {
52		XmlElement root;
53		StringBuilder content;
54
55		parser = new XmlTree (data);
56		content = new StringBuilder ();
57		root = parser.get_root ();
58		add_tag (content, root);
59
60		return content.str.strip ();
61	}
62
63	void add_tag (StringBuilder content, XmlElement tag) {
64		content.append (tag.get_name ());
65		content.append (" ");
66
67		foreach (Attribute a in tag.get_attributes ()) {
68			content.append (a.get_name ());
69			content.append (" ");
70			content.append (a.get_content ());
71			content.append (" ");
72		}
73
74		if (!has_children (tag) && tag.get_content () != "") {
75			content.append (tag.get_content ());
76			content.append (" ");
77		}
78
79		foreach (XmlElement t in tag) {
80			add_tag (content, t);
81		}
82	}
83
84	bool has_children (XmlElement tag) {
85		foreach (XmlElement t in tag) {
86			return true;
87		}
88
89		return false;
90	}
91}
92
93}
94