trial.reporters.html 101/102(99%) 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
404
410
420
430
440
450
460
470
480
490
500
510
520
530
544
550
564
570
58254
590
600
614
620
63252
640
650
664
670
68209
690
70207
710
720
732
740
751
760
770
781
790
800
814
820
834
844
854
864
874
884
890
90141
910
9243
930
9443
950
96756
970
98209
99209
1000
101209
1020
103209
1040
105209
1060
1072
1080
1090
110209
1110
112207
1130
1142
1150
1161
1170
1181
1190
1201
1210
1220
1230
1240
1250
1260
1270
1280
1294
1304
1314
1324
1334
1340
1354
1364
1374
1380
1394
1400
1410
1420
1430
1440
1450
1460
1470
1484
1490
1504
1510
1520
1530
1540
1550
1560
1570
1580
1590
1600
1610
1621
1631
1640
1651
1661
1670
1681
1691
1701
1711
1720
1731
1741
1750
1761
1770
1782
1792
1802
1812
1820
1832
1842
1852
1862
1870
1880
1890
1900
1910
1921
1931
1940
1951
1961
1970
1981
1991
2001
2011
2021
2030
2041
2051
2060
2071
2080
2092
2102
2112
2122
2132
2142
2150
2160
2170
2180
2190
2201
2211
2220
2231
2241
2250
2261
2271
2281
2291
2301
2310
2321
2331
2340
2351
2360
2372
2382
2392
2402
2412
2422
2430
/++ A module containing the HtmlReporter 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.html; import std.stdio; import std.array; import std.conv; import std.datetime; import std.string; import std.algorithm; import std.file; import trial.interfaces; import trial.reporters.writer; /// The "html" reporter outputs a hierarchical HTML body representation of your tests. class HtmlReporter : ILifecycleListener { string header = `<!DOCTYPE html> <html> <head> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <meta charset="UTF-8"> <title>Test Result</title> </head> <body>`; string footer = `</body></html>`; private { bool success = true; } this(string destination) { } void begin(ulong) { } void update() { } void end(SuiteResult[] results) { string content = header; string escapeHtml(string text) { return text.replace("<", "<").replace(">", ">"); } string duration(T)(T item) pure { return ` <span class="duration">` ~ (item.end - item.begin).to!string ~ `</span> `; } string testStatus(T)(T item) { if (item.status == TestResult.Status.success) { return `<span class="label label-success">` ~ item.status.to!string ~ `</span>`; } if (item.status == TestResult.Status.failure) { return `<span class="label label-danger">` ~ item.status.to!string ~ `</span>`; } return `<span class="label label-info">` ~ item.status.to!string ~ `</span>`; } content ~= `<div class="container-fluid">`; string details; double passes = 0; double failures = 0; double pending = 0; double other = 0; Duration totalDuration = Duration.zero; foreach (result; results) { details ~= "\n<h1>" ~ escapeHtml(result.name) ~ "<small>" ~ duration( result) ~ "</small></h1>\n"; totalDuration += result.end - result.begin; foreach (test; result.tests) { details ~= `<div class="row">`; details ~= `<div class="col-sm-4"><strong class='test-name'>` ~ escapeHtml( test.name) ~ `</strong></div>`; details ~= `<div class="col-sm-8">` ~ testStatus( test) ~ ` <span class="label label-default">` ~ duration(test) ~ `</span></div>`; details ~= `</div>`; if (test.throwable !is null) { details ~= `<pre>` ~ escapeHtml(test.throwable.to!string) ~ `</pre>`; } if (test.status == TestResult.Status.success) { passes++; } else if (test.status == TestResult.Status.failure) { failures++; } else if (test.status == TestResult.Status.pending) { pending++; } else { other++; } } } content ~= `<p>passes: <strong>` ~ passes.to!string ~ `</strong></p>`; content ~= `<p>failures: <strong>` ~ failures.to!string ~ `</strong></p>`; content ~= `<p>pending: <strong>` ~ pending.to!string ~ `</strong></p>`; content ~= `<p>other: <strong>` ~ other.to!string ~ `</strong></p>`; content ~= `<p>duration: <strong>` ~ totalDuration.to!string ~ `</strong></p>`; auto passPercent = ((passes / (passes + failures + other)) * 100); auto failurePercent = ((failures / (passes + failures + other)) * 100); auto otherPercent = ((other / (passes + failures + other)) * 100); content ~= `<div class="progress"> <div class="progress-bar progress-bar-success" style="width: ` ~ passPercent.to!string ~ `%;"></div> <div class="progress-bar progress-bar-danger" style="width: ` ~ failurePercent.to!string ~ `%;"></div> <div class="progress-bar" style="width: ` ~ otherPercent.to!string ~ `%;"></div> </div>`; content ~= details ~ "\n</div>" ~ footer; std.file.write("trial-result.html", content); } } version (unittest) { import fluent.asserts; } @("it should print a success test") unittest { auto writer = new BufferedWriter; auto reporter = new HtmlReporter("trial-result.html"); auto begin = Clock.currTime - 10.seconds; auto end = begin + 10.seconds; auto testResult = new TestResult("some test"); testResult.begin = begin; testResult.end = end; testResult.status = TestResult.Status.success; SuiteResult[] result = [ SuiteResult("Test Suite", begin, end, [testResult]) ]; reporter.end(result); auto text = readText("trial-result.html"); text.should.contain(`<h1>Test Suite<small> <span class="duration">10 secs</span> </small></h1>`); text.should.contain(`<strong class='test-name'>some test</strong>`); text.should.contain(`<span class="label label-success">success</span>`); text.should.contain(`<span class="label label-default"> <span class="duration">10 secs</span> </span>`); text.should.contain(`passes: <strong>1</strong>`); text.should.contain(`failures: <strong>0</strong>`); text.should.contain(`other: <strong>0</strong>`); text.should.contain(`duration: <strong>10 secs</strong>`); } @("it should print a failure test") unittest { auto writer = new BufferedWriter; auto reporter = new HtmlReporter("trial-result.html"); auto begin = Clock.currTime - 10.seconds; auto end = begin + 10.seconds; auto testResult = new TestResult("some test"); testResult.begin = begin; testResult.end = end; testResult.status = TestResult.Status.failure; testResult.throwable = new Exception("Some error"); SuiteResult[] result = [SuiteResult("Test Suite", begin, end, [testResult])]; reporter.end(result); auto text = readText("trial-result.html"); text.should.contain(`<span class="label label-danger">failure</span>`); text.should.contain(`<pre>` ~ testResult.throwable.to!string ~ `</pre>`); text.should.contain(`passes: <strong>0</strong>`); text.should.contain(`failures: <strong>1</strong>`); text.should.contain(`other: <strong>0</strong>`); text.should.contain(`duration: <strong>10 secs</strong>`); } @("it should print a pending test") unittest { auto writer = new BufferedWriter; auto reporter = new HtmlReporter("trial-result.html"); auto begin = Clock.currTime - 10.seconds; auto end = begin + 10.seconds; auto testResult = new TestResult("some test"); testResult.begin = begin; testResult.end = end; testResult.status = TestResult.Status.pending; testResult.throwable = new Exception("Some error"); SuiteResult[] result = [SuiteResult("Test Suite", begin, end, [testResult])]; reporter.end(result); auto text = readText("trial-result.html"); text.should.contain(`<span class="label label-info">pending</span>`); text.should.contain(`passes: <strong>0</strong>`); text.should.contain(`failures: <strong>0</strong>`); text.should.contain(`pending: <strong>1</strong>`); text.should.contain(`other: <strong>0</strong>`); text.should.contain(`duration: <strong>10 secs</strong>`); }