trial.reporters.spec 127/129(98%) 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
391
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
760
771
780
791
801
810
820
8316
840
8516
860
870
880
890
900
910
92704
930
940
950
960
970
98704
990
100704
1010
102354
103354
104354
1050
106218
107218
108218
109218
1100
1113
1123
1133
1143
1150
1169
1179
1180
1199
1200
1218
1228
1238
1240
1251
1261
1271
1280
129111
130111
1310
1320
1330
1340
1350
1360
1370
1380
13955
1400
14155
14255
14355
1440
14555
1460
14755
14855
1490
150402
1510
15279
15379
15479
1550
1560
15755
1580
1590
1600
1610
162220
1630
164220
1650
16651
1670
1680
1690
170169
1710
1720
173220
1740
175214
1760
1770
178220
1790
1801
1810
1820
183220
1840
1855
1865
1870
1880
189220
1900
191228
1921
1930
1940
195220
1967
1970
1980
199220
2000
201220
2020
2030
2040
2050
2060
2070
2080
2090
2100
2110
2120
2131
2141
2150
2161
2171
2181
2190
2201
2211
2220
2232
2240
2250
2260
2270
2280
2291
2301
2310
2321
2331
2341
2350
2361
2371
2380
2391
2401
2410
2421
2431
2440
2452
2462
2472
2480
2490
2500
2510
2520
2531
2541
2550
2561
2571
2580
2591
2600
2611
2621
2630
2642
2650
2660
2670
2680
2690
2701
2711
2720
2731
2741
2750
2761
2770
2781
2791
2800
2812
2820
2830
2840
2850
2860
2871
2881
2890
2901
2911
2920
2931
2941
2950
2961
2971
2980
2992
3000
3010
3020
3030
3040
3050
3061
3071
3080
3091
3101
3110
3121
3131
3140
3151
3161
3170
3182
3190
3200
3210
3220
3230
3240
3250
3261
3271
3280
3291
3301
3310
3321
3331
3341
3350
3361
3370
3381
3390
3402
3410
3420
/++ A module containing the SpecReporter This is an example of how this reporter looks <script type="text/javascript" src="https://asciinema.org/a/9z1tolgn7x55v41i3mm3wlkum.js" id="asciicast-9z1tolgn7x55v41i3mm3wlkum" 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.spec; import std.stdio; import std.array; import std.conv; import std.datetime; import std.string; import std.algorithm; import trial.interfaces; import trial.settings; import trial.reporters.writer; /// A structure containing the glyphs used for the spec reporter struct SpecGlyphs { version(Windows) { /// string ok = "+"; } else { /// string ok = "✓"; } string pending = "-"; } /// string specGlyphsToCode(SpecGlyphs glyphs) { return "SpecGlyphs(`" ~ glyphs.ok ~ "`)"; } /// This is the default reporter. The "spec" reporter outputs a hierarchical view nested just as the test cases are. class SpecReporter : ITestCaseLifecycleListener { enum Type { none, success, step, pending, failure, testBegin, testEnd, emptyLine, danger, warning } protected { int failedTests = 0; string lastSuiteName; ReportWriter writer; } private { Settings settings; } this() { writer = defaultWriter; } this(Settings settings) { writer = defaultWriter; this.settings = settings; } this(ReportWriter writer) { this.writer = writer; } private { string indentation(size_t cnt) pure { return " ".replicate(cnt); } } void write(Type t)(string text = "", size_t spaces = 0) { writer.write(indentation(spaces)); switch (t) { case Type.emptyLine: writer.writeln(""); break; case Type.success: writer.write(settings.glyphs.spec.ok, ReportWriter.Context.success); writer.write(" " ~ text, ReportWriter.Context.inactive); break; case Type.pending: writer.write(settings.glyphs.spec.pending, ReportWriter.Context.info); writer.write(" " ~ text, ReportWriter.Context.inactive); break; case Type.failure: writer.write(failedTests.to!string ~ ") " ~ text, ReportWriter.Context.danger); break; case Type.danger: writer.write(text, ReportWriter.Context.danger); break; case Type.warning: writer.write(text, ReportWriter.Context.warning); break; default: writer.write(text); } } void begin(string suite, ref TestResult test) { } protected auto printSuite(string suite) { size_t indents = 1; auto oldPieces = lastSuiteName.split("."); auto pieces = suite.split("."); lastSuiteName = suite; auto prefix = oldPieces.commonPrefix(pieces).array.length; write!(Type.emptyLine)(); indents += prefix; foreach (piece; pieces[prefix .. $]) { write!(Type.none)(piece, indents); write!(Type.emptyLine)(); indents++; } return indents; } void end(string suite, ref TestResult test) { size_t indents = 1; if (suite != lastSuiteName) { indents = printSuite(suite); } else { indents = suite.count('.') + 2; } if (test.status == TestResult.Status.success) { write!(Type.success)(test.name, indents); } if (test.status == TestResult.Status.pending) { write!(Type.pending)(test.name, indents); } if (test.status == TestResult.Status.failure) { write!(Type.failure)(test.name, indents); failedTests++; } auto timeDiff = (test.end - test.begin).total!"msecs"; if(timeDiff >= settings.warningTestDuration && timeDiff < settings.dangerTestDuration) { write!(Type.warning)(" (" ~ timeDiff.to!string ~ "ms)", 0); } if(timeDiff >= settings.dangerTestDuration) { write!(Type.danger)(" (" ~ timeDiff.to!string ~ "ms)", 0); } write!(Type.emptyLine); indents--; } } version (unittest) { import fluent.asserts; } @("it should print a success test") unittest { auto writer = new BufferedWriter; auto reporter = new SpecReporter(writer); auto suite = SuiteResult("some suite"); auto test = new TestResult("some test"); test.status = TestResult.Status.success; reporter.begin("some suite", test); reporter.end("some suite", test); writer.buffer.should.equal("\n some suite" ~ "\n ✓ some test\n"); } @("it should print two success tests") unittest { auto writer = new BufferedWriter; auto reporter = new SpecReporter(writer); auto suite = SuiteResult("some suite"); auto test1 = new TestResult("some test"); test1.status = TestResult.Status.success; auto test2 = new TestResult("other test"); test2.status = TestResult.Status.success; reporter.begin("some suite", test1); reporter.end("some suite", test1); reporter.begin("some suite", test2); reporter.end("some suite", test2); writer.buffer.should.contain("\n some suite\n"); writer.buffer.should.contain("\n ✓ some test\n"); writer.buffer.should.contain("\n ✓ other test\n"); } @("it should print a failing test") unittest { auto writer = new BufferedWriter; auto reporter = new SpecReporter(writer); auto suite = SuiteResult("some suite"); auto test = new TestResult("some test"); test.status = TestResult.Status.failure; reporter.begin("some suite", test); reporter.end("some suite", test); writer.buffer.should.equal("\n some suite" ~ "\n 0) some test\n"); } @("it should print a pending test") unittest { auto writer = new BufferedWriter; auto reporter = new SpecReporter(writer); auto suite = SuiteResult("some suite"); auto test = new TestResult("some test"); test.status = TestResult.Status.pending; reporter.begin("some suite", test); reporter.end("some suite", test); writer.buffer.should.equal("\n some suite" ~ "\n - some test\n"); } @("it should split suites by dot") unittest { auto writer = new BufferedWriter; auto reporter = new SpecReporter(writer); auto suite = SuiteResult("some.suite"); auto test = new TestResult("some test"); test.status = TestResult.Status.failure; test.throwable = new Exception("Random failure"); reporter.end("some.suite", test); reporter.end("some.suite", test); writer.buffer.should.equal( "\n" ~ " some\n" ~ " suite\n" ~ " 0) some test\n" ~ " 1) some test\n"); } @("it should omit the common suite names") unittest { auto writer = new BufferedWriter; auto reporter = new SpecReporter(writer); auto suite = SuiteResult("some.suite"); auto test = new TestResult("some test"); test.status = TestResult.Status.failure; test.throwable = new Exception("Random failure"); reporter.end("some.suite", test); reporter.end("some.other", test); writer.buffer.should.equal( "\n" ~ " some\n" ~ " suite\n" ~ " 0) some test\n\n" ~ " other\n" ~ " 1) some test\n"); } /// it should print the test duration unittest { auto writer = new BufferedWriter; auto reporter = new SpecReporter(writer); auto suite = SuiteResult("some.suite"); auto test = new TestResult("some test"); test.status = TestResult.Status.success; test.end = Clock.currTime; test.begin = test.end - 1.seconds; test.throwable = new Exception("Random failure"); reporter.end("some.suite", test); writer.buffer.should.equal( "\n" ~ " some\n" ~ " suite\n" ~ " ✓ some test (1000ms)\n"); }