trial.reporters.result 146/152(96%) 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
400
410
420
430
441
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
720
730
740
750
761
770
781
791
800
810
826
830
846
850
860
870
880
8947
9047
910
920
930
940
950
960
970
980
990
1000
1010
1020
1030
1040
1050
1060
107232
1083
1090
110229
1110
1120
113232
1140
115230
1160
1170
1182
1192
1200
1212
1220
1230
1240
1250
1260
1270
1280
1290
1300
1310
1320
1330
1347
1350
1360
1370
1380
1397
1400
1417
1427
1430
1447
1450
1461
1470
1480
1497
1500
1513
1520
1530
1547
1550
1563
1570
1580
1597
1601
1616
1621
1630
1640
1657
1660
1677
1680
1690
1700
1710
1720
1730
1741
1750
1760
1770
1780
1791
1800
1810
1820
1830
1841
1850
1860
1870
1880
1893
1900
1913
1920
1931
1940
1951
1960
1970
1982
1990
2000
2010
2020
2036
2043
2053
2060
2070
2080
2090
2100
21129
2120
2132
2142
2150
2160
2170
2182
2190
2202
2210
2221
2230
2240
2250
2261
2270
2280
2290
2300
2310
2320
2330
2342
2350
2360
2370
2380
2390
2400
2410
2420
2430
2440
2451
2461
2470
2480
2490
250178
251178
2520
2530
2540
255115
256115
2570
2580
2590
2600
2610
2620
2630
2640
26513
26613
2670
2680
2690
2701
2711
2720
2730
2740
2750
2760
2770
2780
2790
2800
2810
2820
2830
2840
2850
2860
2870
2880
2890
2900
2911
2921
2931
2940
2951
2961
2970
2982
2990
3000
3010
3020
3030
3041
3051
3061
3070
3081
3091
3100
3111
3121
3130
3141
3151
3160
3171
3181
3190
3202
3210
3220
3230
3240
3250
3261
3271
3281
3290
3301
3311
3321
3330
3341
3351
3360
3371
3381
3390
3401
3411
3420
3431
3441
3450
3462
3470
3480
3490
3500
3510
3520
3531
3541
3551
3560
3571
3581
3591
3601
3610
3621
3631
3640
3651
3661
3670
3681
3691
3700
3711
3721
3730
3741
3751
3760
3772
3782
3790
3800
3810
3820
3830
3841
3851
3861
3870
3881
3891
3901
3911
3920
3931
3941
3950
3961
3971
3980
3991
4001
4010
4021
4031
4040
4051
4061
4070
4082
4092
4100
4110
4120
4130
4140
4151
4161
4171
4180
4191
4201
4211
4220
4231
4241
4250
4261
4271
4280
4291
4301
4310
4322
4332
4340
/++ 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) { version = Have_fluent_asserts_core; } 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 { @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_core) { 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"); }