1 /**
2  * Module for the module result collection.
3  *
4  * License:
5  *     MIT. See LICENSE for full details.
6  */
7 module dunit.result.moduleresultcollection;
8 
9 /**
10  * Imports.
11  */
12 import dunit.result.moduleresult;
13 import std.algorithm;
14 import std.range;
15 
16 /**
17  * A class to hold module results.
18  */
19 class ModuleResultCollection
20 {
21 	/**
22 	 * Collection of module results.
23 	 */
24 	private ModuleResult[] _results;
25 
26 	/**
27 	 * Indicate if the collection is empty.
28 	 *
29 	 * Returns:
30 	 *     true if the collection is empty, false if not.
31 	 */
32 	public bool empty()
33 	{
34 		return !this._results.length;
35 	}
36 
37 	/**
38 	 * The total number of tests in the collection.
39 	 *
40 	 * Returns:
41 	 *     the number of tests that dunit has run.
42 	 */
43 	public size_t totalCount()
44 	{
45 		return this._results.length;
46 	}
47 
48 	/**
49 	 * The amount of tests that contain a DUnitAssertError.
50 	 *
51 	 * Returns:
52 	 *     the number of tests that have failed.
53 	 */
54 	public size_t failedCount()
55 	{
56 		return this._results.count!(result => result.error !is null);
57 	}
58 
59 	/**
60 	 * The amount of tests that don't contain a DUnitAssertError.
61 	 *
62 	 * Returns:
63 	 *     the number of tests that have passed.
64 	 */
65 	public size_t passedCount()
66 	{
67 		return this._results.count!(result => result.error is null);
68 	}
69 
70 	/**
71 	 * Add a result to the collection.
72 	 *
73 	 * This method also sorts the collection by source and makes sure all results containing errors are at the end.
74 	 * This enables the console output to be more user friendly.
75 	 *
76 	 * Params:
77 	 *     result = The module result to add.
78 	 */
79 	public void add(ModuleResult result)
80 	{
81 		this._results ~= result;
82 		this._results.multiSort!("a.error is null && b.error !is null", "a.source < b.source")();
83 	}
84 
85 	/**
86 	 * Overload slicing.
87 	 *
88 	 * Returns:
89 	 *     The internal collection of module results.
90 	 */
91 	public ModuleResult[] opSlice()
92 	{
93 		return this._results;
94 	}
95 
96 	/**
97 	 * Overload indexing.
98 	 *
99 	 * Params:
100 	 *     index = The index of the collection.
101 	 *
102 	 * Returns:
103 	 *     The module result residing at the passed index.
104 	 */
105 	public ModuleResult opIndex(size_t index)
106 	{
107 		return this._results[index];
108 	}
109 }
110 
111 unittest
112 {
113 	import dunit.error;
114 	import dunit.toolkit;
115 
116 	auto results = new ModuleResultCollection();
117 	results.empty().assertTrue();
118 
119 	results.add(new ModuleResult("Module1"));
120 	results.totalCount().assertEqual(1);
121 	results.failedCount().assertEqual(0);
122 	results.passedCount().assertEqual(1);
123 
124 	results.add(new ModuleResult("Module2", new DUnitAssertError("Message", "file.d", 1)));
125 	results.totalCount().assertEqual(2);
126 	results.failedCount().assertEqual(1);
127 	results.passedCount().assertEqual(1);
128 
129 	results.empty().assertFalse();
130 	results[].assertCount(2);
131 
132 	results[0].source.assertEqual("Module1");
133 	results[0].error.assertNull();
134 	results[1].source.assertEqual("Module2");
135 	results[1].error.assertType!(DUnitAssertError)();
136 }