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
390
400
410
420
431
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
770
780
790
800
811
820
831
841
850
860
8716
880
8916
900
910
920
930
940
950
96771
970
980
990
1000
1010
102771
1030
104771
1050
106388
107388
108388
1090
110233
111233
112233
113233
1140
1153
1163
1173
1183
1190
12010
12110
1220
12310
1240
1258
1268
1278
1280
1292
1302
1312
1320
133127
134127
1350
1360
1370
1380
1390
1400
1410
1420
14357
1440
14557
14657
14757
1480
14957
1500
15157
15257
1530
154456
1550
15695
15795
15895
1590
1600
16157
1620
1630
1640
1650
166236
1670
168236
1690
17053
1710
1720
1730
174183
1750
1760
177236
1780
179229
1800
1810
182236
1830
1841
1850
1860
187236
1880
1896
1906
1910
1920
193236
1940
195245
1962
1970
1980
199236
2007
2010
2020
203236
2040
205236
2060
2070
2080
2090
2100
2110
2120
2130
2140
2150
2160
2170
2180
2191
2201
2210
2221
2231
2241
2250
2261
2271
2280
2292
2300
2310
2320
2330
2340
2351
2361
2370
2381
2391
2401
2410
2421
2431
2440
2451
2461
2470
2481
2491
2500
2512
2522
2532
2540
2550
2560
2570
2580
2591
2601
2610
2621
2631
2640
2651
2660
2671
2681
2690
2702
2710
2720
2730
2740
2750
2761
2771
2780
2791
2801
2810
2821
2830
2841
2851
2860
2872
2880
2890
2900
2910
2920
2931
2941
2950
2961
2971
2980
2991
3001
3010
3021
3031
3040
3052
3060
3070
3080
3090
3100
3110
3121
3131
3140
3151
3161
3170
3181
3191
3200
3211
3221
3230
3242
3250
3260
3270
3280
3290
3300
3310
3321
3331
3340
3351
3361
3370
3381
3391
3401
3410
3421
3430
3441
3450
3462
3470
3480
/++ 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; version (Have_fluent_asserts) { version = Have_fluent_asserts_core; } /// 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_core) { 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"); }