trial.reporters.spec 40/63(63%) 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
760
770
780
790
800
811
820
831
841
850
860
870
880
890
900
910
920
930
940
950
96369
970
980
990
1000
1010
102369
1030
104369
1050
106189
107189
108189
1090
110165
111165
112165
113165
1140
1150
1160
1170
1180
1190
1200
1210
1220
1230
1240
1250
1260
1270
1280
1290
1300
1310
1320
13315
13415
1350
1360
1370
1380
1390
1400
1410
1420
1439
1440
1459
1469
1479
1480
1499
1500
1519
1529
1530
15472
1550
15615
15715
15815
1590
1600
1619
1620
1630
1640
1650
166165
1670
168165
1690
1709
1710
1720
1730
174156
1750
1760
177165
1780
179165
1800
1810
182165
1830
1840
1850
1860
187165
1880
1890
1900
1910
1920
193165
1940
195165
1960
1970
1980
199165
2000
2010
2020
203165
2040
205165
2060
2070
2080
2090
2100
2110
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
/++ 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"); }