trial.discovery.unit 249/252(98%) 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
421478
430
44570
450
460
47908
480
4949
500
510
521711
530
5416
550
560
571673
580
5926
600
610
62817
630
640
650
660
670
681
690
700
710
720
730
740
750
760
770
780
790
800
810
820
830
840
850
860
871
880
892
902
912
922
932
942
952
960
972
982
992
1002
1012
1020
1030
1040
1050
1060
1070
1080
1090
1100
1110
1120
1130
1140
1150
116170
1170
11857
1190
12064
1210
1220
12320
1240
1250
126150
1270
1287
1297
1300
13127
13220
1330
13477
1350
13620
13726
1380
1392
1400
1410
1420
14334
14427
1450
14647
14720
1480
1490
1507
1510
1520
153143
1540
1550
1560
1570
1584256
1590
160392
1610
1620
1630
1640
1651302
1660
167651
1680
169164
1700
1710
172487
1730
174451
1750
1760
17768
1780
17932
1800
1810
1824
1830
1840
1850
1860
1870
1882
1892
1902
1912
1922
1932
1942
1950
1960
1970
1980
1992
2000
2011473
2021471
203818
2040
2052
2062
2070
2081959
2090
2101302
2110
212483
2130
2140
2150
216168
217168
2180
2190
2200
2212
2220
2232
2240
2250
2262
2270
2280
2290
2300
2310
232125
2330
2340
2350
2360
2370
2382
2392
2402
2412
2422
2432
2440
2450
2460
2470
248575
2490
250575
2511
2521
2530
2542
2551
2560
2570
2580
2590
2600
2610
2620
2630
2640
2650
2660
2670
2680
2690
2700
2710
2720
273574
2742519
2754070
2761551
277484
2780
279574
280484
2810
2820
28390
2840
2850
2860
2870
2880
2890
2900
2910
2920
2930
29456
2950
2960
2970
2980
2997
3000
3010
3020
3030
3040
3050
3067
3070
3087
3090
3100
3110
3120
3137
3147
3150
3167
3177
3180
3197
3200
32141419
3220
32341419
3240
325784
326784
3270
3280
32941419
3300
33149
3320
3330
33441419
3350
336119
3370
33814
3390
3400
341119
3420
3430
34441419
3450
34642
3470
3480
34941419
3500
351168
352168
353168
3540
355119
3560
357280
3580
35914
3600
3610
362119
3630
3647
3650
3660
367119
3680
36928
3700
3710
372119
3730
3747
3750
3760
377119
3780
379119
3800
381119
3820
3830
3840
3850
3867
3870
3880
3890
3900
3910
3920
3930
3940
3950
3960
3970
3980
399286
400286
4010
402114
4030
4040
4050
40696
4070
4080
4090
410286
411286
4120
4130
4140
415286
4160
4170
418482
4190
4200
4210
422196
423196
4240
4250
4260
4270
4280
4290
4300
431565
4327
4330
4340
435286
4360
4370
4380
4390
440286
4410
442286
443286
4440
4450
4460
447286
4480
4490
4500
4510
4520
4530
454286
4550
4560
4570
4580
459286
4600
461114
4620
4630
4640
46518
4660
4670
4680
469286
4700
4710
4720
4730
4740
475323
4761
4770
4780
479286
4800
481286
482201
4830
4840
485286
4860
487286
4880
4890
4900
4910
4920
4930
494323
4950
4960
4970
4981926
4990
5000
5010
5020
5030
504285
5050
506285
5070
5080
5090
5100
5110
5120
5130
5140
5150
5160
5170
5180
5190
5200
5210
5220
5230
5240
5250
5260
5270
5280
5290
5300
5310
5320
5330
5340
5350
5360
5370
5380
5390
5400
5410
5420
5430
5440
5450
5460
5470
5480
5490
5500
5510
5520
5530
5540
5550
5560
5570
5580
5590
5600
5610
5620
5630
5640
5650
5660
5670
5680
5690
5700
5710
5720
5730
5740
5750
5760
5770
5780
5790
5800
5810
5820
5830
5840
5850
5860
5870
5880
5890
5900
5910
5920
5930
5940
5950
5960
5970
5980
5990
6000
6010
6020
6030
6040
6050
6060
6070
6080
6090
6100
6112
6120
6130
6140
6150
6162
6170
6180
6190
6200
6212
6220
6230
6240
6250
6260
6271
6280
6291
6300
6312
6320
6332
6340
6350
6360
6370
6380
6390
6401
6410
6421
6430
6442
6450
6461
6475
6480
6492
6504
6512
6520
6530
6540
6550
6560
6571
6581
6590
6601
6610
6622
6630
6641
66515
6660
6672
6682
6692
6700
6710
6720
6730
6740
6750
6761
6770
6781
6792
6800
6811
6823
6830
6842
6856
6866
6870
6880
6890
6900
6910
6921
6931
6940
6951
6962
6970
69811
6992
7000
7011
7020
7036
7046
7050
7060
7070
7080
7090
7101
7111
7120
7131
7142
7150
7169
7172
7180
7191
7200
7214
7224
7230
7240
7250
7260
7270
7281
7291
7300
7311
7322
7330
7341
73513
7362
7370
7382
7390
7400
7410
7420
7430
7440
7451
7461
7470
7481
7490
7502
7510
75215
7532
7540
7551
7560
7572
7582
7592
7600
7610
7620
7630
7640
7651
7660
7671
7682
7690
77018
7712
7720
7730
7740
7750
7760
7771
7781
7790
78018
7811
7820
7830
7840
7850
7860
7871
7880
7891
7900
7911
7920
793107
7940
7950
7961
79718
79817
7990
/++ A module containing the default test discovery logic 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.discovery.unit; import std.string; import std.traits; import std.conv; import std.array; import std.file; import std.algorithm; import std.range; import std.typecons; import trial.interfaces; import trial.discovery.code; version (Have_fluent_asserts) { version = Have_fluent_asserts_core; } static if(__VERSION__ >= 2077) { enum unitTestKey = "__un" ~ "ittest_"; } else { enum unitTestKey = "__un" ~ "ittestL"; } enum CommentType { none, begin, end, comment } CommentType commentType(T)(T line) { if (line.length < 2) { return CommentType.none; } if (line[0 .. 2] == "//") { return CommentType.comment; } if (line[0 .. 2] == "/+" || line[0 .. 2] == "/*") { return CommentType.begin; } if (line.indexOf("+/") != -1 || line.indexOf("*/") != -1) { return CommentType.end; } return CommentType.none; } @("It should group comments") unittest { string comments = "//line 1 // line 2 //// other line /** line 3 line 4 ****/ //// other line /++ line 5 line 6 +++/ /** line 7 * * line 8 */"; auto results = comments.compressComments; results.length.should.equal(6); results[0].value.should.equal("line 1 line 2"); results[1].value.should.equal("other line"); results[2].value.should.equal("line 3 line 4"); results[3].value.should.equal("other line"); results[4].value.should.equal("line 5 line 6"); results[5].value.should.equal("line 7 line 8"); results[0].line.should.equal(2); results[1].line.should.equal(4); results[2].line.should.equal(7); results[3].line.should.equal(9); results[4].line.should.equal(13); } struct Comment { ulong line; string value; string toCode() { return `Comment(` ~ line.to!string ~ `, "` ~ value.replace(`\`, `\\`).replace(`"`, `\"`) ~ `")`; } } Comment[] commentGroupToString(T)(T[] group) { if (group.front[1] == CommentType.comment) { auto slice = group.until!(a => a[1] != CommentType.comment).array; string value = slice.map!(a => a[2].stripLeft('/').array.to!string).map!(a => a.strip) .join(' ').array.to!string; return [Comment(slice[slice.length - 1][0], value)]; } if (group.front[1] == CommentType.begin) { auto ch = group.front[2][1]; auto index = 0; auto newGroup = group.map!(a => Tuple!(int, CommentType, immutable(char), string)(a[0], a[1], a[2].length > 2 ? a[2][1] : ' ', a[2])).array; foreach (item; newGroup) { index++; if (item[1] == CommentType.end && item[2] == ch) { break; } } auto slice = group.map!(a => Tuple!(int, CommentType, immutable(char), string)(a[0], a[1], a[2].length > 2 ? a[2][1] : ' ', a[2])).take(index); string value = slice.map!(a => a[3].strip).map!(a => a.stripLeft('/') .stripLeft(ch).array.to!string).map!(a => a.strip).join(' ') .until(ch ~ "/").array.stripRight('/').stripRight(ch).strip.to!string; return [Comment(slice[slice.length - 1][0], value)]; } return []; } string getComment(const Comment[] comments, const ulong line, const string defaultValue) pure { auto r = comments.filter!(a => (line - a.line) < 3); return r.empty ? defaultValue : r.front.value; } bool connects(T)(T a, T b) { auto items = a[0] < b[0] ? [a, b] : [b, a]; if (items[1][0] - items[0][0] != 1) { return false; } if (a[1] == b[1]) { return true; } if (items[0][1] != CommentType.end && items[1][1] != CommentType.begin) { return true; } return false; } @("check comment types") unittest { "".commentType.should.equal(CommentType.none); "some".commentType.should.equal(CommentType.none); "//some".commentType.should.equal(CommentType.comment); "/+some".commentType.should.equal(CommentType.begin); "/*some".commentType.should.equal(CommentType.begin); "some+/some".commentType.should.equal(CommentType.end); "some*/some".commentType.should.equal(CommentType.end); } auto compressComments(string code) { Comment[] result; auto lines = code.splitter("\n").map!(a => a.strip).enumerate(1) .map!(a => Tuple!(int, CommentType, string)(a[0], a[1].commentType, a[1])).filter!( a => a[2] != "").array; auto tmp = [lines[0]]; auto prev = lines[0]; foreach (line; lines[1 .. $]) { if (tmp.length == 0 || line.connects(tmp[tmp.length - 1])) { tmp ~= line; } else { result ~= tmp.commentGroupToString; tmp = [line]; } } if (tmp.length > 0) { result ~= tmp.commentGroupToString; } return result; } /// Remove comment tokens string clearCommentTokens(string text) { return text.strip('/').strip('+').strip('*').strip; } /// clearCommentTokens should remove comment tokens unittest { clearCommentTokens("// text").should.equal("text"); clearCommentTokens("///// text").should.equal("text"); clearCommentTokens("/+++ text").should.equal("text"); clearCommentTokens("/*** text").should.equal("text"); clearCommentTokens("/*** text ***/").should.equal("text"); clearCommentTokens("/+++ text +++/").should.equal("text"); } size_t extractLine(string name) { static if(__VERSION__ >= 2077) { auto idx = name.indexOf("_d_"); if(idx > 0) { idx += 3; auto lastIdx = name.lastIndexOf("_"); if(idx != -1 && isNumeric(name[idx .. lastIdx])) { return name[idx .. lastIdx].to!size_t; } } } else { enum len = unitTestKey.length; if(name.length < len) { return 0; } auto postFix = name[len .. $]; auto idx = postFix.indexOf("_"); if(idx != -1 && isNumeric(postFix[0 .. idx])) { return postFix[0 .. idx].to!size_t; } } auto pieces = name.split("_") .filter!(a => a != "") .map!(a => a[0] == 'L' ? a[1..$] : a) .filter!(a => a.isNumeric) .map!(a => a.to!size_t).array; if(pieces.length > 0) { return pieces[0]; } return 0; } /// The default test discovery looks for unit test sections and groups them by module class UnitTestDiscovery : ITestDiscovery { TestCase[string][string] testCases; static Comment[][string] comments; TestCase[] getTestCases() { return testCases.values.map!(a => a.values).joiner.array; } TestCase[] discoverTestCases(string file) { TestCase[] testCases = []; version (Have_fluent_asserts_core) version (Have_libdparse) { import fluentasserts.core.results; auto tokens = fileToDTokens(file); void noTest() { assert(false, "you can not run this test"); } auto iterator = TokenIterator(tokens); auto moduleName = iterator.skipUntilType("module").skipOne.readUntilType(";").strip; string lastName; DLangAttribute[] attributes; foreach (token; iterator) { auto type = str(token.type); if (type == "}") { lastName = ""; attributes = []; } if (type == "@") { attributes ~= iterator.readAttribute; } if (type == "comment") { if (lastName != "") { lastName ~= " "; } lastName ~= token.text.clearCommentTokens; } if (type == "version") { iterator.skipUntilType(")"); } if (type == "unittest") { auto issues = attributes.filter!(a => a.identifier == "Issue"); auto flakynes = attributes.filter!(a => a.identifier == "Flaky"); auto stringAttributes = attributes.filter!(a => a.identifier == ""); Label[] labels = []; foreach (issue; issues) { labels ~= Label("issue", issue.value); } if (!flakynes.empty) { labels ~= Label("status_details", "flaky"); } if (!stringAttributes.empty) { lastName = stringAttributes.front.value.strip; } if (lastName == "") { lastName = "unnamed test at line " ~ token.line.to!string; } auto testCase = TestCase(moduleName, lastName, &noTest, labels); testCase.location = SourceLocation(file, token.line); testCases ~= testCase; } } } return testCases; } void addModule(string file, string moduleName)() { mixin("import " ~ moduleName ~ ";"); mixin("discover!(`" ~ file ~ "`, `" ~ moduleName ~ "`, " ~ moduleName ~ ")();"); } private { string testName(alias test)(ref Comment[] comments) { string defaultName = test.stringof.to!string; string name = defaultName; foreach (attr; __traits(getAttributes, test)) { static if (is(typeof(attr) == string)) { name = attr; } } enum len = unitTestKey.length; size_t line; try { line = extractLine(name); } catch(Exception) {} if (name == defaultName && name.indexOf(unitTestKey) == 0) { try { if(line != 0) { name = comments.getComment(line, defaultName); } } catch (Exception e) { } } if (name == defaultName || name == "") { name = "unnamed test at line " ~ line.to!string; } return name; } SourceLocation testSourceLocation(alias test)(string fileName) { string name = test.stringof.to!string; enum len = unitTestKey.length; size_t line; try { line = extractLine(name); } catch (Exception e) { return SourceLocation(); } return SourceLocation(fileName, line); } Label[] testLabels(alias test)() { Label[] labels; foreach (attr; __traits(getAttributes, test)) { static if (__traits(hasMember, attr, "labels")) { labels ~= attr.labels; } } return labels; } auto addTestCases(string file, alias moduleName, composite...)() if (composite.length == 1 && isUnitTestContainer!(composite)) { if(file !in comments) { comments[file] = file.readText.compressComments; } foreach (test; __traits(getUnitTests, composite)) { auto testCase = TestCase(moduleName, testName!(test)(comments[file]), { test(); }, testLabels!(test)); testCase.location = testSourceLocation!test(file); testCases[moduleName][test.mangleof] = testCase; } } void discover(string file, alias moduleName, composite...)() if (composite.length == 1 && isUnitTestContainer!(composite)) { addTestCases!(file, moduleName, composite); static if (isUnitTestContainer!composite) { foreach (member; __traits(allMembers, composite)) { static if (__traits(compiles, __traits(getMember, composite, member)) && isSingleField!(__traits(getMember, composite, member)) && isUnitTestContainer!(__traits(getMember, composite, member)) && !isModule!(__traits(getMember, composite, member))) { if (__traits(getMember, composite, member).mangleof !in testCases) { discover!(file, moduleName, __traits(getMember, composite, member))(); } } } } } } } private template isUnitTestContainer(DECL...) if (DECL.length == 1) { static if (!isAccessible!DECL) { enum isUnitTestContainer = false; } else static if (is(FunctionTypeOf!(DECL[0]))) { enum isUnitTestContainer = false; } else static if (is(DECL[0]) && !isAggregateType!(DECL[0])) { enum isUnitTestContainer = false; } else static if (isPackage!(DECL[0])) { enum isUnitTestContainer = true; } else static if (isModule!(DECL[0])) { enum isUnitTestContainer = DECL[0].stringof != "module object"; } else static if (!__traits(compiles, fullyQualifiedName!(DECL[0]))) { enum isUnitTestContainer = false; } else static if (!is(typeof(__traits(allMembers, DECL[0])))) { enum isUnitTestContainer = false; } else { enum isUnitTestContainer = true; } } private template isModule(DECL...) if (DECL.length == 1) { static if (is(DECL[0])) enum isModule = false; else static if (is(typeof(DECL[0])) && !is(typeof(DECL[0]) == void)) enum isModule = false; else static if (!is(typeof(DECL[0].stringof))) enum isModule = false; else static if (is(FunctionTypeOf!(DECL[0]))) enum isModule = false; else enum isModule = DECL[0].stringof.startsWith("module "); } private template isPackage(DECL...) if (DECL.length == 1) { static if (is(DECL[0])) enum isPackage = false; else static if (is(typeof(DECL[0])) && !is(typeof(DECL[0]) == void)) enum isPackage = false; else static if (!is(typeof(DECL[0].stringof))) enum isPackage = false; else static if (is(FunctionTypeOf!(DECL[0]))) enum isPackage = false; else enum isPackage = DECL[0].stringof.startsWith("package "); } private template isAccessible(DECL...) if (DECL.length == 1) { enum isAccessible = __traits(compiles, testTempl!(DECL[0])()); } private template isSingleField(DECL...) { enum isSingleField = DECL.length == 1; } private void testTempl(X...)() if (X.length == 1) { static if (is(X[0])) { auto x = X[0].init; } else { auto x = X[0].stringof; } } /// This adds asserts to the module version (unittest) { version(Have_fluent_asserts_core) { import fluent.asserts; } } /// It should extract the line from the default test name unittest { extractLine("__unittest_runTestsOnDevices_133_0()").should.equal(133); } /// It should extract the line from the default test name with _d_ in symbol name unittest { extractLine("__unittest_runTestsOnDevices_d_133_0()").should.equal(133); } @("It should extract the line from the default test name with _L in symbol name") unittest { extractLine("__unittest_L607_C1()").should.equal(607); } /// It should find this test unittest { auto testDiscovery = new UnitTestDiscovery; testDiscovery.addModule!(__FILE__, "trial.discovery.unit"); testDiscovery.testCases.keys.should.contain("trial.discovery.unit"); testDiscovery.testCases["trial.discovery.unit"].values.map!"a.name".should.contain( "It should find this test"); } /// It should find this flaky test @Flaky unittest { auto testDiscovery = new UnitTestDiscovery; testDiscovery.addModule!(__FILE__, "trial.discovery.unit"); testDiscovery.testCases.keys.should.contain("trial.discovery.unit"); auto r = testDiscovery.testCases["trial.discovery.unit"].values.filter!( a => a.name == "It should find this flaky test"); r.empty.should.equal(false).because("a flaky test is in this module"); r.front.labels.map!(a => a.name).should.equal(["status_details"]); r.front.labels[0].value.should.equal("flaky"); } /// It should find the line of this test unittest { enum line = __LINE__ - 2; auto testDiscovery = new UnitTestDiscovery; testDiscovery.addModule!(__FILE__, "trial.discovery.unit"); testDiscovery.testCases.keys.should.contain("trial.discovery.unit"); auto r = testDiscovery.testCases["trial.discovery.unit"].values.filter!( a => a.name == "It should find the line of this test"); r.empty.should.equal(false).because("the location should be present"); r.front.location.fileName.should.endWith("unit.d"); r.front.location.line.should.equal(line); } /// It should find this test with issues attributes @Issue("1") @Issue("2") unittest { auto testDiscovery = new UnitTestDiscovery; testDiscovery.addModule!(__FILE__, "trial.discovery.unit"); testDiscovery.testCases.keys.should.contain("trial.discovery.unit"); auto r = testDiscovery.testCases["trial.discovery.unit"].values.filter!( a => a.name == "It should find this test with issues attributes"); r.empty.should.equal(false).because("an issue test is in this module"); r.front.labels.map!(a => a.name).should.equal(["issue", "issue"]); r.front.labels.map!(a => a.value).should.equal(["1", "2"]); } /// The discoverTestCases should find the test with issues attributes unittest { immutable line = __LINE__ - 2; auto testDiscovery = new UnitTestDiscovery; auto tests = testDiscovery.discoverTestCases(__FILE__); tests.length.should.be.greaterThan(0); auto testFilter = tests.filter!(a => a.name == "It should find this test with issues attributes"); testFilter.empty.should.equal(false); auto theTest = testFilter.front; theTest.labels.map!(a => a.name).should.equal(["issue", "issue"]); theTest.labels.map!(a => a.value).should.equal(["1", "2"]); } /// The discoverTestCases should find the test with the flaky attribute unittest { immutable line = __LINE__ - 2; auto testDiscovery = new UnitTestDiscovery; auto tests = testDiscovery.discoverTestCases(__FILE__); tests.length.should.be.greaterThan(0); auto testFilter = tests.filter!(a => a.name == "It should find this flaky test"); testFilter.empty.should.equal(false); auto theTest = testFilter.front; theTest.labels.map!(a => a.name).should.equal(["status_details"]); theTest.labels.map!(a => a.value).should.equal(["flaky"]); } @("", "The discoverTestCases should find the test with the string attribute name") unittest { immutable line = __LINE__ - 2; auto testDiscovery = new UnitTestDiscovery; auto tests = testDiscovery.discoverTestCases(__FILE__); tests.length.should.be.greaterThan(0); auto testFilter = tests.filter!( a => a.name == "The discoverTestCases should find the test with the string attribute name"); testFilter.empty.should.equal(false); testFilter.front.labels.length.should.equal(0); } /// The discoverTestCases /// should find this test unittest { immutable line = __LINE__ - 2; auto testDiscovery = new UnitTestDiscovery; auto tests = testDiscovery.discoverTestCases(__FILE__); tests.length.should.be.greaterThan(0); auto testFilter = tests.filter!(a => a.name == "The discoverTestCases should find this test"); testFilter.empty.should.equal(false); auto thisTest = testFilter.front; thisTest.suiteName.should.equal("trial.discovery.unit"); thisTest.location.fileName.should.equal(__FILE__); thisTest.location.line.should.equal(line); } /// discoverTestCases should ignore version(unittest) unittest { auto testDiscovery = new UnitTestDiscovery; auto tests = testDiscovery.discoverTestCases(__FILE__); tests.length.should.be.greaterThan(0); auto testFilter = tests.filter!(a => a.name == "This adds asserts to the module"); testFilter.empty.should.equal(true); } unittest { /// discoverTestCases should set the default test names immutable line = __LINE__ - 3; auto testDiscovery = new UnitTestDiscovery; testDiscovery.discoverTestCases(__FILE__).map!(a => a.name) .array.should.contain("unnamed test at line 774"); } /// discoverTestCases should find the same tests like testCases unittest { auto testDiscovery = new UnitTestDiscovery; testDiscovery.addModule!(__FILE__, "trial.discovery.unit"); auto allTests = testDiscovery .getTestCases .sort!((a, b) => a.location.line < b.location.line) .array; testDiscovery .discoverTestCases(__FILE__).map!(a => a.toString).join("\n") .should.equal(allTests.map!(a => a.toString).join("\n")); }