summaryrefslogtreecommitdiff
path: root/valadate/testreport.vala
blob: f5e58f6d9992a7404d9980d62e470c9ecd5ad583 (plain)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
 * Valadate - Unit testing library for GObject-based libraries.
 * Copyright (C) 2017  Chris Daley <chebizarro@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.

 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 *
 * Authors:
 * 	Chris Daley <chebizarro@gmail.com>
 */
 
public class Valadate.TestReport {

	private const string XML_DECL ="<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
	private const string TESTSUITE_XML =
		"""<testsuite disabled="" errors="" failures="" hostname="" id="" """ +
		"""name="" package="" skipped="" tests="" time="" timestamp="" >"""+
		"""<properties/></testsuite>""";
	private const string TESTCASE_XML =
		"""<testcase assertions="" classname="" name="" status="" time="" />""";
	private const string MESSAGE_XML = "<%s message=\"%s\" type=\"%s\">%s</%s>";
	private const string TESTCASE_START =
		"<testcase assertions=\"\" classname=\"%s\" name=\"%s\" status=\"\" time=\"\">";
	private const string VDX_NS = "xmlns:vdx=\"https://www.valadate.org/vdx\"";
	private const string TESTCASE_TAG = "testcase";
	private const string ROOT_TAG = "root";
	private const string SKIP_TAG = "skipped";
	private const string ERROR_TAG = "error";
	private const string FAILURE_TAG = "failure";
	private const string INFO_TAG = "info";
	private const string TIMER_TAG = "timer";
	private const string SYSTEM_OUT_TAG = "system-out";
	private const string SYSTEM_ERR_TAG = "system-err";

	public Test test {get;set;}
	public bool subprocess {get;set;}
	
	public XmlFile xml {get;set;}

	private static int64 start_time;
	private static int64 end_time;

	private static Regex regex_err;
	private const string regex_err_string =
		"""(\*{2}\n([A-Z]*):([\S]*) ([\S ]*)\n)""";
	
	public TestReport(Test test, bool subprocess) throws Error {
		this.test = test;
		this.subprocess = subprocess;

		if(test.status == TestStatus.NOT_RUN)
			test.status = TestStatus.RUNNING;

		if(subprocess) {
			Log.set_default_handler (log_func);
			GLib.set_printerr_handler (printerr_func);
			regex_err = new Regex(regex_err_string);
		}

		if(test is TestSuite || test is TestCase)
			new_testsuite();
		else if (test is TestAdapter)
			new_testcase();
	}
	
	private void new_testsuite() throws Error {
		if(subprocess)
			return;
		
		var decl = "%s<%s>%s</%s>".printf(XML_DECL, ROOT_TAG, TESTSUITE_XML, ROOT_TAG);
		var doc = Xml.Parser.read_memory(decl, decl.length);
		var root = doc->get_root_element()->children;
		root->set_prop("tests", test.count.to_string());
		root->set_prop("name",test.label);
		xml = new XmlFile.from_doc(doc);
	}

	private void new_testcase() throws Error {
		if(subprocess) {
			stderr.printf("%s<%s>",XML_DECL,ROOT_TAG);
			stderr.printf(TESTCASE_START,test.parent.get_type().name(), test.label);
			start_time = get_monotonic_time();
		} else {
			var decl = "%s<%s>%s</%s>".printf(XML_DECL, ROOT_TAG, TESTCASE_XML, ROOT_TAG);
			var doc = Xml.Parser.read_memory(decl, decl.length);
			var root = doc->get_root_element()->children;
			root->set_prop("classname",((TestAdapter)test).parent.name);
			root->set_prop("status",test.status.to_string().substring(21));
			root->set_prop("name",test.label);
			xml = new XmlFile.from_doc(doc);
		}
	}

	public void add_error(string message) {
		if (test.status != TestStatus.SKIPPED &&
			test.status != TestStatus.TODO)
			test.status = TestStatus.ERROR;

		add_message(ERROR_TAG, message);

		if(subprocess) {
			emit_timer();
			stderr.printf("</%s></%s>",TESTCASE_TAG, ROOT_TAG);
			stderr.putc(0);
		}
		update_status();
	}

	public void add_failure(string message) {
		if (test.status != TestStatus.SKIPPED &&
			test.status != TestStatus.TODO)
			test.status = TestStatus.FAILED;

		add_message(FAILURE_TAG, message);
		
		if(subprocess) {
			emit_timer();
			stderr.printf("</%s></%s>",TESTCASE_TAG, ROOT_TAG);
			stderr.putc(0);
		}
		update_status();
	}

