trial.reporters.specsteps 85/90(94%) 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
461
470
480
490
500
510
520
530
540
550
560
570
580
590
600
610
620
630
640
650
664
670
684
690
700
710
720
734
740
750
760
770
780
790
800
810
826
836
840
850
860
870
886
890
906
913
923
932
942
951
961
970
980
990
1000
1016
1020
1037
1040
1050
1060
1076
1081
1090
1100
1116
1120
1130
1140
1150
1160
1176
1186
1196
1200
1210
1220
1230
1246
1250
1260
1270
1280
1290
1300
1310
1320
1330
1340
1350
1360
1370
1381
1391
1400
1411
1421
1431
1440
1451
1461
1470
1481
1491
1500
1511
1521
1531
1541
1551
1561
1570
1581
1590
1601
1611
1620
1631
1640
1652
1660
1670
1680
1690
1700
1710
1720
1730
1740
1750
1760
1770
1780
1791
1801
1810
1821
1831
1841
1850
1861
1871
1881
1890
1901
1910
1922
1930
1940
1950
1960
1970
1980
1990
2000
2010
2021
2031
2040
2051
2061
2071
2081
2091
2100
2111
2121
2131
2140
2151
2160
2172
2180
2190
2200
2210
2220
2230
2240
2250
2260
2271
2281
2290
2301
2311
2321
2330
2341
2351
2360
2371
2381
2390
2401
2411
2421
2431
2441
2451
2460
2471
2480
2491
2501
2510
2521
2530
2542
2550
2560
2570
2580
2590
2600
2610
2620
2630
/++ A module containing the SpecStepsReporter This is an example of how this reporter looks <script type="text/javascript" src="https://asciinema.org/a/bsk6do8t4zay9k9vznvh8yn71.js" id="asciicast-bsk6do8t4zay9k9vznvh8yn71" 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.specsteps; import trial.interfaces; import trial.reporters.spec; import trial.reporters.writer; import trial.settings; import std.datetime; import std.conv; /// A structure containing the glyphs used for the spec steps reporter struct SpecStepsGlyphs { version(Windows) { /// string testBegin = "/"; /// string testEnd = "\\"; /// string step = "|"; } else { /// string testBegin = "┌"; /// string testEnd = "└"; /// string step = "│"; } } /// string specStepsGlyphsToCode(SpecStepsGlyphs glyphs) { return "SpecStepsGlyphs(`" ~ glyphs.testBegin ~ "`, `" ~ glyphs.testEnd ~ "`, `" ~ glyphs.step ~ "`)"; } /// A flavour of the "spec" reporter that show the tests and the steps of your tests. class SpecStepsReporter : SpecReporter, ISuiteLifecycleListener, IStepLifecycleListener { private { size_t indents; size_t stepIndents; Settings settings; } this(Settings settings) { super(settings); this.settings = settings; } this(ReportWriter writer) { super(writer); } void begin(ref SuiteResult suite) { indents = printSuite(suite.name); } void end(ref SuiteResult) { } override { void begin(string suite, ref TestResult test) { stepIndents = 0; write!(Type.none)(settings.glyphs.specSteps.testBegin ~ " " ~ test.name ~ "\n", indents); } void end(string suite, ref TestResult test) { write!(Type.none)(settings.glyphs.specSteps.testEnd ~ " ", indents); if(test.status == TestResult.Status.success) { write!(Type.success)("Success", 0); } else if(test.status == TestResult.Status.failure) { write!(Type.failure)("Failure", 0); failedTests++; } else if(test.status == TestResult.Status.pending) { write!(Type.pending)("Pending", 0); } else { write!(Type.none)("Unknown", 0); } 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.none)("\n", 0); } } void begin(string suite, string test, ref StepResult s) { stepIndents++; write!(Type.none)(settings.glyphs.specSteps.step, indents); write!(Type.none)(" " ~ s.name ~ "\n", stepIndents); } void end(string suite, string test, ref StepResult) { stepIndents--; } } version (unittest) { version(Have_fluent_asserts) { import fluent.asserts; } } @("it should format the steps for a success test") unittest { auto writer = new BufferedWriter; auto reporter = new SpecStepsReporter(writer); auto suite = SuiteResult("some suite"); auto test = new TestResult("some test"); test.status = TestResult.Status.success; auto step = new StepResult(); step.name = "some step"; reporter.begin(suite); reporter.begin("some suite", test); reporter.begin("some suite", "some test", step); reporter.begin("some suite", "some test", step); reporter.end("some suite", "some test", step); reporter.end("some suite", "some test", step); reporter.begin("some suite", "some test", step); reporter.end("some suite", "some test", step); reporter.end("some suite", test); reporter.begin("some suite", test); reporter.end("some suite", test); reporter.end(suite); writer.buffer.should.equal("\n" ~ " some suite\n" ~ " ┌ some test\n" ~ " │ some step\n" ~ " │ some step\n" ~ " │ some step\n" ~ " └ ✓ Success\n" ~ " ┌ some test\n" ~ " └ ✓ Success\n"); } @("it should format a pending test") unittest { auto writer = new BufferedWriter; auto reporter = new SpecStepsReporter(writer); auto suite = SuiteResult("some suite"); auto test = new TestResult("some test"); test.status = TestResult.Status.pending; reporter.begin(suite); reporter.begin("some suite", test); reporter.end("some suite", test); reporter.end(suite); writer.buffer.should.equal("\n" ~ " some suite\n" ~ " ┌ some test\n" ~ " └ - Pending\n"); } @("it should print the duration of a long test") unittest { auto writer = new BufferedWriter; auto reporter = new SpecStepsReporter(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 - 200.msecs; reporter.begin(suite); reporter.begin("some suite", test); reporter.end("some suite", test); reporter.end(suite); writer.buffer.should.equal("\n" ~ " some suite\n" ~ " ┌ some test\n" ~ " └ ✓ Success (200ms)\n"); } @("it should format the steps for a failing test") unittest { auto writer = new BufferedWriter; auto reporter = new SpecStepsReporter(writer); auto suite = SuiteResult("some suite"); auto test = new TestResult("some test"); test.status = TestResult.Status.failure; auto step = new StepResult(); step.name = "some step"; reporter.begin(suite); reporter.begin("some suite", test); reporter.begin("some suite", "some test", step); reporter.begin("some suite", "some test", step); reporter.end("some suite", "some test", step); reporter.end("some suite", "some test", step); reporter.begin("some suite", "some test", step); reporter.end("some suite", "some test", step); reporter.end("some suite", test); reporter.begin("some suite", test); reporter.end("some suite", test); reporter.end(suite); writer.buffer.should.equal("\n" ~ " some suite\n" ~ " ┌ some test\n" ~ " │ some step\n" ~ " │ some step\n" ~ " │ some step\n" ~ " └ 0) Failure\n" ~ " ┌ some test\n" ~ " └ 1) Failure\n"); }