trial.discovery.unit 243/246(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
381490
390
40574
410
420
43916
440
4549
460
470
481727
490
5016
510
520
531689
540
5526
560
570
58825
590
600
610
620
630
641
650
660
670
680
690
700
710
720
730
740
750
760
770
780
790
800
810
820
831
840
852
862
872
882
892
902
912
920
932
942
952
962
972
980
990
1000
1010
1020
1030
1040
1050
1060
1070
1080
1090
1100
1110
112170
1130
11457
1150
11664
1170
1180
11920
1200
1210
122150
1230
1247
1257
1260
12727
12820
1290
13077
1310
13220
13326
1340
1352
1360
1370
1380
13934
14027
1410
14247
14320
1440
1450
1467
1470
1480
149143
1500
1510
1520
1530
1544431
1550
156398
1570
1580
1590
1600
1611314
1620
163657
1640
165164
1660
1670
168493
1690
170457
1710
1720
17368
1740
17532
1760
1770
1784
1790
1800
1810
1820
1830
1842
1852
1862
1872
1882
1892
1902
1910
1920
1930
1940
1952
1960
1971485
1981483
199824
2000
2012
2022
2030
2041977
2050
2061314
2070
208489
2090
2100
2110
212168
213168
2140
2150
2160
2172
2180
2192
2200
2210
2222
2230
2240
2250
2260
2270
228125
2290
2300
2310
2320
2330
2342
2352
2362
2372
2382
2392
2400
2410
2420
2430
244577
2450
246577
2471
2481
2490
2502
2511
2520
2530
2540
2550
2560
2570
2580
2590
2600
2610
2620
2630
2640
2650
2660
2670
2680
269576
2702537
2714098
2721561
273488
2740
275576
276488
2770
2780
27988
2800
2810
2820
2830
2840
2850
2860
2870
2880
2890
29060
2910
2920
2930
2940
2957
2960
2970
2980
2990
3000
3010
3027
3030
3040
3050
3060
3070
3080
3097
3107
3110
3127
3137
3140
3157
3160
31742175
3180
31942175
3200
321812
322812
3230
3240
32542175
3260
32749
3280
3290
33042175
3310
332119
3330
33414
3350
3360
337119
3380
3390
34042175
3410
34228
3430
3440
34542175
3460
347168
348168
349168
3500
351119
3520
353280
3540
35514
3560
3570
358119
3590
3607
3610
3620
363119
3640
36528
3660
3670
368119
3690
3707
3710
3720
373119
3740
375119
3760
377119
3780
3790
3800
3810
3827
3830
3840
3850
3860
3870
3880
3890
3900
3910
3920
3930
3940
395287
396287
3970
3980
3990
4000
4010
40294
4030
4040
4050
4060
407287
4080
4090
4100
411287
4120
4130
414486
4150
4160
4170
418199
419199
4200
4210
4220
4230
4240
4250
4260
427567
4287
4290
4300
431287
4320
4330
4340
4350
436287
4370
4380
439287
4400
4410
4420
443287
4440
4450
4460
4470
4480
4490
450287
4510
4520
4530
4540
455287
4560
4570
4580
4590
4600
46118
4620
4630
4640
465287
4660
4670
4680
4690
4700
4710
47252
4730
474269
4751
4760
4770
4780
4790
480287
481202
4820
4830
484287
4850
486287
4870
4880
4890
4900
4910
4920
4930
494321
4950
4960
4970
498321
4990
5000
5010
5020
5030
5040
5050
5060
5070
5080
509283
5100
511283
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
6110
6120
6130
6140
6150
6160
6172
6180
6190
6200
6210
6222
6230
6240
6250
6260
6272
6280
6290
6300
6310
6320
6331
6340
6351
6360
6372
6380
6392
6400
6410
6420
6430
6440
6450
6461
6470
6481
6490
6502
6510
6521
65314
6540
6552
6563
6572
6580
6590
6600
6610
6620
6630
6641
6650
6661
6670
6682
6690
6701
67112
6720
6732
6742
6752
6760
6770
6780
6790
6800
6810
6821
6830
6841
6852
6860
6871
68815
6890
6902
6914
6924
6930
6940
6950
6960
6970
6981
6991
7000
7011
7022
7030
70411
7052
7060
7071
7080
7094
7104
7110
7120
7130
7140
7150
7161
7171
7180
7191
7202
7210
7229
7232
7240
7251
7260
7273
7283
7290
7300
7310
7320
7330
7341
7351
7360
7371
7382
7390
7401
74113
7422
7430
7442
7450
7460
7470
7480
7490
7500
7511
7521
7530
7541
7550
7562
7570
75815
7592
7600
7611
7620
7632
7642
7652
7660
7670
7680
7690
7700
7711
7720
7731
7742
7750
77618
7772
7780
7790
7800
7810
7820
7831
7841
7850
78618
7871
7880
7890
7900
7910
7920
7931
7940
7951
7960
7971
7980
79974
8000
8010
8021
80318
80417
8050
/++ 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; 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) 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 ~ ")(0);"); } 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; } void addTestCases(string file, alias moduleName, composite...)() if (composite.length == 1 && isUnitTestContainer!(composite)) { static if( !composite[0].stringof.startsWith("package") && std.traits.moduleName!composite != moduleName ) { return; } else { 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...)(int index) if (composite.length == 1 && isUnitTestContainer!(composite)) { if(index > 10) { return; } addTestCases!(file, moduleName, composite); static if (isUnitTestContainer!composite) { foreach (member; __traits(allMembers, composite)) { static if(!is( typeof(__traits(getMember, composite, member)) == void)) { 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))(index + 1); } } } } } } } } 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) { 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 780"); } /// 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")); }