trial.reporters.result 144/164(87%) 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
8547
8647
870
880
890
900
910
920
930
940
950
960
970
980
990
1000
1010
1020
103232
1043
1050
106229
1070
1080
109232
1100
111231
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
1380
1397
1400
1417
1420
1437
1440
1451
1460
1470
1487
1490
1503
1510
1520
1537
1540
1553
1560
1570
1587
1591
1606
1611
1620
1630
1647
1650
1660
1670
1680
1690
1700
1711
1720
1730
1740
1750
1761
1770
1780
1790
1800
1811
1820
1830
1840
1850
1863
1870
1883
1890
1901
1910
1921
1930
1940
1952
1962
1972
1980
1990
2000
2010
2026
2033
2043
2053
2060
2073
2080
2090
2100
2110
2120
2133
2143
2153
2163
2173
2180
2190
2200
2210
22225
2230
2241
2251
2260
2270
2280
2291
2300
2311
2320
2331
2340
2350
2360
2370
2380
2390
2400
2410
2420
2430
2440
2451
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
2840
2850
2860
2870
2880
2890
2900
2910
2920
2930
2940
2950
2960
2970
2980
2990
3000
3010
3021
3031
3041
3050
3061
3071
3080
3092
3100
3110
3120
3130
3140
3151
3161
3171
3180
3191
3201
3210
3221
3231
3240
3251
3261
3270
3281
3291
3300
3312
3320
3330
3340
3350
3360
3371
3381
3391
3400
3411
3421
3431
3440
3451
3461
3470
3481
3491
3500
3511
3521
3530
3541
3551
3560
3572
3580
3590
3600
3610
3620
3630
3641
3651
3661
3670
3681
3691
3701
3711
3720
3731
3741
3750
3761
3771
3780
3791
3801
3810
3821
3831
3840
3851
3861
3870
3882
3892
3900
3910
3920
3930
3940
3951
3961
3971
3980
3991
4001
4011
4021
4030
4041
4051
4060
4071
4081
4090
4101
4111
4120
4131
4141
4150
4161
4171
4180
4192
4202
4210
4220
4230
4240
4250
4261
4271
4281
4290
4301
4311
4321
4330
4341
4351
4360
4371
4381
4390
4401
4411
4420
4432
4442
4450
/++ 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) { 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(""); reportExceptions; 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(""); } 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 ", ReportWriter.Context.active); writer.write(timeDiff.to!string, ReportWriter.Context.info); writer.write("!\n", ReportWriter.Context.active); } void reportTestsResult() { string suiteText = suites == 1 ? "1 suite" : suites.to!string ~ " suites"; auto timeDiff = Clock.currTime - beginTime; writer.write("Executed ", ReportWriter.Context.active); writer.write(tests.to!string, ReportWriter.Context.info); if(failedTests > 0) { writer.write(" (", ReportWriter.Context.active); writer.write(failedTests.to!string ~ " failed", ReportWriter.Context.danger); writer.write(")", ReportWriter.Context.active); } writer.write(" tests in ", ReportWriter.Context.active); writer.write(suiteText, ReportWriter.Context.info); writer.write(" in ", ReportWriter.Context.active); writer.write(timeDiff.to!string, ReportWriter.Context.info); writer.write(".\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) { 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) { class TrialResultPrinter : ResultPrinter { @trusted: 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) { version(Have_fluent_asserts) { 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"); }