trial.reporters.visualtrial 190/197(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
390
400
410
420
430
440
450
460
470
480
490
500
510
520
530
546
550
566
570
580
590
600
611
621
630
640
650
660
670
680
690
700
710
720
730
741
751
761
771
781
791
801
813
821
831
840
850
860
870
880
894
904
914
920
934
943
952
962
972
982
992
1000
1010
1020
1034
1040
1054
1064
1070
1080
1090
1100
1110
1121
1131
1141
1150
1162
1170
1180
1190
1200
1210
1221
1231
1240
1251
1261
1271
1281
1291
1300
1311
1320
1332
1340
1350
1360
1370
1380
1390
1400
1410
1420
1430
1441
1451
1460
1471
1481
1491
1501
1510
1521
1530
1542
1550
1560
1570
1580
1590
1601
1611
1620
1631
1641
1651
1660
1671
1680
1692
1700
1710
1720
1730
1740
1750
1760
1770
1780
1790
1801
1811
1820
1831
1841
1850
1861
1870
1882
1890
1900
1910
1920
1931
1940
1950
1960
1971
1980
1991
2001
2010
2021
2031
2041
2050
2061
2070
2082
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
23621
2375
2384
2390
2405
2415
2425
2435
2440
2450
24616
2474
2484
2494
2501
2510
2520
2534
2544
2554
2560
2570
25812
2590
2600
2610
26212
2632
2642
2650
2660
26710
2680
26910
2701
2711
2720
2730
2741
2750
2760
2779
2789
2790
2809
2811
2821
2831
2840
2851
2861
2871
2880
2891
2901
2911
2920
2931
2941
2951
2960
2971
2981
2991
3000
3011
3021
3031
3040
3051
3061
3071
3080
3091
3100
3111
3120
3131
3141
3150
3160
3171
3181
3190
3201
3211
3221
3231
3241
3250
3260
3270
3280
3290
3300
3310
3320
3330
3340
3350
3361
3372
3381
3390
3401
3412
3422
3432
3442
3450
3461
3472
3480
3491
3502
3510
3521
3532
3540
3551
3562
3570
3581
3592
3600
3611
3622
3630
3641
3652
3660
3670
3680
3690
3700
3711
3722
3731
3740
3751
3760
3771
3781
3791
3801
3811
3820
3832
3842
3852
3860
3871
3882
3890
3900
3910
3920
3931
3940
3950
3961
3972
3980
3990
4001
4011
4020
4031
4041
4050
4062
4070
4080
4090
4100
4111
4120
4131
4141
4151
4162
4170
4181
4192
4200
4210
4220
4230
4241
4251
4260
4270
4282
4291
4300
4310
4321
4331
4341
4350
4362
4370
4380
4390
4401
4411
4420
4430
/++ A module containing the Visual Trial reporter used to send data to the Visual Studio Code plugin 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.visualtrial; import std.conv; import std.string; import std.algorithm; import std.stdio; import std.datetime; import std.exception; version(Have_fluent_asserts) { import fluentasserts.core.base; import fluentasserts.core.results; } import trial.interfaces; import trial.reporters.writer; enum Tokens : string { beginTest = "BEGIN TEST;", endTest = "END TEST;", suite = "suite", test = "test", file = "file", line = "line", labels = "labels", status = "status", errorFile = "errorFile", errorLine = "errorLine", message = "message" } /// This reporter will print the results using thr Test anything protocol version 13 class VisualTrialReporter : ILifecycleListener, ITestCaseLifecycleListener { private { ReportWriter writer; } /// this() { writer = defaultWriter; } /// this(ReportWriter writer) { this.writer = writer; } /// void begin(ulong testCount) { writer.writeln("", ReportWriter.Context._default); writer.writeln("", ReportWriter.Context._default); } /// void update() { } /// void end(SuiteResult[]) { } /// void begin(string suite, ref TestResult result) { std.stdio.stdout.flush; std.stdio.stderr.flush; writer.writeln("BEGIN TEST;", ReportWriter.Context._default); writer.writeln("suite:" ~ suite, ReportWriter.Context._default); writer.writeln("test:" ~ result.name, ReportWriter.Context._default); writer.writeln("file:" ~ result.fileName, ReportWriter.Context._default); writer.writeln("line:" ~ result.line.to!string, ReportWriter.Context._default); writer.writeln("labels:[" ~ result.labels.map!(a => a.toString).join(", ") ~ "]", ReportWriter.Context._default); std.stdio.stdout.flush; std.stdio.stderr.flush; } /// void end(string suite, ref TestResult test) { std.stdio.stdout.flush; std.stdio.stderr.flush; writer.writeln("status:" ~ test.status.to!string, ReportWriter.Context._default); if(test.status != TestResult.Status.success) { if(test.throwable !is null) { writer.writeln("errorFile:" ~ test.throwable.file, ReportWriter.Context._default); writer.writeln("errorLine:" ~ test.throwable.line.to!string, ReportWriter.Context._default); writer.writeln("message:" ~ test.throwable.msg.split("\n")[0], ReportWriter.Context._default); writer.write("error:", ReportWriter.Context._default); writer.writeln(test.throwable.toString, ReportWriter.Context._default); } } writer.writeln("END TEST;", ReportWriter.Context._default); std.stdio.stdout.flush; std.stdio.stderr.flush; } } /// it should print "The Plan" at the beginning unittest { auto writer = new BufferedWriter; auto reporter = new VisualTrialReporter(writer); reporter.begin(10); writer.buffer.should.equal("\n\n"); } /// it should print the test location unittest { auto writer = new BufferedWriter; auto reporter = new VisualTrialReporter(writer); auto test = new TestResult("other test"); test.fileName = "someFile.d"; test.line = 100; test.labels = [ Label("name", "value"), Label("name1", "value1") ]; test.status = TestResult.Status.success; reporter.begin("some suite", test); writer.buffer.should.equal("BEGIN TEST;\n" ~ "suite:some suite\n" ~ "test:other test\n" ~ "file:someFile.d\n" ~ "line:100\n" ~ `labels:[{ "name": "name", "value": "value" }, { "name": "name1", "value": "value1" }]` ~ "\n"); } /// it should print a sucess test unittest { auto writer = new BufferedWriter; auto reporter = new VisualTrialReporter(writer); auto test = new TestResult("other test"); test.fileName = "someFile.d"; test.line = 100; test.status = TestResult.Status.success; reporter.end("some suite", test); writer.buffer.should.equal("status:success\nEND TEST;\n"); } /// it should print a failing test with a basic throwable unittest { auto writer = new BufferedWriter; auto reporter = new VisualTrialReporter(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("status:failure\n" ~ "errorFile:file.d\n" ~ "errorLine:42\n" ~ "message:Test's failure\n" ~ "error:object.Exception@file.d(42): Test's failure\n" ~ "END TEST;\n"); } /// it should not print the YAML if the throwable is missing unittest { auto writer = new BufferedWriter; auto reporter = new VisualTrialReporter(writer); auto test = new TestResult("other's test"); test.status = TestResult.Status.failure; reporter.end("some suite", test); writer.buffer.should.equal("status:failure\nEND 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 VisualTrialReporter(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("status:failure\n" ~ "errorFile:unknown\n" ~ "errorLine:0\n" ~ "message:message\n" ~ "error:fluentasserts.core.base.TestException@unknown(0): message\n\n" ~ " Extra:a\n" ~ " Missing:b\n\n" ~ "END TEST;\n"); } /// Parse the output from the visual trial reporter class VisualTrialReporterParser { TestResult testResult; string suite; bool readingTest; alias ResultEvent = void delegate(TestResult); alias OutputEvent = void delegate(string); ResultEvent onResult; OutputEvent onOutput; private { bool readingErrorMessage; } /// add a line to the parser void add(string line) { if(line == Tokens.beginTest) { if(testResult is null) { testResult = new TestResult("unknown"); } readingTest = true; testResult.begin = Clock.currTime; testResult.end = Clock.currTime; return; } if(line == Tokens.endTest) { enforce(testResult !is null, "The test result was not created!"); readingTest = false; if(onResult !is null) { onResult(testResult); } readingErrorMessage = false; testResult = null; return; } if(!readingTest) { return; } if(readingErrorMessage) { testResult.throwable.msg ~= "\n" ~ line; return; } auto pos = line.indexOf(":"); if(pos == -1) { if(onOutput !is null) { onOutput(line); } return; } string token = line[0 .. pos]; string value = line[pos+1 .. $]; switch(token) { case Tokens.suite: suite = value; break; case Tokens.test: testResult.name = value; break; case Tokens.file: testResult.fileName = value; break; case Tokens.line: testResult.line = value.to!size_t; break; case Tokens.labels: testResult.labels = Label.fromJsonArray(value); break; case Tokens.status: testResult.status = value.to!(TestResult.Status); break; case Tokens.errorFile: if(testResult.throwable is null) { testResult.throwable = new ParsedVisualTrialException(); } testResult.throwable.file = value; break; case Tokens.errorLine: if(testResult.throwable is null) { testResult.throwable = new ParsedVisualTrialException(); } testResult.throwable.line = value.to!size_t; break; case Tokens.message: enforce(testResult.throwable !is null, "The throwable must exist!"); testResult.throwable.msg = value; readingErrorMessage = true; break; default: if(onOutput !is null) { onOutput(line); } } } } /// Parse a successful test unittest { auto parser = new VisualTrialReporterParser(); parser.testResult.should.beNull; auto begin = Clock.currTime; parser.add("BEGIN TEST;"); parser.testResult.should.not.beNull; parser.testResult.begin.should.be.greaterOrEqualTo(begin); parser.testResult.end.should.be.greaterOrEqualTo(begin); parser.testResult.status.should.equal(TestResult.Status.created); parser.add("suite:suite name"); parser.suite.should.equal("suite name"); parser.add("test:test name"); parser.testResult.name.should.equal("test name"); parser.add("file:some file.d"); parser.testResult.fileName.should.equal("some file.d"); parser.add("line:22"); parser.testResult.line.should.equal(22); parser.add(`labels:[ { "name": "name1", "value": "label1" }, { "name": "name2", "value": "label2" }]`); parser.testResult.labels.should.equal([Label("name1", "label1"), Label("name2", "label2")]); parser.add("status:success"); parser.testResult.status.should.equal(TestResult.Status.success); parser.add("END TEST;"); parser.testResult.should.beNull; } /// Parse a failing test unittest { auto parser = new VisualTrialReporterParser(); parser.testResult.should.beNull; auto begin = Clock.currTime; parser.add("BEGIN TEST;"); parser.add("errorFile:file.d"); parser.add("errorLine:147"); parser.add("message:line1"); parser.add("line2"); parser.add("line3"); parser.testResult.throwable.should.not.beNull; parser.testResult.throwable.file.should.equal("file.d"); parser.testResult.throwable.line.should.equal(147); parser.add("END TEST;"); parser.testResult.should.beNull; } /// Raise an event when the test is ended unittest { bool called; void checkResult(TestResult result) { called = true; result.should.not.beNull; } auto parser = new VisualTrialReporterParser(); parser.onResult = &checkResult; parser.add("BEGIN TEST;"); parser.add("END TEST;"); called.should.equal(true); } /// It should not replace a test result that was already assigned unittest { auto testResult = new TestResult(""); auto parser = new VisualTrialReporterParser(); parser.testResult = testResult; parser.add("BEGIN TEST;"); parser.testResult.should.equal(testResult); parser.add("END TEST;"); parser.testResult.should.beNull; } /// It should raise an event with unparsed lines unittest { bool raised; auto parser = new VisualTrialReporterParser(); void onOutput(string line) { line.should.equal("some output"); raised = true; } parser.onOutput = &onOutput; parser.add("BEGIN TEST;"); parser.add("some output"); raised.should.equal(true); } class ParsedVisualTrialException : Exception { this() { super(""); } }