	public void add_skip(string message) {
		test.status = TestStatus.SKIPPED;
		add_message(SKIP_TAG, message);
		update_status();
	}

	public void add_success() {
		if (test.status != TestStatus.SKIPPED &&
			test.status != TestStatus.TODO)
			test.status = TestStatus.PASSED;
		if(subprocess) {
			emit_timer();
			stderr.printf("</%s></%s>",TESTCASE_TAG, ROOT_TAG);
			stderr.putc(0);
		}
		update_status();
	}

	private void add_message(string tag, string message) {
		var escaped = Markup.escape_text(message);
		if(subprocess) {
			stderr.printf(MESSAGE_XML, tag, escaped, tag.up(), message, tag);
		} else {
			Xml.Node* child = new Xml.Node(null, tag);
			child->set_content(escaped);
			
			string[] tags = {ERROR_TAG, FAILURE_TAG, INFO_TAG};
			
			if(tag in tags) {
				child->new_prop("message", escaped);
				child->new_prop("type", tag.up());
			}
			
			Xml.Node* root = xml.eval("//testcase | //testsuite")[0];
			root->add_child(child);
		}
	}
	/**
	 * Adds arbitrary text to the TestReport. In the xml output this
	 * text will be encapsulated in <system-out/> or <system-err/> tag
	 * 
	 * @param text The text to be added to the {@link TestReport}.
	 * the text will be escaped before being added.
	 * @param tag The tag to use for adding the text
	 */ 
	public void add_text(string text, string tag) {
		var markup = Markup.escape_text(text);
		Xml.Node* child = new Xml.Node(null, tag);
		child->set_content(markup);
		
		string[] tags = {ERROR_TAG, FAILURE_TAG, INFO_TAG};
			
		if(tag in tags) {
			child->new_prop("message", markup);
			child->new_prop("type", tag.up());
		}
		
		Xml.Node* root = xml.eval("//testcase | //testsuite")[0];
		root->add_child(child);
	}
	
	public void update_status() {
		if(test is TestAdapter && !subprocess) {
			Xml.Node* root = xml.eval("//testcase")[0];
			root->set_prop("status",test.status.to_string().substring(21));
			root->set_prop("time",test.time.to_string());
		}
	}

	private static void emit_timer() {
		end_time = get_monotonic_time();
		var ms = "%f".printf(((double)(end_time-start_time))/1000);
		stderr.printf(MESSAGE_XML, TIMER_TAG, ms, TIMER_TAG, ms, TIMER_TAG);
	}

	private static void printerr_func (string? text) {
		if(text == null)
			return;
		MatchInfo info;
		if(regex_err.match(text, 0, out info)) {
			var escaped = Markup.escape_text(info.fetch(4));
			stderr.printf(MESSAGE_XML, ERROR_TAG, escaped, ERROR_TAG, text, ERROR_TAG);
			emit_timer();
			stderr.printf("</%s></%s>",TESTCASE_TAG, ROOT_TAG);
			stderr.putc(0);
		}
	}

	private void log_func (
		string? log_domain,
		LogLevelFlags log_levels,
		string? message)	{

		if (((log_levels & LogLevelFlags.LEVEL_INFO) != 0) ||
			((log_levels & LogLevelFlags.LEVEL_MESSAGE) != 0) ||
			((log_levels & LogLevelFlags.LEVEL_DEBUG) != 0)) {
			add_message(INFO_TAG, message);
		} else {
			add_error(message);
		}
	}

	public void process_buffer(string buffer) throws Error {
		
		xml = new XmlFile.from_string(buffer);

		var bits = xml.eval("//testcase/text()");

		if(bits.size != 0) {
			Xml.Node* textnode = bits[0];
			add_message(SYSTEM_ERR_TAG, textnode->get_content());
			textnode->unlink();
		}

		var errs = xml.eval("//failure | //error");
		if (errs.size > 0 &&
			test.status != TestStatus.SKIPPED &&
			test.status != TestStatus.TODO)
			test.status = TestStatus.FAILED;

		bits = xml.eval("//timer");
		Xml.Node* timer = bits[0];
		test.time = double.parse(timer->get_content());
		timer->unlink();

		update_status();
	}

	public void add_stdout(string text) {
		add_message(SYSTEM_OUT_TAG, text);
	}
}