forked from jmcabo/dunit
-
Notifications
You must be signed in to change notification settings - Fork 9
/
example.d
executable file
·306 lines (257 loc) · 6.22 KB
/
example.d
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/usr/bin/env dub
/+ dub.sdl:
name "example"
dependency "d-unit" version=">=0.8.0"
+/
// Copyright Juan Manuel Cabo 2012.
// Copyright Mario Kröplin 2017.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
module example;
import dunit;
import core.thread;
import core.time;
import std.range;
import std.stdio;
import std.system : os;
/**
* This example demonstrates the reporting of test failures.
*/
class Test
{
mixin UnitTest;
@Test
public void assertEqualsFailure() @safe pure
{
string expected = "bar";
string actual = "baz";
assertEquals(expected, actual);
}
@Test
public void assertAssocArrayEqualsFailure() pure
{
string[int] expected = [1: "foo", 2: "bar"];
string[int] actual = [1: "foo", 2: "baz"];
assertArrayEquals(expected, actual);
}
@Test
public void assertRangeEqualsFailure() @safe pure
{
int[] expected = [0, 1, 1, 2];
auto actual = iota(0, 3);
assertRangeEquals(expected, actual);
}
@Test
public void assertAllFailure() @safe
{
assertAll(
assertLessThan(6 * 7, 42),
assertGreaterThan(6 * 7, 42),
);
}
}
/**
* This example demonstrates the order in which the fixture functions run.
* The functions 'setUp' and 'tearDown' run before and after each test.
* The functions 'setUpAll' and 'tearDownAll' run once before and after
* all tests in the class.
*/
class TestFixture
{
mixin UnitTest;
public this()
{
debug writeln("@this()");
}
@BeforeAll
public static void setUpAll()
{
debug writeln("@BeforeAll");
}
@AfterAll
public static void tearDownAll()
{
debug writeln("@AfterAll");
}
@BeforeEach
public void setUp()
{
debug writeln("@BeforeEach");
}
@AfterEach
public void tearDown()
{
debug writeln("@AfterEach");
}
@Test
public void test1() @safe pure
{
debug writeln("@test1()");
}
@Test
public void test2() @safe pure
{
debug writeln("@test2()");
}
}
/**
* This example demonstrates how to reuse tests and a test fixture.
*/
class TestReuse : TestFixture
{
mixin UnitTest;
@BeforeEach
public override void setUp()
{
debug writeln("@BeforeEach override");
}
}
/**
* This example demonstrates various things to know about the test framework.
*/
class TestingThisAndThat
{
mixin UnitTest;
// test function can have default arguments
@Test
public void testResult(bool actual = true) @safe pure
{
assertTrue(actual);
}
// test function can even be private
// tagged test functions can be selected to be included or excluded
@Test
@Tag("fast")
@Tag("smoke")
private void success() @safe pure
{
testResult(true);
}
// disabled test function
@Test
@Disabled("not ready yet")
public void failure() @safe pure
{
testResult(false);
}
// disabled, because condition is true
@Test
@DisabledIf(() => true, "disabled by condition")
public void disabledByCondition() @safe pure
{
testResult(false);
}
// not disabled, because condition is false
@Test
@DisabledIf(() => false, "disabled by condition")
public void notDisabledByCondition() @safe pure
{
testResult(true);
}
// not disabled, because condition is true
@Test
@EnabledIf(() => true, "not enabled by condition")
public void enabledByCondition() @safe pure
{
testResult(true);
}
// disabled, because condition is false
@Test
@EnabledIf(() => false, "not enabled by condition")
public void notEnabledByCondition() @safe pure
{
testResult(false);
}
// disabled, because environment variable matches pattern
@Test
@DisabledIfEnvironmentVariable("PATH", ".*")
public void disabledByEnvironmentVariable() @safe pure
{
testResult(false);
}
// not disabled, because environment variable does not match pattern
@Test
@DisabledIfEnvironmentVariable("PATH", "42")
public void notDisabledByEnvironmentVariable() @safe pure
{
testResult(true);
}
// not disabled, because environment variable matches pattern
@Test
@EnabledIfEnvironmentVariable("PATH", ".*")
public void enabledByEnvironmentVariable() @safe pure
{
testResult(true);
}
// disabled, because environment variable does not match pattern
@Test
@EnabledIfEnvironmentVariable("PATH", "42")
public void notEnabledByEnvironmentVariable() @safe pure
{
testResult(false);
}
// disabled on the operating system on which the program runs
@Test
@DisabledOnOs(os)
public void disabledByOs() @safe pure
{
testResult(false);
}
// not disabled on the operating system on which the program runs
@Test
@EnabledOnOs(os)
public void enabledByOs() @safe pure
{
testResult(true);
}
// failed contracts are errors, not failures
@Test
public void error() @safe pure
{
assert(false);
}
// expected exception can be further verified
@Test
public void testException() @safe pure
{
import std.exception : enforce;
auto exception = expectThrows(enforce(false));
assertEquals("Enforcement failed", exception.msg);
}
}
/**
* This example demonstrates how to test asynchronous code.
*/
class TestingAsynchronousCode
{
mixin UnitTest;
private Thread thread;
private bool done;
@BeforeEach
public void setUp()
{
done = false;
thread = new Thread(&threadFunction);
}
@AfterEach
public void tearDown()
{
thread.join();
}
private void threadFunction()
{
Thread.sleep(100.msecs);
done = true;
}
@Test
@Tag("slow")
public void test()
{
assertFalse(done);
thread.start();
assertEventually({ return done; });
}
}
// either use the 'Main' mixin or call 'dunit_main(args)'
mixin Main;