trial.discovery.testclass 118/133(88%) 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
444
450
464
470
484
490
5018
510
522
530
540
550
564
570
580
590
600
610
620
630
640
654
660
670
680
694
704
710
724
730
7418
750
762
770
780
790
800
810
820
830
840
850
860
870
880
890
900
910
920
935
940
950
960
970
982
990
1000
1010
1020
1030
1040
1050
1062
1070
1080
1090
1100
1110
1120
1132
1142
1150
1162
1172
1180
1192
1202
1210
1222
1230
1244904
1250
1264904
1270
1286
1290
1300
1314904
1320
13314
1340
1350
1364904
1370
1386
1396
1400
1416
1420
1436
14420
1454
1460
1470
1480
1490
1500
1512
1520
1530
1540
1550
1560
15737
1580
1590
1600
1610
1620
1630
1640
1650
1660
1670
1680
1690
1700
1710
1720
1730
1740
1750
1760
1770
1780
1790
1800
18120
18265
1830
18420
1850
1864
1870
1880
1890
1900
1910
1920
1930
1940
1950
1960
1970
1980
1990
2000
2010
2020
2030
2040
20553
2060
2070
2080
2090
2100
2110
2120
2130
21410
21510
2160
21710
2184
2190
2200
2210
2224
2230
22418
2250
2262
2270
2280
2290
2300
2310
2324
2330
23418
2350
2362
2370
2380
2390
2404
2410
2420
24310
2440
24510
2460
2470
2480
2490
2500
2510
2520
2530
2540
2550
2560
2570
2580
2590
2600
2610
2620
2630
26410
2650
2660
2670
2680
2690
2705
2710
2720
2730
27410
2750
2765
2770
2780
2790
2805
2810
2820
2830
2840
2850
2860
2870
2880
2890
2900
2910
2920
29310
2940
2950
2960
2970
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
4682
4690
4700
4710
4720
4730
4740
4750
4760
4770
4780
4792
4800
4810
4820
4830
4840
4852
4860
4870
4880
4890
4900
4912
4920
4930
4940
4950
4960
4972
4980
4990
5000
5010
5020
5030
5042
5050
5060
5070
5080
5090
5100
5110
5121
5131
5140
5151
5160
5172
5182
5192
5200
5212
5222
5230
5240
5250
5260
5270
5281
5290
5301
5310
5322
5332
5342
5350
5362
5372
5380
5390
5400
5410
5420
5430
5440
5451
5460
5470
5481
5491
5500
5512
5521
5530
5541
5550
5562
5570
5580
5590
5600
5610
5620
5630
5641
5650
5660
5671
5681
5690
5703
5711
5720
5731
5740
5752
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
6170
6180
6190
6200
6210
6220
6230
6240
6250
6260
6270
6280
6290
6300
6310
6320
6330
6340
6350
6360
6370
6380
6390
6400
6410
6420
6430
6440
6450
6460
6471
6481
6490
6500
6510
6521
6530
6541
6550
6562
6572
6580
6590
6600
6610
6620
6630
6640
6650
6662
6670
6680
6690
6700
6710
6722
6730
6740
6750
6760
6770
6780
6791
6800
6810
6820
6830
6840
6850
6860
6870
6880
6890
6900
6912
6920
6930
6941
6951
6960
6970
6980
6991
7000
7011
7020
7032
7042
7050
7060
7070
7080
7090
7101
7111
7120
7131
7140
7152
7161
7170
7180
7192
7202
7210
7220
/++ 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; /// 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) 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): 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")); }