1 module more.test; 2 3 import std.stdio : writeln, writefln; 4 import std.format : format; 5 6 __gshared uint passTestCount; 7 __gshared uint failTestCount; 8 9 enum outerBar = "========================================="; 10 enum innerBar = "-----------------------------------------"; 11 void startTest(string name) 12 { 13 version (PrintTestBoundaries) 14 { 15 writeln(outerBar); 16 writeln(name, ": Start"); 17 writeln(innerBar); 18 } 19 } 20 void endFailedTest(string name) 21 { 22 failTestCount++; 23 version (PrintTestBoundaries) 24 { 25 writeln(innerBar); 26 writeln(name, ": Failed"); 27 writeln(outerBar); 28 } 29 } 30 void endPassedTest(string name) 31 { 32 passTestCount++; 33 version (PrintTestBoundaries) 34 { 35 writeln(innerBar); 36 writeln(name, ": Passed"); 37 writeln(outerBar); 38 } 39 } 40 template scopedTest(string name) { 41 enum scopedTest = 42 "startTest(\""~name~"\");"~ 43 "scope(failure) {import std.stdio : stdout; stdout.flush();endFailedTest(\""~name~"\");}"~ 44 "scope(success) endPassedTest(\""~name~"\");"; 45 } 46 void writeSection(string name) 47 { 48 writeln(innerBar); 49 writeln(name); 50 writeln(innerBar); 51 } 52 void assertEqual(string expected, string actual) pure 53 { 54 if(expected != actual) { 55 throw new Exception(format("Expected %s Actual %s", 56 expected ? ('"' ~ expected ~ '"') : "<null>", 57 actual ? ('"' ~ actual ~ '"') : "<null>")); 58 } 59 } 60 void dumpTestResults() 61 { 62 writeSection("Final Results"); 63 writefln("%s test(s) passed", passTestCount); 64 writefln("%s test(s) failed", failTestCount); 65 }