trial.reporters.tap 59/61(96%) 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
395
400
415
420
430
440
450
461
471
480
490
500
510
520
530
540
550
560
570
580
590
600
610
620
630
644
651
660
673
680
690
703
712
721
730
741
750
760
772
780
790
800
810
820
830
840
850
860
870
886
890
901
910
921
931
941
951
961
971
981
990
1000
1010
1020
1031
1041
1051
1061
1071
1081
1090
1100
1110
1120
1130
1140
1150
1160
1170
1181
1191
1201
1210
1222
1230
1240
1250
1260
1270
1281
1291
1300
1311
1321
1330
1341
1350
1362
1370
1380
1390
1400
1410
1421
1431
1440
1451
1461
1471
1480
1491
1500
1512
1520
1530
1540
1550
1560
1570
1580
1590
1600
1610
1620
1631
1641
1650
1661
1671
1680
1691
1700
1712
1720
1730
1740
1750
1761
1770
1780
1790
1801
1810
1821
1831
1840
1851
1861
1871
1880
1891
1900
1912
1920
1930
1940
1950
1960
1970
1980
1990
2000
2010
2020
2030
/++ A module containing the TAP13 reporter https://testanything.org/ This is an example of how this reporter looks <script type="text/javascript" src="https://asciinema.org/a/135734.js" id="asciicast-135734" 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.tap; import std.conv; import std.string; import std.algorithm; version(Have_fluent_asserts_core) { import fluentasserts.core.base; import fluentasserts.core.results; } import trial.interfaces; import trial.reporters.writer; /// This reporter will print the results using thr Test anything protocol version 13 class TapReporter : ILifecycleListener, ITestCaseLifecycleListener { private { ReportWriter writer; } /// this() { writer = defaultWriter; } /// this(ReportWriter writer) { this.writer = writer; } /// void begin(ulong testCount) { writer.writeln("TAP version 13", ReportWriter.Context._default); writer.writeln("1.." ~ testCount.to!string, ReportWriter.Context._default); } /// void update() { } /// void end(SuiteResult[]) { } /// void begin(string, ref TestResult) { } /// void end(string suite, ref TestResult test) { if(test.status == TestResult.Status.success) { writer.writeln("ok - " ~ suite ~ "." ~ test.name, ReportWriter.Context._default); } else { writer.writeln("not ok - " ~ suite ~ "." ~ test.name, ReportWriter.Context._default); version(Have_fluent_asserts_core) { if(test.throwable !is null) { if(cast(TestException) test.throwable !is null) { printTestException(test); } else { printThrowable(test); } writer.writeln(""); } } else { printThrowable(test); } } } version(Have_fluent_asserts_core) { private void printTestException(ref TestResult test) { auto diagnostic = test.throwable.msg.split("\n").map!(a => "# " ~ a).join("\n"); auto msg = test.throwable.msg.split("\n")[0]; writer.writeln(diagnostic, ReportWriter.Context._default); writer.writeln(" ---", ReportWriter.Context._default); writer.writeln(" message: '" ~ msg ~ "'", ReportWriter.Context._default); writer.writeln(" severity: " ~ test.status.to!string, ReportWriter.Context._default); writer.writeln(" location:", ReportWriter.Context._default); writer.writeln(" fileName: '" ~ test.throwable.file.replace("'", "\'") ~ "'", ReportWriter.Context._default); writer.writeln(" line: " ~ test.throwable.line.to!string, ReportWriter.Context._default); } } private void printThrowable(ref TestResult test) { writer.writeln(" ---", ReportWriter.Context._default); writer.writeln(" message: '" ~ test.throwable.msg ~ "'", ReportWriter.Context._default); writer.writeln(" severity: " ~ test.status.to!string, ReportWriter.Context._default); writer.writeln(" location:", ReportWriter.Context._default); writer.writeln(" fileName: '" ~ test.throwable.file.replace("'", "\'") ~ "'", ReportWriter.Context._default); writer.writeln(" line: " ~ test.throwable.line.to!string, ReportWriter.Context._default); } } version(unittest) { import fluent.asserts; } /// it should print "The Plan" at the beginning unittest { auto writer = new BufferedWriter; auto reporter = new TapReporter(writer); reporter.begin(10); writer.buffer.should.equal("TAP version 13\n1..10\n"); } /// it should print a sucess test unittest { auto writer = new BufferedWriter; auto reporter = new TapReporter(writer); auto test = new TestResult("other test"); test.status = TestResult.Status.success; reporter.end("some suite", test); writer.buffer.should.equal("ok - some suite.other test\n"); } /// it should print a failing test with a basic throwable unittest { auto writer = new BufferedWriter; auto reporter = new TapReporter(writer); auto test = new TestResult("other's test"); test.status = TestResult.Status.failure; test.throwable = new Exception("Test's failure", "file.d", 42); reporter.end("some suite", test); writer.buffer.should.equal("not ok - some suite.other's test\n" ~ " ---\n" ~ " message: 'Test\'s failure'\n" ~ " severity: failure\n" ~ " location:\n" ~ " fileName: 'file.d'\n" ~ " line: 42\n\n"); } /// it should not print the YAML if the throwable is missing unittest { auto writer = new BufferedWriter; auto reporter = new TapReporter(writer); auto test = new TestResult("other's test"); test.status = TestResult.Status.failure; reporter.end("some suite", test); writer.buffer.should.equal("not ok - some suite.other's test\n"); } /// it should print the results of a TestException unittest { IResult[] results = [ cast(IResult) new MessageResult("message"), cast(IResult) new ExtraMissingResult("a", "b") ]; auto exception = new TestException(results, "unknown", 0); auto writer = new BufferedWriter; auto reporter = new TapReporter(writer); auto test = new TestResult("other's test"); test.status = TestResult.Status.failure; test.throwable = exception; reporter.end("some suite", test); writer.buffer.should.equal("not ok - some suite.other's test\n" ~ "# message\n" ~ "# \n" ~ "# Extra:a\n" ~ "# Missing:b\n" ~ "# \n" ~ " ---\n" ~ " message: 'message'\n" ~ " severity: failure\n" ~ " location:\n" ~ " fileName: 'unknown'\n" ~ " line: 0\n\n"); }