Created
July 1, 2015 00:25
-
-
Save JesseKPhillips/df79479cbf6a0e3c6b0d to your computer and use it in GitHub Desktop.
Example test framework
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
auto test(T)(T lhs) { | |
auto t = new Test!T; | |
t.lhs = lhs; | |
import std.typecons : Tuple; | |
return Tuple!(Test!T , "lhs", Test!T, "verify")(t, t); | |
} | |
class Test(T) { | |
T lhs; | |
string testError; | |
string testFile; | |
size_t testLine; | |
bool opEquals(T)(auto ref const T rhs, in string file = __FILE__, in size_t line = __LINE__) { | |
import std.string : format; | |
testError = format("lhs = %s; rhs = %s;", lhs.m, rhs.m); | |
testFile = file; | |
testLine = line; | |
return lhs == rhs; | |
} | |
auto opCall(bool d) { | |
if(!d) | |
throw new Exception(testError, testFile, testLine); | |
} | |
} | |
unittest { | |
struct A { | |
int m; | |
} | |
A a; | |
A b; | |
a.m = 5; | |
b.m = 5; | |
auto testA = a.test; | |
with(testA) { | |
verify(testA.lhs == b); | |
b.m = 6; | |
verify(testA.lhs != b); | |
verify(testA.lhs == b); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment