trial.reporters.result 25/50(50%) 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
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
720
730
740
750
761
770
781
791
800
810
820
830
840
850
860
870
880
899
909
910
920
930
940
950
960
970
980
990
1000
1010
1020
1030
1040
1050
1060
107165
1080
1090
110165
1110
1120
113165
1140
115165
1160
1170
1180
1190
1200
1210
1220
1230
1240
1250
1260
1270
1280
1290
1300
1310
1320
1330
1341
1350
1360
1370
1380
1391
1400
1411
1421
1430
1441
1450
1460
1470
1480
1491
1500
1510
1520
1530
1541
1550
1561
1570
1580
1591
1600
1611
1620
1630
1640
1651
1660
1671
1680
1690
1700
1710
1720
1730
1740
1750
1760
1770
1780
1790
1800
1810
1820
1830
1840
1850
1860
1870
1880
1890
1900
1910
1920
1930
1940
1950
1960
1970
1980
1990
2000
2010
2020
2032
2041
2051
2060
2070
2080
2090
2100
2113
2120
2130
2140
2150
2160
2170
2180
2190
2200
2210
2220
2230
2240
2250
2260
2270
2280
2290
2300
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
2840
2850
2860
2870
2880
2890
2900
2910
2920
2930
2940
2950
2960
2970
2980
2990
3000
3010
3020
3030
3040
3050
3060
3070
3080
3090
3100
3110
3120
3130
3140
3150
3160
3170
3180
3190
3200
3210
3220
3230
3240
3250
3260
3270
3280
3290
3300
3310
3320
3330
3340
3350
3360
3370
3380
3390
3400
3410
3420
3430
3440
3450
3460
3470
3480
3490
3500
3510
3520
3530
3540
3550
3560
3570
3580
3590
3600
3610
3620
3630
3640
3650
3660
3670
3680
3690
3700
3710
3720
3730
3740
3750
3760
3770
3780
3790
3800
3810
3820
3830
3840
3850
3860
3870
3880
3890
3900
3910
3920
3930
3940
3950
3960
3970
3980
3990
4000
4010
4020
4030
4040
4050
4060
4070
4080
4090
4100
4110
4120
4130
4140
4150
4160
4170
4180
4190
4200
4210
4220
4230
4240
4250
4260
4270
4280
4290
4300
4310
4320
4330
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"); }