trial.discovery.testclass 140/162(86%) 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
464
470
484
490
504
510
524
530
5418
550
562
570
580
590
604
610
620
630
640
650
660
674
680
694
700
710
720
734
744
750
764
770
7818
790
802
810
820
830
840
850
860
870
880
890
900
910
920
930
940
950
960
975
980
990
1000
1010
1022
1030
1040
1050
1060
1070
1080
1090
1102
1110
1122
1130
1140
1150
1160
1172
1182
1190
1202
1212
1220
1232
1242
1250
1262
1270
1284754
1290
1304754
1310
13210
1330
1340
1354754
1360
13714
1380
1390
1404754
1410
1426
1436
1440
1456
1460
1476
14820
1494
1500
1510
1520
1530
1540
1552
1560
1570
1580
1590
1600
16137
1620
1630
1640
1650
1660
1670
1680
16937
1700
17154
1720
1730
1740
1750
17653
177883
1780
1790
1800
1810
18220
18320
1840
18520
18665
1870
18820
1890
1904
1910
1920
1930
1940
1950
1960
1970
1980
1990
2000
20154
2020
20354
2040
20554
2060
2070
20853
20953
2100
211883
2120
213883
2140
2150
2160
2170
21810
21910
2200
22110
2224
2230
2244
2250
2264
2270
22818
2290
2302
2310
2320
2330
2340
2350
2364
2370
23818
2390
2402
2410
2420
2430
2444
2450
2460
24710
2480
24910
2500
2510
2520
2530
2540
2550
2560
2570
2580
2590
2600
2610
2620
2630
2640
2650
26610
2670
26810
2690
27015
2710
2720
2730
2745
2750
2760
2770
27810
2790
2805
2810
2820
2830
2845
2850
2860
2870
2880
2890
2900
2910
2920
2930
2940
29510
2960
29710
2980
2990
3000
3010
3020
3030
3040
3050
3060
3070
3080
3090
3100
3110
3120
3130
3140
3150
3160
3170
3180
3190
3200
3210
3220
3230
3240
3250
3260
3270
3280
3290
3300
3310
3320
3330
3340
3350
3360
3370
3380
3390
3400
3410
3420
3430
3440
3450
3460
3470
3480
3490
3500
3510
3520
3530
3540
3550
3560
3570
3580
3590
3600
3610
3620
3630
3640
3650
3660
3670
3680
3690
3700
3710
3720
3730
3740
3750
3760
3770
3780
3790
3800
3810
3820
3830
3840
3850
3860
3870
3880
3890
3900
3910
3920
3930
3940
3950
3960
3970
3980
3990
4000
4010
4020
4030
4040
4050
4060
4070
4080
4090
4100
4110
4120
4130
4140
4150
4160
4170
4180
4190
4200
4210
4220
4230
4240
4250
4260
4270
4280
4290
4300
4310
4320
4330
4340
4350
4360
4370
4380
4390
4400
4410
4420
4430
4440
4450
4460
4470
4480
4490
4500
4510
4520
4530
4540
4550
4560
4570
4580
4590
4600
4610
4620
4630
4640
4650
4660
4670
4680
4690
4700
4710
4722
4730
4740
4750
4760
4770
4780
4790
4800
4810
4820
4832
4840
4850
4860
4870
4880
4892
4900
4910
4920
4930
4940
4952
4960
4970
4980
4990
5000
5012
5020
5030
5040
5050
5060
5070
5082
5090
5100
5110
5120
5130
5140
5150
5161
5171
5180
5191
5200
5212
5222
5232
5240
5252
5262
5270
5280
5290
5300
5310
5321
5330
5341
5350
5362
5372
5382
5390
5402
5412
5420
5430
5440
5450
5460
5470
5480
5491
5500
5510
5521
5531
5540
5552
5561
5570
5581
5590
5602
5610
5620
5630
5640
5650
5660
5670
5681
5690
5700
5711
5721
5730
5743
5751
5760
5771
5780
5792
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
6170
6180
6190
6200
6210
6220
6230
6240
6250
6260
6270
6280
6290
6300
6310
6320
6330
6340
6352
6360
6370
6380
6390
6400
6410
6420
6430
6441
6450
6460
6470
6480
6490
6500
6511
6521
6530
6540
6550
6561
6570
6581
6590
6602
6612
6620
6630
6640
6650
6660
6670
6680
6690
6702
6710
6720
6730
6740
6750
6762
6770
6780
6790
6800
6810
6820
6831
6840
6850
6860
6870
6880
6890
6901
6910
6920
6930
6940
6952
6960
6970
6981
6991
7000
7010
7020
7031
7040
7051
7060
7072
7082
7090
7100
7110
7120
7130
7141
7151
7160
7171
7180
7192
7201
7210
7220
7233
7242
7250
7260
/++ A module containing the discovery logic for classes annodated with @Test 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.testclass; import std.meta; import std.traits; import std.uni; import std.conv; import std.string; import std.algorithm; import std.random; import std.array; import trial.interfaces; public import trial.attributes; import trial.discovery.code; version (Have_fluent_asserts) { version = Have_fluent_asserts_core; } /// A structure that stores data about the setup events(methods) struct SetupEvent { string name; TestSetupAttribute setup; TestCaseFunction func; } private { SetupEvent[][string] setupMethods; Object[string] testClassInstances; size_t[string] testMethodCount; size_t[string] testMethodExecuted; } private void methodDone(string ModuleName, string ClassName)() { enum key = ModuleName ~ "." ~ ClassName; testMethodExecuted[key]++; if (testMethodExecuted[key] >= testMethodCount[key]) { if (key in setupMethods) { foreach (setupMethod; setupMethods[key].filter!(a => a.setup.afterAll)) { setupMethod.func(); } } testClassInstances.remove(key); } } private auto getTestClassInstance(string ModuleName, string ClassName)() { mixin(`import ` ~ ModuleName ~ `;`); enum key = ModuleName ~ "." ~ ClassName; if (key !in testClassInstances) { mixin(`auto instance = new ` ~ ClassName ~ `();`); testClassInstances[key] = instance; testMethodExecuted[key] = 0; if (key in setupMethods) { foreach (setupMethod; setupMethods[key].filter!(a => a.setup.beforeAll)) { setupMethod.func(); } } } mixin(`return cast(` ~ key ~ `) testClassInstances[key];`); } /// The default test discovery looks for unit test sections and groups them by module class TestClassDiscovery : ITestDiscovery { private TestCase[] list; /// Returns all the test cases that were found in the modules /// added with `addModule` TestCase[] getTestCases() { return list; } TestCase[] discoverTestCases(string file) { TestCase[] testCases = []; version (Have_fluent_asserts_core) version (Have_libdparse) { import fluentasserts.core.results; import std.stdio; 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; string lastSuite; DLangAttribute[] attributes; int blockIndex; foreach (token; iterator) { auto type = str(token.type); if (type == "version") { iterator.skipUntilType(")"); } if (type == "unittest") { iterator.skipNextBlock; } if (type == "class") { iterator.skipOne; auto dlangClass = iterator.readClass; lastSuite = moduleName ~ "." ~ dlangClass.name; testCases ~= dlangClass.functions.filter!(a => a.hasAttribute("Test")) .map!(a => TestCase(lastSuite, a.testName, &noTest, [], SourceLocation(file, a.getAttribute("Test").line))) .array; } } } return testCases; } /// Add tests from a certain module void addModule(string file, string moduleName)() { discover!moduleName; } private { void discover(string ModuleName)() { mixin("static import " ~ ModuleName ~ ";"); enum classList = classMembers!(ModuleName); foreach (className; classList) { mixin("alias CurrentClass = " ~ ModuleName ~ "." ~ className ~ ";"); static if(__traits(getProtection, CurrentClass) == "public") { enum members = __traits(allMembers, CurrentClass); foreach (member; members) { static if(__traits(hasMember, CurrentClass, member)) { static if (isSetupMember!(ModuleName, className, member)) { enum setup = getSetup!(ModuleName, className, member); enum key = ModuleName ~ "." ~ className; auto exists = key in setupMethods && !setupMethods[key].filter!(a => a.name == member).empty; if (!exists) { setupMethods[key] ~= SetupEvent(member, setup, ({ mixin(`auto instance = new ` ~ className ~ `();`); mixin(`instance.` ~ member ~ `;`); })); } } } } } } foreach (className; classList) { enum key = ModuleName ~ "." ~ className; mixin("alias CurrentClass = " ~ key ~ ";"); enum protection = __traits(getProtection, CurrentClass); static if(protection == "public") { enum members = __traits(allMembers, CurrentClass); testMethodCount[key] = 0; foreach (member; members) { enum memberProtection = __traits(getProtection, member); static if(memberProtection == "public") { static if (isTestMember!(ModuleName, className, member)) { testMethodCount[key]++; string testName = getTestName!(ModuleName, className, member); auto testCase = TestCase(ModuleName ~ "." ~ className, testName, ({ auto instance = getTestClassInstance!(ModuleName, className); enum key = ModuleName ~ "." ~ className; if (key in setupMethods) { foreach (setupMethod; setupMethods[key].filter!(a => a.setup.beforeEach)) { setupMethod.func(); } } mixin(`instance.` ~ member ~ `;`); if (key in setupMethods) { foreach (setupMethod; setupMethods[key].filter!(a => a.setup.afterEach)) { setupMethod.func(); } } methodDone!(ModuleName, className); }), []); testCase.location = getTestLocation!(ModuleName, className, member); list ~= testCase; } } } } } } } } /// string getTestName(string ModuleName, string className, string member)() { mixin("static import " ~ ModuleName ~ ";"); mixin("enum attributes = __traits(getAttributes, " ~ ModuleName ~ "." ~ className ~ "." ~ member ~ ");"); enum testAttributes = testAttributes!attributes; string name; foreach (attribute; attributes) { static if (is(typeof(attribute) == string)) { name = attribute; } } if (name.length == 0) { return member.camelToSentence; } else { return name; } } /// SourceLocation getTestLocation(string ModuleName, string className, string member)() { mixin("static import " ~ ModuleName ~ ";"); mixin("enum attributes = __traits(getAttributes, " ~ ModuleName ~ "." ~ className ~ "." ~ member ~ ");"); enum testAttributes = testAttributes!attributes; return SourceLocation(testAttributes[0].file, testAttributes[0].line); } /// auto getSetup(string ModuleName, string className, string member)() { mixin("static import " ~ ModuleName ~ ";"); mixin("enum attributes = __traits(getAttributes, " ~ ModuleName ~ "." ~ className ~ "." ~ member ~ ");"); return setupAttributes!attributes[0]; } /// bool isTestMember(string ModuleName, string className, string member)() { mixin("static import " ~ ModuleName ~ ";"); static if(__traits(hasMember, mixin(ModuleName ~ "." ~ className), member)) { static if(__traits(compiles, isCallable!(mixin(ModuleName ~ "." ~ className ~ "." ~ member)))) { static if(isCallable!(mixin(ModuleName ~ "." ~ className ~ "." ~ member))) { static if(__traits(compiles, mixin("__traits(getAttributes, " ~ ModuleName ~ "." ~ className ~ "." ~ member ~ ")"))) { mixin("enum attributes = __traits(getAttributes, " ~ ModuleName ~ "." ~ className ~ "." ~ member ~ ");"); return testAttributes!attributes.length > 0; } else { return false; } } else { return false; } } else { return false; } } else { return false; } } /// bool isSetupMember(string ModuleName, string className, string member)() { mixin("static import " ~ ModuleName ~ ";"); static if(__traits(hasMember, mixin(ModuleName ~ "." ~ className), member)) { static if(__traits(compiles, isCallable!(mixin(ModuleName ~ "." ~ className ~ "." ~ member)))) { static if(isCallable!(mixin(ModuleName ~ "." ~ className ~ "." ~ member))) { static if(__traits(compiles, mixin("__traits(getAttributes, " ~ ModuleName ~ "." ~ className ~ "." ~ member ~ ")"))) { mixin("enum attributes = __traits(getAttributes, " ~ ModuleName ~ "." ~ className ~ "." ~ member ~ ");"); return setupAttributes!attributes.length > 0; } else { return false; } } else { return false; } } else { return false; } } else { return false; } } /// template isClass(string moduleName) { template isModuleClass(string name) { mixin(" static import " ~ moduleName ~ "; static if (is(" ~ moduleName ~ "." ~ name ~ " == class)) enum bool isModuleClass = true; else enum bool isModuleClass = false;"); } alias isClass = isModuleClass; } /// template isTestAttribute(alias Attribute) { import trial.attributes; static if (!is(CommonType!(Attribute, TestAttribute) == void)) { enum bool isTestAttribute = true; } else { enum bool isTestAttribute = false; } } /// template isRightParameter(string parameterName) { template isRightParameter(alias Attribute) { enum isRightParameter = Attribute.parameterName == parameterName; } } /// template isSetupAttribute(alias Attribute) { static if (!is(CommonType!(Attribute, TestSetupAttribute) == void)) { enum bool isSetupAttribute = true; } else { enum bool isSetupAttribute = false; } } /// template isValueProvider(alias Attribute) { static if (__traits(hasMember, Attribute, "provide") && __traits(hasMember, Attribute, "parameterName")) { enum bool isValueProvider = true; } else { enum bool isValueProvider = false; } } /// template extractClasses(string moduleName, members...) { mixin("static import " ~ moduleName ~ ";"); alias Filter!(isClass!moduleName, members) extractClasses; } /// template extractValueProviders(Elements...) { alias Filter!(isValueProvider, Elements) extractValueProviders; } /// template testAttributes(attributes...) { alias Filter!(isTestAttribute, attributes) testAttributes; } /// template setupAttributes(attributes...) { alias Filter!(isSetupAttribute, attributes) setupAttributes; } /// template classMembers(string moduleName) { mixin("static import " ~ moduleName ~ ";"); mixin("alias extractClasses!(moduleName, __traits(allMembers, " ~ moduleName ~ ")) classMembers;"); } version (unittest) { version(Have_fluent_asserts_core): import trial.attributes; import fluent.asserts; class SomeTestSuite { static string lastTest; @Test() void aSimpleTest() { lastTest = "a simple test"; } } class OtherTestSuite { static string[] order; @BeforeEach() void beforeEach() { order ~= "before each"; } @AfterEach() void afterEach() { order ~= "after each"; } @BeforeAll() void beforeAll() { order ~= "before all"; } @AfterAll() void afterAll() { order ~= "after all"; } @Test() @("Some other name") void aCustomTest() { order ~= "a custom test"; } } } /// TestClassDiscovery should find the Test Suite class unittest { auto discovery = new TestClassDiscovery(); discovery.addModule!(`lifecycle/trial/discovery/testclass.d`, `trial.discovery.testclass`); auto testCases = discovery.getTestCases; testCases.length.should.equal(2); testCases[0].suiteName.should.equal(`trial.discovery.testclass.SomeTestSuite`); testCases[0].name.should.equal(`A simple test`); testCases[1].suiteName.should.equal(`trial.discovery.testclass.OtherTestSuite`); testCases[1].name.should.equal(`Some other name`); } /// discoverTestCases should find the Test Suite class unittest { auto testDiscovery = new TestClassDiscovery; auto testCases = testDiscovery.discoverTestCases(__FILE__); testCases.length.should.equal(2); testCases[0].suiteName.should.equal(`trial.discovery.testclass.SomeTestSuite`); testCases[0].name.should.equal(`A simple test`); testCases[1].suiteName.should.equal(`trial.discovery.testclass.OtherTestSuite`); testCases[1].name.should.equal(`Some other name`); } /// TestClassDiscovery should execute tests from a Test Suite class unittest { scope (exit) { SomeTestSuite.lastTest = ""; } auto discovery = new TestClassDiscovery(); discovery.addModule!(`lifecycle/trial/discovery/testclass.d`, `trial.discovery.testclass`); auto test = discovery.getTestCases.filter!(a => a.suiteName == `trial.discovery.testclass.SomeTestSuite`) .filter!(a => a.name == `A simple test`).front; test.func(); SomeTestSuite.lastTest.should.equal("a simple test"); } /// TestClassDiscovery should execute the before and after methods tests from a Test Suite class unittest { scope (exit) { OtherTestSuite.order = []; } auto discovery = new TestClassDiscovery(); discovery.addModule!(`lifecycle/trial/discovery/testclass.d`, `trial.discovery.testclass`); auto test = discovery.getTestCases.filter!(a => a.suiteName == `trial.discovery.testclass.OtherTestSuite`) .filter!(a => a.name == `Some other name`).front; test.func(); OtherTestSuite.order.should.equal(["before all", "before each", "a custom test", "after each", "after all"]); } private string generateRandomParameters(alias T, int index)() pure nothrow { alias paramTypes = std.traits.Parameters!T; enum params = ParameterIdentifierTuple!T; alias providers = Filter!(isRightParameter!(params[index].stringof[1 .. $ - 1]), extractValueProviders!(__traits(getAttributes, T))); enum provider = "Filter!(isRightParameter!(" ~ params[index].stringof ~ "), extractValueProviders!(__traits(getAttributes, T)))"; static if (providers.length > 0) { immutable string definition = "auto param_" ~ params[index] ~ " = " ~ provider ~ "[0]().provide; "; } else { immutable string definition = "auto param_" ~ params[index] ~ " = uniform!" ~ paramTypes[index].stringof ~ "(); "; } static if (index == 0) { return definition; } else { return definition ~ generateRandomParameters!(T, index - 1); } } private string generateMethodParameters(alias T, int size)() { enum params = ParameterIdentifierTuple!T; static if (size == 0) { return ""; } else static if (size == 1) { return "param_" ~ params[0]; } else { return generateMethodParameters!(T, size - 1) ~ ", param_" ~ params[size - 1]; } } /// Call a method using the right data provders void methodCaller(alias T, U)(U func) { enum parameterCount = arity!T; mixin(generateRandomParameters!(T, parameterCount - 1)); mixin("func(" ~ generateMethodParameters!(T, parameterCount) ~ ");"); } /// methodCaller should call the method with random numeric values unittest { class TestClass { static int usedIntValue = 0; static ulong usedUlongValue = 0; void randomMethod(int value, ulong other) { usedIntValue = value; usedUlongValue = other; } } auto instance = new TestClass; methodCaller!(instance.randomMethod)(&instance.randomMethod); TestClass.usedIntValue.should.not.equal(0); TestClass.usedUlongValue.should.not.equal(0); } struct ValueProvider(string name, alias T) { immutable static string parameterName = name; auto provide() { return T(); } } auto For(string name, alias T)() { return ValueProvider!(name, T)(); } version (unittest) { auto someCustomFunction() { return 6; } } /// methodCaller should call the method with custom random generators unittest { class TestClass { static int usedIntValue = 0; static ulong usedUlongValue = 0; @For!("value", { return 5; }) @For!("other", { return someCustomFunction(); }) void randomMethod(int value, ulong other) { usedIntValue = value; usedUlongValue = other; } } auto instance = new TestClass; methodCaller!(instance.randomMethod)(&instance.randomMethod); TestClass.usedIntValue.should.equal(5); TestClass.usedUlongValue.should.equal(6); } /// discoverTestCases should find the same tests like testCases unittest { auto discovery = new TestClassDiscovery; discovery.addModule!(__FILE__, `trial.discovery.testclass`); discovery .discoverTestCases(__FILE__) .map!(a => a.toString) .join("\n") .should.equal( discovery.getTestCases .sort!((a, b) => a.location.line < b.location.line) .map!(a => a.toString) .join("\n")); }