summaryrefslogtreecommitdiff
path: root/valadate/testadapter.vala
blob: 195d1a27a2e36ead35914f167f7a55c6b105ae56 (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
/*
 * 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>
 */

private class Valadate.TestAdapter : Object, Test {

	public string name {get;set;}
	public string label {get;set;}
	public double time {get;set;}
	
	public int timeout {get;set;}

	public TestStatus status {get;set;default=TestStatus.NOT_RUN;}
	public string status_message {get;set;}

	public int count {
		get {
			return 1;
		}
	}

	public int size {
		get {
			return count;
		}
	}

	private TestCase.TestMethod test;
	public Test? parent {get;set;}

	public new Test get(int index) {
		return this;
	}

	public TestAdapter(string name, int timeout) {
		this.name = name;
		this.timeout = timeout;
	}

	public void add_test(owned TestPlan.TestMethod testmethod) {
		this.test = () => {
			testmethod(parent as TestCase);
		};
	}

	public void add_async_test (
		TestPlan.AsyncTestMethod async_begin,
		TestPlan.AsyncTestMethodResult async_finish)
	{
		var p = parent as TestCase;
		this.test = () => {
			AsyncResult? result = null;
			var loop = new MainLoop();
			var thread = new Thread<void*>.try(name, () => {
				async_begin(p, (o, r) => { result = r; loop.quit();});
				return null;
			});
			Timeout.add(timeout, () => {
				loop.quit();
				return false;
			},
			Priority.HIGH);
			loop.run();
			if(result == null)
				throw new IOError.TIMED_OUT(
					"The test timed out after %d milliseconds",timeout);
			async_finish(p, result);
		};
	}

	public void add_test_method(owned TestCase.TestMethod testmethod) {
		this.test = (owned)testmethod;
	}

	public void run(TestResult result) {
		if(status == TestStatus.SKIPPED)
			return;
		var p = parent as TestCase;
		result.add_test(this);
		p.set_up();
		try {
			test();
		} catch (Error e) {
			result.add_failure(this, e.message);
		}
		p.tear_down();
		result.add_success(this);
	}
}