10203040506070809010011012013014015016017018019020021022023024025026027028029030031032033034035036037038039040041042043044045046047048049050051052053054055056057058059060061062063064065066067068069070071072073074075076077078079080081082083084085086087088089090091092093094095096097098099010001010102010301040105010601070108010930251100111308112011301140115011601170118011901200121012201230124012501260127012801290130013101320133013401350136013701380139014001410142014301440145014601470148014901500151015201530154015501560157015801590160016101620163016401650166016701680169017001710172017301740175017601773821780179382180018101820183018401850186018701880189019001910192019301940195019601970198019902000201020238220317622042834205107220634520702083822093452100211021237213021402150216021702180219022002210222022329224022502260227022802290230023102320233023402350236023702380239024002410242024302440245024602470248024902500251025202530254025502560257025802590260026102620263026402650266026702680269027002710272027302740275027602770278027902800281028202830284028502860287028802890290029102920293029402950296029702980299030003010302030303040305030603070308030903100311031203130314031503160317031803190320032103220323032403250326032703281913291913300331033203330334033537336033703380339034019134103420343034419134503460347345348034903500351154352154353035403550356035703580359036038136113620363036419136503660367036803691913700371037219137303740375037619137703780379038003810382038319138403850386038703881913890390039103920393039403950396039703981913990400040104020403040404052540604071904080409041004110412041319141419141504160417191418041919142004210422042304240425042604272154280429043004312154320433043404350436043704380439044004410442172443044417244504460447044804490450045104520453045404550456045704580459046004610462046304640465046604670468046904700471047204730474047504760477047804790480048104820483048404850486048704880489049004910492049304940495049604970498049905000501050205030504050505060507050805090510051105120513051405150516051705180519052005210522052305240525052605270528052905300531053205330534053505360537053805390540054105420543054405450546054705480549055005510 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; } 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; } 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; } string clearCommentTokens(string text) { return text.strip('/').strip('+').strip('*').strip; } 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; } 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; } }
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; } 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; } 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; } string clearCommentTokens(string text) { return text.strip('/').strip('+').strip('*').strip; } 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; } 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; } }