1 module introspection.unittest_; 2 3 import introspection.type; 4 import introspection.attribute; 5 import introspection.location; 6 7 version(unittest) { 8 import fluent.asserts; 9 } 10 11 alias TestFunction = void function(); 12 13 /// Structure representing an unittest 14 struct UnitTest { 15 /// 16 TestFunction testFunction; 17 18 /// 19 Type type; 20 21 /// 22 Attribute[] attributes; 23 24 /// 25 Location location; 26 } 27 28 /// 29 UnitTest describeUnitTest(alias T)() { 30 UnitTest test; 31 32 test.testFunction = &T; 33 34 test.type = describeType!(typeof(T)); 35 36 test.attributes = describeAttributeList!(__traits(getAttributes, T)); 37 38 auto location = __traits(getLocation, T); 39 test.location = Location(location[0], location[1], location[2]); 40 41 return test; 42 } 43 44 /// 45 UnitTest[] describeUnitTests(alias T)() { 46 UnitTest[] tests; 47 48 static foreach(test; __traits(getUnitTests, T)) { 49 tests ~= describeUnitTest!test; 50 } 51 52 return tests; 53 } 54 55 /// it should describe this unittest 56 @("some attribute") 57 unittest { 58 auto result = describeUnitTests!(introspection.unittest_); 59 60 result.length.should.equal(1); 61 62 result[0].type.name.should.equal("void()"); 63 64 result[0].attributes.length.should.equal(1); 65 result[0].attributes[0].name.should.equal(`"some attribute"`); 66 67 result[0].location.file.should.equal("source/introspection/unittest_.d"); 68 result[0].location.line.should.be.greaterThan(0); 69 result[0].location.column.should.equal(1); 70 }