1 |
//#define ENABLE_EXTENDED_ERROR_MESSAGES |
2 |
#define ENABLE_SHORT_ERROR_MESSAGES |
3 |
using System; |
4 |
using System.Collections.Generic; |
5 |
using System.Diagnostics; |
6 |
using System.Text; |
7 |
|
8 |
namespace libThermoControl |
9 |
{ |
10 |
public interface IUnitTest |
11 |
{ |
12 |
void RunTests(); |
13 |
} |
14 |
public static class UnitTester |
15 |
{ |
16 |
static Stack<UnitTest> tests = new Stack<UnitTest>(); |
17 |
public static void RunTests() |
18 |
{ |
19 |
CreateTests(); |
20 |
while (tests.Count != 0) |
21 |
{ |
22 |
var test = tests.Peek(); |
23 |
if (!test.RunTest()) |
24 |
{ |
25 |
Console.WriteLine("Failed to run unit test: '{0}'", test.Name); |
26 |
} |
27 |
tests.Pop(); |
28 |
} |
29 |
} |
30 |
private static void CreateTests() |
31 |
{ |
32 |
tests = new Stack<UnitTest>(); |
33 |
tests.Push(new DefaultTest()); |
34 |
} |
35 |
|
36 |
#region sub-classes |
37 |
#region Base UnitTest class |
38 |
private abstract class UnitTest |
39 |
{ |
40 |
public bool RunTest() |
41 |
{ |
42 |
try |
43 |
{ |
44 |
Console.WriteLine("Running Unit Test: '{0}'", this.Name); |
45 |
__RunTest(); |
46 |
return true; |
47 |
} |
48 |
catch (Exception ex) |
49 |
{ |
50 |
WriteShortErrorMessage(ex); |
51 |
WriteExtendedErrorMessage(ex); |
52 |
return false; |
53 |
} |
54 |
} |
55 |
protected abstract void __RunTest(); |
56 |
public virtual string Name { get { return this.GetType().Name; } } |
57 |
|
58 |
[Conditional("ENABLE_EXTENDED_ERROR_MESSAGES")] |
59 |
private void WriteExtendedErrorMessage(Exception ex) |
60 |
{ |
61 |
Console.WriteLine("Extended Failure Reason:{0}{1}", System.Environment.NewLine, ex.ToString().Replace(ex.Message, "")); |
62 |
} |
63 |
|
64 |
[Conditional("ENABLE_SHORT_ERROR_MESSAGES")] |
65 |
private void WriteShortErrorMessage(Exception ex) |
66 |
{ |
67 |
Console.WriteLine("Failure Reason:{0}{1}", System.Environment.NewLine, ex.Message); |
68 |
} |
69 |
} |
70 |
#endregion |
71 |
|
72 |
#region unit test class implementations |
73 |
private class DefaultTest : UnitTest |
74 |
{ |
75 |
protected override void __RunTest() |
76 |
{ |
77 |
throw new NotImplementedException(); |
78 |
} |
79 |
public override string Name |
80 |
{ |
81 |
get |
82 |
{ |
83 |
return "Default Unit Test"; |
84 |
} |
85 |
} |
86 |
} |
87 |
#endregion |
88 |
|
89 |
#endregion |
90 |
} |
91 |
} |