trial.reporters.result 135/152(88%) line coverage

      
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
180
190
200
210
220
230
240
250
260
270
280
290
300
310
320
330
340
350
360
370
380
390
401
410
420
430
440
450
460
470
480
490
500
510
520
530
540
550
560
570
580
590
600
610
620
630
640
650
660
670
680
690
700
710
721
730
741
751
760
770
786
790
806
810
820
830
840
8545
8645
870
880
890
900
910
920
930
940
950
960
970
980
990
1000
1010
1020
103216
1043
1050
106213
1070
1080
109216
1100
111215
1120
1130
1141
1151
1160
1171
1180
1190
1200
1210
1220
1230
1240
1250
1260
1270
1280
1290
1307
1310
1320
1330
1340
1357
1360
1377
1387
1390
1407
1410
1421
1430
1440
1457
1460
1473
1480
1490
1507
1510
1523
1530
1540
1557
1561
1576
1581
1590
1600
1617
1620
1637
1640
1650
1660
1670
1680
1690
1701
1710
1720
1730
1740
1751
1760
1770
1780
1790
1801
1810
1820
1830
1840
1853
1860
1873
1880
1891
1900
1911
1920
1930
1942
1950
1960
1970
1980
1996
2003
2013
2020
2030
2040
2050
2060
20725
2080
2091
2101
2110
2120
2130
2141
2150
2161
2170
2181
2190
2200
2210
2220
2230
2240
2250
2260
2270
2280
2290
2301
2310
2320
2330
2340
2350
2360
2370
2380
2390
2400
2410
2420
2430
2440
2450
2460
2470
2480
2490
2500
2510
2520
2530
2540
2550
2560
2570
2580
2590
2600
2610
2620
2630
2640
2650
2660
2670
2680
2690
2700
2710
2720
2730
2740
2750
2760
2770
2780
2790
2800
2810
2820
2830
2841
2851
2861
2870
2881
2891
2900
2912
2920
2930
2940
2950
2960
2971
2981
2991
3000
3011
3021
3030
3041
3051
3060
3071
3081
3090
3101
3111
3120
3132
3140
3150
3160
3170
3180
3191
3201
3211
3220
3231
3241
3251
3260
3271
3281
3290
3301
3311
3320
3331
3341
3350
3361
3371
3380
3392
3400
3410
3420
3430
3440
3450
3461
3471
3481
3490
3501
3511
3521
3531
3540
3551
3561
3570
3581
3591
3600
3611
3621
3630
3641
3651
3660
3671
3681
3690
3702
3712
3720
3730
3740
3750
3760
3771
3781
3791
3800
3811
3821
3831
3841
3850
3861
3871
3880
3891
3901
3910
3921
3931
3940
3951
3961
3970
3981
3991
4000
4012
4022
4030
4040
4050
4060
4070
4081
4091
4101
4110
4121
4131
4141
4150
4161
4171
4180
4191
4201
4210
4221
4231
4240
4252
4262
4270
/++ A module containing the ResultReporter This is an example of how this reporter looks <script type="text/javascript" src="https://asciinema.org/a/12x1mkxfmsj1j0f7qqwarkiyw.js" id="asciicast-12x1mkxfmsj1j0f7qqwarkiyw" async></script> Copyright: © 2017 Szabo Bogdan License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file. Authors: Szabo Bogdan +/ module trial.reporters.result; import std.stdio; import std.array; import std.conv; import std.datetime; import trial.interfaces; import trial.reporters.writer; version (Have_fluent_asserts_core) { import fluentasserts.core.base; import fluentasserts.core.results; } /// A structure containing the glyphs used for the result reporter struct TestResultGlyphs { version(Windows) { /// string error = "x"; } else { /// string error = "✖"; } } /// string testResultGlyphsToCode(TestResultGlyphs glyphs) { return "TestResultGlyphs(`" ~ glyphs.error ~ "`)"; } /// The "Result" reporter will print an overview of your test run class ResultReporter : ILifecycleListener, ITestCaseLifecycleListener, ISuiteLifecycleListener, IStepLifecycleListener { private { TestResultGlyphs glyphs; int suites; int tests; int pending; int failedTests; SysTime beginTime; ReportWriter writer; Throwable[] exceptions; string[] failedTestNames; string currentSuite; } this() { writer = defaultWriter; } this(TestResultGlyphs glyphs) { writer = defaultWriter; this.glyphs = glyphs; } this(ReportWriter writer) { this.writer = writer; } void begin(ref SuiteResult suite) { suites++; currentSuite = suite.name; } void end(ref SuiteResult suite) { } void update() { } void begin(string suite, ref TestResult test) { } void end(string suite, ref TestResult test) { if(test.status == TestResult.Status.pending) { pending++; } else { tests++; } if (test.status != TestResult.Status.failure) { return; } exceptions ~= test.throwable; failedTestNames ~= currentSuite ~ " " ~ test.name; failedTests++; } void begin(string suite, string test, ref StepResult step) { } void end(string suite, string test, ref StepResult step) { } void begin(ulong) { beginTime = Clock.currTime; } void end(SuiteResult[] results) { auto diff = Clock.currTime - beginTime; writer.writeln(""); writer.writeln(""); if (tests == 0) { reportNoTest; } if (tests == 1) { reportOneTestResult; } if (tests > 1) { reportTestsResult; } if(pending == 1) { reportOnePendingTest; } else if(pending > 1) { reportManyPendingTests; } writer.writeln(""); reportExceptions; } private { void reportNoTest() { writer.write("There are no tests to run."); } void reportOnePendingTest() { writer.write("There is a pending test.\n", ReportWriter.Context.info); } void reportManyPendingTests() { writer.write("There are " ~ pending.to!string ~ " pending tests.\n", ReportWriter.Context.info); } void reportOneTestResult() { auto timeDiff = Clock.currTime - beginTime; if (failedTests > 0) { writer.write(glyphs.error ~ " The test failed in " ~ timeDiff.to!string ~ ":", ReportWriter.Context.danger); return; } writer.write("The test succeeded in " ~ timeDiff.to!string ~ "!\n", ReportWriter.Context.info); } void reportTestsResult() { string suiteText = suites == 1 ? "1 suite" : suites.to!string ~ " suites"; auto timeDiff = Clock.currTime - beginTime; writer.write("Executed " ~ tests.to!string ~ " tests in " ~ suiteText ~ " in " ~ timeDiff.to!string ~ ".\n", ReportWriter.Context.info); } void reportExceptions() { foreach (size_t i, t; exceptions) { writer.writeln(""); writer.writeln(i.to!string ~ ") " ~ failedTestNames[i] ~ ":", ReportWriter.Context.danger); version (Have_fluent_asserts_core) { TestException e = cast(TestException) t; if (e is null) { writer.writeln(t.to!string); } else { e.print(new TrialResultPrinter(defaultWriter)); } } else { writer.writeln(t.to!string); } writer.writeln(""); } } } } version (Have_fluent_asserts_core) { class TrialResultPrinter : ResultPrinter { ReportWriter writer; this(ReportWriter writer) { this.writer = writer; } void primary(string text) { writer.write(text, ReportWriter.Context._default); writer.write(""); } void info(string text) { writer.write(text, ReportWriter.Context.info); writer.write(""); } void danger(string text) { writer.write(text, ReportWriter.Context.danger); writer.write(""); } void success(string text) { writer.write(text, ReportWriter.Context.success); writer.write(""); } void dangerReverse(string text) { writer.writeReverse(text, ReportWriter.Context.danger); writer.write(""); } void successReverse(string text) { writer.writeReverse(text, ReportWriter.Context.success); writer.write(""); } } } version (unittest) { import fluent.asserts; } @("The user should be notified with a message when no test is present") unittest { auto writer = new BufferedWriter; auto reporter = new ResultReporter(writer); SuiteResult[] results; reporter.begin(0); reporter.end(results); writer.buffer.should.contain("There are no tests to run."); } @("The user should see a nice message when one test is run") unittest { auto writer = new BufferedWriter; auto reporter = new ResultReporter(writer); SuiteResult[] results = [SuiteResult("some suite")]; results[0].tests = [new TestResult("some test")]; results[0].tests[0].status = TestResult.Status.success; reporter.begin(1); reporter.begin(results[0]); reporter.begin("some suite", results[0].tests[0]); reporter.end("some suite", results[0].tests[0]); reporter.end(results[0]); reporter.end(results); writer.buffer.should.contain("The test succeeded in"); } @("The user should see the number of suites and tests when multiple tests are run") unittest { auto writer = new BufferedWriter; auto reporter = new ResultReporter(writer); SuiteResult[] results = [SuiteResult("some suite")]; results[0].tests = [new TestResult("some test"), new TestResult("other test")]; results[0].tests[0].status = TestResult.Status.success; results[0].tests[1].status = TestResult.Status.success; reporter.begin(2); reporter.begin(results[0]); reporter.begin("some suite", results[0].tests[0]); reporter.end("some suite", results[0].tests[0]); reporter.begin("some suite", results[0].tests[1]); reporter.end("some suite", results[0].tests[1]); reporter.end(results[0]); reporter.end(results); writer.buffer.should.contain("Executed 2 tests in 1 suite in "); } @("The user should see the number if there is a pending test") unittest { auto writer = new BufferedWriter; auto reporter = new ResultReporter(writer); SuiteResult[] results = [SuiteResult("some suite")]; results[0].tests = [new TestResult("some test"), new TestResult("other test"), new TestResult("pending test")]; results[0].tests[0].status = TestResult.Status.success; results[0].tests[1].status = TestResult.Status.success; results[0].tests[2].status = TestResult.Status.pending; reporter.begin(2); reporter.begin(results[0]); reporter.begin("some suite", results[0].tests[0]); reporter.end("some suite", results[0].tests[0]); reporter.begin("some suite", results[0].tests[1]); reporter.end("some suite", results[0].tests[1]); reporter.begin("some suite", results[0].tests[2]); reporter.end("some suite", results[0].tests[2]); reporter.end(results[0]); reporter.end(results); writer.buffer.should.contain("Executed 2 tests in 1 suite in "); writer.buffer.should.contain("There is a pending test."); } @("The user should see the number if there are more than one pending tests") unittest { auto writer = new BufferedWriter; auto reporter = new ResultReporter(writer); SuiteResult[] results = [SuiteResult("some suite")]; results[0].tests = [new TestResult("some test"), new TestResult("other test"), new TestResult("pending test")]; results[0].tests[0].status = TestResult.Status.success; results[0].tests[1].status = TestResult.Status.pending; results[0].tests[2].status = TestResult.Status.pending; reporter.begin(2); reporter.begin(results[0]); reporter.begin("some suite", results[0].tests[0]); reporter.end("some suite", results[0].tests[0]); reporter.begin("some suite", results[0].tests[1]); reporter.end("some suite", results[0].tests[1]); reporter.begin("some suite", results[0].tests[2]); reporter.end("some suite", results[0].tests[2]); reporter.end(results[0]); reporter.end(results); writer.buffer.should.contain("The test succeeded in"); writer.buffer.should.contain("There are 2 pending tests."); } @("The user should see the reason of a failing test") unittest { auto writer = new BufferedWriter; auto reporter = new ResultReporter(writer); SuiteResult[] results = [SuiteResult("some suite")]; results[0].tests = [new TestResult("some test")]; results[0].tests[0].status = TestResult.Status.failure; results[0].tests[0].throwable = new Exception("Random failure"); reporter.begin(1); reporter.begin(results[0]); reporter.begin("some suite", results[0].tests[0]); reporter.end("some suite", results[0].tests[0]); reporter.end(results[0]); reporter.end(results); writer.buffer.should.contain("✖ The test failed in"); writer.buffer.should.contain("0) some suite some test:\n"); }