1 module unit_threaded.asserts;
2 
3 @safe:
4 
5 /**
6  * Helper to call the standard assert
7  */
8 void assertEqual(T, U)(T t, U u, string file = __FILE__, size_t line = __LINE__) @trusted /* std.conv.to */ {
9     import std.conv: to;
10     assert(t == u, "\n" ~ file ~ ":" ~ line.to!string ~ "\nExp: " ~ u.to!string ~ "\nGot: " ~ t.to!string);
11 }
12 
13 
14 void assertExceptionMsg(E)(lazy E expr, string expected,
15                            in string file = __FILE__,
16                            in size_t line = __LINE__)
17 {
18     import unit_threaded.should: UnitTestException;
19     import std..string: stripLeft, replace, split;
20     import std.path: dirSeparator;
21     import std.algorithm: map, all, endsWith;
22     import std.range: zip;
23     import std.conv: to, text;
24     import core.exception: AssertError;
25 
26     string getExceptionMsg(E)(lazy E expr) {
27         try
28         {
29             expr();
30         }
31         catch(UnitTestException ex)
32         {
33             return ex.toString;
34         }
35         assert(0, "Expression did not throw UnitTestException");
36     }
37 
38 
39     //updating the tests below as line numbers change is tedious.
40     //instead, replace the number there with the actual line number
41     expected = expected.replace(":123", ":" ~ line.to!string).replace("/", dirSeparator);
42     auto msg = getExceptionMsg(expr);
43     auto expLines = expected.split("\n").map!stripLeft;
44     auto msgLines = msg.split("\n").map!stripLeft;
45     if(!zip(msgLines, expLines).all!(a => a[0].endsWith(a[1]))) {
46         throw new AssertError(text("\nExpected Exception:\n", expected, "\nGot Exception:\n", msg), file, line);
47     }
48 }