Skip to content

Instantly share code, notes, and snippets.

@JesseKPhillips
Created July 1, 2015 00:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JesseKPhillips/df79479cbf6a0e3c6b0d to your computer and use it in GitHub Desktop.
Save JesseKPhillips/df79479cbf6a0e3c6b0d to your computer and use it in GitHub Desktop.
Example test framework
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