1 /**
2  * Module to replace the built-in unit tester.
3  *
4  * License:
5  *     MIT. See LICENSE for full details.
6  */
7 module dunit.moduleunittester;
8 
9 /**
10  * Imports.
11  */
12 import core.exception;
13 import core.runtime;
14 import dunit.error;
15 import dunit.output.console;
16 import dunit.result.moduleresult;
17 import dunit.result.moduleresultcollection;
18 
19 /**
20  * Replace the standard unit test handler.
21  */
22 version(unittest) shared static this()
23 {
24 	Runtime.moduleUnitTester = function()
25 	{
26 		auto console = new Console();
27 		auto results = new ModuleResultCollection();
28 
29 		console.writeHeader();
30 
31 		foreach (module_; ModuleInfo)
32 		{
33 			if (module_)
34 			{
35 				auto unitTest = module_.unitTest;
36 
37 				if (unitTest)
38 				{
39 					try
40 					{
41 						unitTest();
42 					}
43 					catch (DUnitAssertError ex)
44 					{
45 						results.add(new ModuleResult(module_.name, ex));
46 						continue;
47 					}
48 					catch (AssertError ex)
49 					{
50 						results.add(new ModuleResult(module_.name, new DUnitAssertError(ex.msg, ex.file, ex.line)));
51 						continue;
52 					}
53 					results.add(new ModuleResult(module_.name));
54 				}
55 			}
56 		}
57 
58 		console.writeReport(results);
59 
60 		return !results.failedCount();
61 	};
62 }