1 module blerp.blerp;
2 
3 /**
4  * Imports.
5  */
6 import core.exception;
7 import core.runtime;
8 
9 import blerp.results;
10 import blerp.console;
11 
12 import std.stdio;
13 import std.conv: to;
14 import std.format : format;
15 import std.traits : getUDAs;
16 
17 /**
18  * Replace the standard unit test handler.
19  */
20 //version (unittest) shared static this()
21 
22 template Tuple(T...)
23 {
24     alias Tuple = T;
25 }
26 
27 version (unittest) shared static this()
28 {
29     Runtime.moduleUnitTester = { return true; };
30 }
31 
32 struct BlerpTest
33 {
34     string name;
35 }
36 
37 version (unittest) template runTests(string module_name) //if( __traits(isModule, mixin(packageName(mixin(module_name)"."~Nmodule_name)))
38 {
39     bool runTests()
40     {
41         auto console = new Console();
42         auto results = new Results();
43 
44         console.writeHeader(module_name);
45 
46         mixin("static import " ~ module_name ~ ";");
47 
48         alias tests = Tuple!(__traits(getUnitTests, mixin(module_name)));
49 
50         foreach (test; tests)
51         {
52             alias test_attribute = getUDAs!(test, BlerpTest);
53             string test_name;
54 
55             static if (test_attribute.length >= 1)
56             {
57                 test_name = format("%s.%s", module_name, test_attribute[0].name);
58             }
59             else
60             {
61                 test_name = format("%s", module_name);
62             }
63 
64             //If this is not a BlerpTest marked test, ignore it.
65             if (test_attribute.length >= 1)
66             {
67                 try
68                 {
69                     test();
70                 }
71                 catch (AssertError ex)
72                 {
73                     results.add(new Result(test_name, exceptionInfo(true, ex.msg, ex.file, ex.line, to!string(ex.info))));
74                     writeln(format("FAILED    %s", test_name));
75                     continue;
76                 }
77                 results.add(new Result(test_name));
78                 writeln(format("PASSED    %s", test_name));
79 
80             }
81             else
82             {
83                 writeln(format("          Ignoring test %s, it is not marked as a BlerpTest",
84                         test_name));
85             }
86         }
87 
88         console.writeReport(results);
89 
90         return !results.failedCount;
91     }
92 }