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
|
/* testcase.vala
*
* Copyright (C) 2009 Julien Peeters
*
* 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
*
* Author:
* Julien Peeters <contact@julienpeeters.fr>
*/
public abstract class Gee.TestCase : Object {
private GLib.TestSuite suite;
private TestAdaptor[] adaptors = new TestAdaptor[0];
protected TestCase (string name) {
this.suite = new GLib.TestSuite (name);
}
public void add_test (string name, owned TestMethod test) {
var adaptor = new TestDefaultAdaptor (name, (owned)test, this);
this.adaptors += adaptor;
this.suite.add (new GLib.TestCase (adaptor.name,
adaptor.set_up,
adaptor.run,
adaptor.tear_down ));
}
public void add_async_test (string name, owned AsyncTestMethod test, int timeout = 10000) {
var adaptor = new TestAsyncAdaptor (name, (owned)test, this, timeout);
this.adaptors += adaptor;
this.suite.add (new GLib.TestCase (adaptor.name,
adaptor.set_up,
adaptor.run,
adaptor.tear_down ));
}
public virtual void set_up () {
}
public virtual void tear_down () {
}
public GLib.TestSuite get_suite () {
return this.suite;
}
}
namespace Gee {
public delegate void TestMethod ();
public delegate void TestFinishedCallback ();
public delegate void AsyncTestMethod (TestFinishedCallback cb);
}
private interface Gee.TestAdaptor : Object {
public abstract void set_up (void* fixture);
public abstract void run (void* fixture);
public abstract void tear_down (void* fixture);
}
private class Gee.TestDefaultAdaptor : Object, TestAdaptor {
[CCode (notify = false)]
public string name { get; private set; }
private TestMethod test;
private TestCase test_case;
public TestDefaultAdaptor (string name,
owned TestMethod test,
TestCase test_case) {
this.name = name;
this.test = (owned)test;
this.test_case = test_case;
}
public void set_up (void* fixture) {
this.test_case.set_up ();
}
public void run (void* fixture) {
this.test ();
}
public void tear_down (void* fixture) {
this.test_case.tear_down ();
}
}
private class Gee.TestAsyncAdaptor : Object, TestAdaptor {
[CCode (notify = false)]
public string name { get; private set; }
private AsyncTestMethod test;
private TestCase test_case;
private MainLoop main_loop;
private int timeout;
public TestAsyncAdaptor (string name,
owned AsyncTestMethod test,
TestCase test_case,
int timeout) {
this.name = name;
this.test = (owned)test;
this.test_case = test_case;
this.timeout = timeout;
}
public void set_up (void* fixture) {
this.test_case.set_up ();
main_loop = new MainLoop ();
}
public void run (void* fixture) {
this.test (finish);
Timeout.add (timeout, finish_timeout);
main_loop.run ();
}
public void finish () {
Idle.add (() => { main_loop.quit (); return false; });
}
public bool finish_timeout () {
Test.fail ();
Test.message (@"Timeout of $(timeout)ms reached.");
main_loop.quit ();
return false;
}
public void tear_down (void* fixture) {
this.test_case.tear_down ();
}
}
|