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
92765
930
940
950
960
970
98765
990
100765
1010
102386
103386
104386
1050
106233
107233
108233
109233
1100
1113
1123
1133
1143
1150
1169
1179
1180
1199
1200
1217
1227
1237
1240
1251
1261
1271
1280
129126
130126
1310
1320
1330
1340
1350
1360
1370
1380
13957
1400
14157
14257
14357
1440
14557
1460
14757
14857
1490
150453
1510
15294
15394
15494
1550
1560
15757
1580
1590
1600
1610
162235
1630
164235
1650
16653
1670
1680
1690
170182
1710
1720
173235
1740
175229
1760
1770
178235
1790
1801
1810
1820
183235
1840
1855
1865
1870
1880
189235
1900
191242
1921
1930
1940
195235
1966
1970
1980
199235
2000
201235
2020
2030
2040
2050
2060
2070
2080
2090
2100
2110
2120
2130
2140
2151
2161
2170
2181
2191
2201
2210
2221
2231
2240
2252
2260
2270
2280
2290
2300
2311
2321
2330
2341
2351
2361
2370
2381
2391
2400
2411
2421
2430
2441
2451
2460
2472
2482
2492
2500
2510
2520
2530
2540
2551
2561
2570
2581
2591
2600
2611
2620
2631
2641
2650
2662
2670
2680
2690
2700
2710
2721
2731
2740
2751
2761
2770
2781
2790
2801
2811
2820
2832
2840
2850
2860
2870
2880
2891
2901
2910
2921
2931
2940
2951
2961
2970
2981
2991
3000
3012
3020
3030
3040
3050
3060
3070
3081
3091
3100
3111
3121
3130
3141
3151
3160
3171
3181
3190
3202
3210
3220
3230
3240
3250
3260
3270
3281
3291
3300
3311
3321
3330
3341
3351
3361
3370
3381
3390
3401
3410
3422
3430
3440
/++ 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) { version(Have_fluent_asserts) { 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"); }