-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdunit.d
530 lines (479 loc) · 15.7 KB
/
dunit.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/** Unit testing framework ('dunit')
*
* Allows to define unittests simply as methods which names start
* with 'test'.
* The only thing necessary to create a unit test class, is to
* declare the mixin TestMixin inside the class. This will register
* the class and its test methods for the test runner.
*
* License: <a href="http://www.boost.org/LICENSE_1_0.txt">Boost License 1.0</a>.
* Authors: Juan Manuel Cabo
* Version: 0.6
* Source: dunit.d
* Last update: 2012-03-21
*/
/* Copyright Juan Manuel Cabo 2012.
* 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 dunit;
import std.stdio;
import std.conv;
import core.thread;
import core.time;
string[] testClasses;
string[][string] testNamesByClass;
void function(Object o, string testName)[string] testCallers;
Object function()[string] testCreators;
/** Asserts that both values are equal. */
public static void assertEquals(T)(T s, T t,
string file = __FILE__,
size_t line = __LINE__)
if (!__traits(isScalar, T))
{
if (s is t) {
return;
}
if (s != t) {
throw new core.exception.AssertError(
"Expected: '"~to!string(s)~"', but was: '"~to!string(t)~"'",
file, line);
}
}
/** Asserts that both values are equal. This function checks values of
* different scalar types, that, though being different types, they
* can be compared */
public static void assertEquals(S, T)(S s, T t,
string file = __FILE__,
size_t line = __LINE__)
if (__traits(isScalar, T) && __traits(isScalar, S))
{
if (s != t) {
throw new core.exception.AssertError(
"Expected: '"~to!string(s)~"', but was: '"~to!string(t)~"'",
file, line);
}
}
/** Checks a delegate until the timeout expires. The assert error is produced
* if the delegate fails to return 'true' before the timeout.
*
* The parameter timeoutMsecs determines the maximum timeout to wait before
* asserting a failure (default is 500ms).
*
* The parameter recheckTimeMsecs determines how often the predicate will
* be checked (default is 10ms).
*
* This kind of assertion is very useful to check on code that runs in another
* thread. For instance, the thread that listens to a socket.
*/
public static void assertWithTimeout(bool delegate() condition,
int timeoutMsecs = 500, int recheckTimeMsecs = 10,
string msg = null,
string file = __FILE__,
size_t line = __LINE__)
{
int count = 0;
while (!condition()) {
if (recheckTimeMsecs * count > timeoutMsecs) {
if (msg is null) {
msg = "Timeout elapsed for condition.";
}
throw new core.exception.AssertError(msg, file, line);
}
Thread.sleep(dur!"msecs"(recheckTimeMsecs));
++count;
}
}
mixin template DUnitMain() {
int main (string[] args) {
runTests();
return 0;
}
}
/**
* Runs all the unit tests.
*/
public static void runTests() {
runTests_Progress();
//debug: runTests_Tree();
}
/**
* Runs all the unit tests, showing progress dots, and the results at the end.
*/
public static void runTests_Progress() {
struct TestError {
string testClass;
string testName;
Throwable error;
bool isAssertError;
this(string tc, string tn, Throwable er) {
this.testClass = tc;
this.testName = tn;
this.error = er;
this.isAssertError = (typeid(er) is typeid(core.exception.AssertError));
}
}
TestError[] errors;
int testsRun = 0;
startDots();
foreach (string className; testClasses) {
//Create the class:
Object testObject = null;
try {
testObject = testCreators[className]();
} catch (Throwable t) {
errors ~= TestError(className, "CONSTRUCTOR", t);
printF();
}
if (testObject is null) {
continue;
}
//setUpClass
try {
testCallers[className](testObject, "setUpClass");
} catch (Throwable t) {
errors ~= TestError(className, "setUpClass", t);
}
//Run each test of the class:
foreach (string testName; testNamesByClass[className]) {
++testsRun;
printDot();
//setUp
bool setUpOk = false;
bool allOk = true;
try {
testCallers[className](testObject, "setUp");
setUpOk = true;
} catch (Throwable t) {
errors ~= TestError(className, "setUp", t);
printF();
}
if (!setUpOk) {
continue;
}
//test
try {
testCallers[className](testObject, testName);
} catch (Throwable t) {
errors ~= TestError(className, testName, t);
allOk = false;
}
//tearDown (call anyways if test failed)
try {
testCallers[className](testObject, "tearDown");
} catch (Throwable t) {
errors ~= TestError(className, "tearDown", t);
allOk = false;
}
if (!allOk) {
printF();
}
}
//tearDownClass
try {
testCallers[className](testObject, "tearDownClass");
} catch (Throwable t) {
errors ~= TestError(className, "tearDownClass", t);
}
}
endDots();
/* Count how many problems where asserts, and how many other exceptions.
*/
int failedCount = 0;
int errorCount = 0;
foreach (TestError te; errors) {
if (te.isAssertError) {
++failedCount;
} else {
++errorCount;
}
}
/* Display results
*/
writeln();
if (failedCount == 0 && errorCount == 0) {
writeln();
printOk();
writefln(" (%d Test%s)", testsRun, ((testsRun == 1) ? "" : "s"));
return;
}
/* Errors
*/
if (errorCount != 0) {
if (errorCount == 1) {
writeln("There was 1 error:");
} else {
writefln("There were %d errors:", errorCount);
}
int i = 0;
foreach (TestError te; errors) {
//Errors are any exception except AssertError;
if (te.isAssertError) {
continue;
}
Throwable t = te.error;
writefln("%d) %s(%s)%s@%s(%d): %s", ++i,
te.testName, te.testClass,
typeid(t).name, t.file, t.line, t.msg);
}
}
/* Failures
*/
if (failedCount != 0) {
if (failedCount == 1) {
writeln("There was 1 failure:");
} else {
writefln("There were %d failures:", failedCount);
}
int i = 0;
foreach (TestError te; errors) {
//Failures are only AssertError exceptions.
if (!te.isAssertError) {
continue;
}
Throwable t = te.error;
writefln("%d) %s(%s)%s@%s(%d): %s", ++i,
te.testName, te.testClass,
typeid(t).name, t.file, t.line, t.msg);
}
}
writeln();
printFailures();
writefln("Tests run: %d, Failures: %d, Errors: %d", testsRun, failedCount, errorCount);
}
version (Posix) {
private static bool _useColor = false;
private static bool _useColorWasComputed = false;
private static bool canUseColor() {
if (!_useColorWasComputed) {
//Disable colors if the results output is written
//to a file or pipe instead of a tty:
import core.sys.posix.unistd;
_useColor = (isatty(stdout.fileno()) != 0);
_useColorWasComputed = true;
}
return _useColor;
}
private static void startColorGreen() {
if (canUseColor()) {
write("\x1B[1;37;42m"); stdout.flush();
}
}
private static void startColorRed() {
if (canUseColor()) {
write("\x1B[1;37;41m"); stdout.flush();
}
}
private static void endColors() {
if (canUseColor()) {
write("\x1B[0;;m"); stdout.flush();
}
}
} else {
private static void startColorGreen() {
}
private static void startColorRed() {
}
private static void endColors() {
}
}
private static bool showingRed = false;
private static void startDots() {
showingRed = false;
}
private static void printDot() {
startColorGreen();
write("."); stdout.flush();
endColors();
}
private static void printF() {
if (!showingRed) {
showingRed = true;
}
startColorRed();
write("F"); stdout.flush();
endColors();
}
private static void endDots() {
showingRed = false;
}
private static void printOk() {
startColorGreen();
write("OK");
endColors();
}
private static void printFailures() {
startColorRed();
write("FAILURES!!!");
endColors();
writeln();
}
/**
* Runs all the unit tests, showing the test tree as the tests run.
*/
public static void runTests_Tree() {
//List Test classes:
writeln("Unit tests: ");
foreach (string className; testClasses) {
writeln(" " ~ className);
//Create the class:
Object testObject = null;
try {
testObject = testCreators[className]();
} catch (Throwable t) {
writefln(" ERROR IN CONSTRUCTOR: " ~ className ~ ".this(): "
~ "(): %s@%s(%d): %s", typeid(t).name, t.file, t.line, t.msg);
}
if (testObject is null) {
continue;
}
//setUpClass
try {
testCallers[className](testObject, "setUpClass");
} catch (Throwable t) {
writefln(" ERROR IN setUpClass: " ~ className ~ ".setUpClass(): "
~ "(): %s@%s(%d): %s", typeid(t).name, t.file, t.line, t.msg);
}
//Run each test of the class:
foreach (string testName; testNamesByClass[className]) {
//setUp
bool setUpOk = false;
try {
testCallers[className](testObject, "setUp");
setUpOk = true;
} catch (Throwable t) {
writefln(" ERROR: setUp"
~ "(): %s@%s(%d): %s", typeid(t).name, t.file, t.line, t.msg);
}
if (!setUpOk) {
continue;
}
//test
try {
TickDuration startTime = TickDuration.currSystemTick();
testCallers[className](testObject, testName);
double elapsedMs = (TickDuration.currSystemTick() - startTime).usecs() / 1000.0;
writefln(" OK: %6.2f ms %s()", elapsedMs, testName);
} catch (Throwable t) {
writefln(" FAILED: " ~ testName
~ "(): %s@%s(%d): %s", typeid(t).name, t.file, t.line, t.msg);
}
//tearDown (call anyways if test failed)
try {
testCallers[className](testObject, "tearDown");
} catch (Throwable t) {
writefln(" ERROR: tearDown"
~ "(): %s@%s(%d): %s", typeid(t).name, t.file, t.line, t.msg);
}
}
//tearDownClass
try {
testCallers[className](testObject, "tearDownClass");
} catch (Throwable t) {
writefln(" ERROR IN tearDownClass: " ~ className ~ ".tearDownClass(): "
~ "(): %s@%s(%d): %s", typeid(t).name, t.file, t.line, t.msg);
}
}
}
/**
* Registers a class as a unit test.
*/
mixin template TestMixin() {
public static this() {
//Names of test methods:
immutable(string[]) _testMethods = _testMethodsList!(
typeof(this),
__traits(allMembers, typeof(this))
).ret;
//Factory method:
static Object createFunction() {
mixin("return (new " ~ typeof(this).stringof ~ "());");
}
//Run method:
//Generate a switch statement, that calls the method that matches the testName:
static void runTest(Object o, string testName) {
mixin(
generateRunTest!(typeof(this),
__traits(allMembers, typeof(this)))
);
}
//Register UnitTest class:
string className = typeof(this).stringof;
testClasses ~= className;
testNamesByClass[className] = _testMethods.dup;
testCallers[className] = &runTest;
testCreators[className] = &createFunction;
}
private template _testMethodsList(T, args...) {
static if (args.length == 0) {
immutable(string[]) ret = [];
} else {
//Skip strings that don't start with "test":
static if (args[0].length < 4 || args[0][0..4] != "test"
|| !(__traits(compiles, mixin("(new " ~ T.stringof ~ "())." ~ args[0] ~ "()")) ))
{
static if(args.length == 1) {
immutable(string[]) ret = [];
} else {
immutable(string[]) ret = _testMethodsList!(T, args[1..$]).ret;
}
} else {
//Return the first argument and the rest:
static if (args.length == 1) {
immutable(string[]) ret = [args[0]];
} else {
static if (args.length > 1) {
immutable(string[]) ret = [args[0]] ~ _testMethodsList!(T, args[1..$]).ret;
} else {
immutable(string[]) ret = [];
}
}
}
}
}
/**
* Generates the function that runs a method from its name.
*/
private template generateRunTest(T, args...) {
immutable(string) generateRunTest =
T.stringof ~ " testObject = cast("~T.stringof~")o; "
~"switch (testName) { "
~generateRunTestImpl!(T, args).ret
~" default: break; "
~"}";
}
/**
* Generates the case statements.
*/
private template generateRunTestImpl(T, args...) {
static if (args.length == 0) {
immutable(string) ret = "";
} else {
//Skip method names that don't start with 'test':
static if (((args[0].length < 4 || args[0][0..4] != "test")
&& (args[0].length < 5 || args[0][0..5] != "setUp")
&& (args[0].length < 8 || args[0][0..8] != "tearDown")
&& (args[0].length < 10 || args[0][0..10] != "setUpClass")
&& (args[0].length < 13 || args[0][0..13] != "tearDownClass"))
|| !(__traits(compiles, mixin("(new " ~ T.stringof ~ "())." ~ args[0] ~ "()")) ))
{
static if (args.length == 1) {
immutable(string) ret = "";
} else {
immutable(string) ret = generateRunTestImpl!(T, args[1..$]).ret;
}
} else {
//Create the case statement that calls that test:
static if (args.length == 1) {
immutable(string) ret =
"case \"" ~ args[0] ~ "\": testObject." ~ args[0] ~ "(); break; ";
} else {
immutable(string) ret =
"case \"" ~ args[0] ~ "\": testObject." ~ args[0] ~ "(); break; "
~ generateRunTestImpl!(T, args[1..$]).ret;
}
}
}
}
}