/
UnitTester.d
677 lines (560 loc) · 12.6 KB
/
UnitTester.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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
/**
* Copyright: Copyright (c) 2010-2011 Jacob Carlborg. All rights reserved.
* Authors: Jacob Carlborg
* Version: Initial created: Oct 17, 2010
* License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0)
*
* This is a simple unit test framework inspired by rspec. This framework is used for
* collecting unit test failures (assert exceptions) and presents them to the user in a
* nice format.
*
* The following are features of how a test report is printed:
*
* $(UL
* $(LI print the filename and line number of the failing test)
* $(LI print the description of a failing or pending test)
* $(LI print a snippet of the file around a failing test)
* $(LI print the stack trace of a failing test)
* $(LI print the number of failing, pending and passed test.
* As well as the total number of tests)
* $(LI minimal output then all tests pass)
* )
*
* If an assertion fails in a "it" block, that block will end. No other block is affected
* by the failed assertion.
*
* Examples:
* ---
* import orange.test.UnitTester;
*
* int sum (int x, int y)
* {
* return x * y;
* }
*
* unittest ()
* {
* describe("sum") in {
* it("should return the sum of the two given arguments") in {
* assert(sum(1, 2) == 3);
* }
* }
* }
*
* void main ()
* {
* run;
* }
* ---
* When the code above is run, it would print, since the test is failing, something similar:
* ---
* sum
* - should return the sum of the given arguments
*
* Failures:
* 1) sum should return the sum of the given arguments
* # main.d:44
* Stack trace:
* tango.core.Exception.AssertException@main(44): Assertion failure
*
*
* describe("sum") in {
* it("should return the sum of the given arguments") in {
* assert(sum(1, 2) == 3);
* };
* };
*
* 1 test, 1 failure
* ---
*/
module orange.test.UnitTester;
version (Tango)
{
import tango.core.Exception;
import tango.io.device.File;
import tango.io.FilePath;
import tango.io.stream.Lines;
import tango.sys.Environment;
import tango.util.Convert;
}
else
{
import core.exception;
import std.conv;
private alias AssertError AssertException;
}
import orange.core._;
import orange.util._;
/**
* Describes a test or a set of tests.
*
* Examples:
* ---
* unittest ()
* {
* describe("the description of the tests") in {
*
* };
* }
* ---
*
* Params:
* message = the message to describe the test
*
* Returns: a context in which the tests will be run
*/
Use!(void delegate (), string) describe (string message)
{
return UnitTester.instance.describe(message);
}
/**
* Describes what a test should do.
*
* Examples:
* ---
* unittest ()
* {
* describe("the description of the tests") in {
* it("should do something") in {
* // put your assert here
* };
*
* it("should do something else") in {
* // put another assert here
* }
* };
* }
* ---
*
* Params:
* message = what the test should do
*
* Returns: a context in which the test will be run
*/
Use!(void delegate (), string) it (string message)
{
return UnitTester.instance.test(message);
}
/// A delegate that will be called before each test.
void delegate () before ()
{
return UnitTester.instance.before;
}
/// A delegate that will be called before each test.
void delegate () before (void delegate () before)
{
return UnitTester.instance.before = before;
}
/// A delegate that will be called after each test.
void delegate () after ()
{
return UnitTester.instance.after;
}
/// A delegate that will be called after each test.
void delegate () after (void delegate () after)
{
return UnitTester.instance.after = after;
}
/// Runs all tests.
void run ()
{
UnitTester.instance.run;
}
private:
class UnitTester
{
private:
struct DescriptionManager
{
Description[] descriptions;
size_t lastIndex = size_t.max;
void opCatAssign (Description description)
{
descriptions ~= description;
lastIndex++;
}
void opCatAssign (Test test)
{
last.tests ~= test;
}
Description opIndex (size_t i)
{
return descriptions[i];
}
Description last ()
{
return descriptions[$ - 1];
}
Description first ()
{
return descriptions[0];
}
int opApply (int delegate(ref Description) dg)
{
int result = 0;
foreach (desc ; descriptions)
{
result = dg(desc);
if (result)
return result;
}
return result;
}
size_t length ()
{
return descriptions.length;
}
}
class Description
{
private
{
DescriptionManager descriptions;
Test[] tests;
Test[] failures;
Test[] pending;
size_t lastIndex = size_t.max;
string message;
void delegate () description;
}
this (string message)
{
this.message = message;
}
void run ()
{
if (shouldRun)
description();
}
bool shouldRun ()
{
return description !is null;
}
}
struct Test
{
void delegate () test;
string message;
AssertException exception;
bool failed ()
{
return !succeeded;
}
bool succeeded ()
{
if (exception is null)
return true;
return false;
}
void run ()
{
if (!isPending)
test();
}
bool isPending ()
{
return test is null;
}
}
static UnitTester instance_;
DescriptionManager descriptions;
Description currentDescription;
void delegate () before_;
void delegate () after_;
size_t numberOfFailures;
size_t numberOfPending;
size_t numberOfTests;
size_t failureId;
string defaultIndentation = " ";
string indentation;
static UnitTester instance ()
{
if (instance_)
return instance_;
return instance_ = new UnitTester;
}
Use!(void delegate (), string) describe (string message)
{
addDescription(message);
Use!(void delegate (), string) use;
use.args[0] = &internalDescribe;
use.args[1] = message;
return use;
}
Use!(void delegate (), string) test (string message)
{
addTest(message);
Use!(void delegate (), string) use;
use.args[0] = &internalTest;
use.args[1] = message;
return use;
}
void run ()
{
foreach (description ; descriptions)
runDescription(description);
printResult;
}
void runDescription (Description description)
{
restore(currentDescription) in {
currentDescription = description;
description.run;
foreach (desc ; description.descriptions)
runDescription(desc);
foreach (test ; description.tests)
{
if (test.isPending)
addPendingTest(description, test);
try
{
execute in {
test.run();
};
}
catch (AssertException e)
handleFailure(description, test, e);
}
};
}
void delegate () before ()
{
return before_;
}
void delegate () before (void delegate () before)
{
return before_ = before;
}
void delegate () after ()
{
return after_;
}
void delegate () after (void delegate () after)
{
return after_ = after;
}
void addTest (string message)
{
numberOfTests++;
currentDescription.tests ~= Test(null, message);
}
void addDescription (string message)
{
if (currentDescription)
currentDescription.descriptions ~= new Description(message);
else
descriptions ~= new Description(message);
}
void addPendingTest (Description description, ref Test test)
{
numberOfPending++;
description.pending ~= test;
}
void handleFailure (Description description, ref Test test, AssertException exception)
{
numberOfFailures++;
test.exception = exception;
description.failures ~= test;
}
void internalDescribe (void delegate () dg, string message)
{
if (currentDescription)
currentDescription.descriptions.last.description = dg;
else
descriptions.last.description = dg;
}
void internalTest (void delegate () dg, string message)
{
currentDescription.tests[$ - 1] = Test(dg, message);
}
void printResult ()
{
if (isAllTestsSuccessful)
return printSuccess();
foreach (description ; descriptions)
{
printDescription(description);
printResultImpl(description.descriptions);
}
failureId = 0;
printPending;
printFailures;
print("\n", numberOfTests, " ", pluralize("test", numberOfTests),", ", numberOfFailures, " ", pluralize("failure", numberOfFailures));
printNumberOfPending;
println();
}
void printResultImpl (DescriptionManager descriptions)
{
restore(indentation) in {
indentation ~= defaultIndentation;
foreach (description ; descriptions)
{
printDescription(description);
printResultImpl(description.descriptions);
}
};
}
void printDescription (Description description)
{
println(indentation, description.message);
restore(indentation) in {
indentation ~= defaultIndentation;
foreach (i, ref test ; description.tests)
{
print(indentation, "- ", test.message);
if (test.isPending)
print(" (PENDING: Not Yet Implemented)");
if (test.failed)
print(" (FAILED - ", ++failureId, ')');
println();
}
};
}
void printPending ()
{
if (!hasPending)
return;
println("\nPending:");
restore(indentation) in {
indentation ~= defaultIndentation;
foreach (description ; descriptions)
{
printPendingDescription(description);
printPendingImpl(description.descriptions);
}
};
}
void printPendingImpl (DescriptionManager descriptions)
{
foreach (description ; descriptions)
{
printPendingDescription(description);
printPendingImpl(description.descriptions);
}
}
void printPendingDescription (Description description)
{
foreach (test ; description.pending)
println(indentation, description.message, " ", test.message, "\n", indentation, indentation, "# Not Yet Implemented");
}
void printFailures ()
{
if (!hasFailures)
return;
println("\nFailures:");
restore(indentation) in {
indentation ~= defaultIndentation;
foreach (description ; descriptions)
{
printFailuresDescription(description);
printFailuresImpl(description.descriptions);
}
};
}
void printFailuresImpl (DescriptionManager descriptions)
{
foreach (description ; descriptions)
{
printFailuresDescription(description);
printFailuresImpl(description.descriptions);
}
}
void printFailuresDescription (Description description)
{
foreach (test ; description.failures)
{
auto str = indentation ~ to!(string)(++failureId) ~ ") ";
auto whitespace = toWhitespace(str.length);
println(str, description.message, " ", test.message);
println(whitespace, "# ", test.exception.file, ".d:", test.exception.line);
println(whitespace, "Stack trace:");
print(whitespace);
version (Tango)
{
test.exception.writeOut(&printStackTrace);
println();
println(readFailedTest(test));
}
}
}
void printStackTrace (string str)
{
return print(str);
/*if (str.find("start") < size_t.max ||
str.find("main") < size_t.max ||
str.find("rt.compiler.") < size_t.max ||
str.find("orange.") ||
str.find(":0") ||
str.find("_d_assert") ||
str.find("onAssertError") ||
str.find("tango.core.Exception.AssertException._ctor ") ||
str.find("object.") ||
str.find("tango.core.tools."))
return;*/
}
version (Tango)
{
string readFailedTest (ref Test test, int numberOfSurroundingLines = 3)
{
auto filename = test.exception.file.dup.replace('.', '/');
filename ~= ".d";
filename = Environment.toAbsolute(filename);
auto lineNumber = test.exception.line;
string str;
auto file = new File(filename);
foreach (i, line ; new Lines!(char)(file))
if (i >= (lineNumber - 1) - numberOfSurroundingLines && i <= (lineNumber - 1) + numberOfSurroundingLines)
str ~= line ~ '\n';
file.close;
return str;
}
}
void printNumberOfPending ()
{
if (hasPending)
print(", ", numberOfPending, " pending");
}
void printSuccess ()
{
println("All ", numberOfTests, pluralize(" test", numberOfTests), " passed successfully.");
}
bool isAllTestsSuccessful ()
{
return !hasPending && !hasFailures;
}
bool hasPending ()
{
return numberOfPending > 0;
}
bool hasFailures ()
{
return numberOfFailures > 0;
}
Use!(void delegate ()) execute ()
{
Use!(void delegate ()) use;
use.args[0] = &executeImpl;
return use;
}
void executeImpl (void delegate () dg)
{
auto before = this.before;
auto after = this.after;
if (before) before();
if (dg) dg();
if (after) after();
}
string toWhitespace (size_t value)
{
string str;
for (size_t i = 0; i < value; i++)
str ~= ' ';
return str;
}
string pluralize (string str, size_t value)
{
if (value == 1)
return str;
return str ~ "s";
}
}