1 |
#define ENABLE_EXTENDED_ERROR_MESSAGES |
2 |
#define ENABLE_SHORT_ERROR_MESSAGES |
3 |
|
4 |
#define CONFIG_UNITTEST |
5 |
|
6 |
using System; |
7 |
using System.Collections.Generic; |
8 |
using System.Diagnostics; |
9 |
using System.Text; |
10 |
|
11 |
namespace libThermoControl |
12 |
{ |
13 |
public interface IUnitTest |
14 |
{ |
15 |
void RunTests(); |
16 |
} |
17 |
public static class UnitTester |
18 |
{ |
19 |
static Stack<UnitTest> tests = new Stack<UnitTest>(); |
20 |
public static void RunTests() |
21 |
{ |
22 |
CreateTests(); |
23 |
while (tests.Count != 0) |
24 |
{ |
25 |
var test = tests.Peek(); |
26 |
if (!test.RunTest()) |
27 |
{ |
28 |
Console.WriteLine("Failed to run unit test: '{0}'", test.Name); |
29 |
} |
30 |
tests.Pop(); |
31 |
} |
32 |
} |
33 |
private static void CreateTests() |
34 |
{ |
35 |
tests = new Stack<UnitTest>(); |
36 |
tests.Push(new ThermistatControlsUnitTest()); |
37 |
tests.Push(new ConfigurationUnitTest()); |
38 |
} |
39 |
|
40 |
#region sub-classes |
41 |
#region Base UnitTest class |
42 |
private abstract class UnitTest |
43 |
{ |
44 |
public bool RunTest() |
45 |
{ |
46 |
try |
47 |
{ |
48 |
Console.WriteLine("Running Unit Test: '{0}'", this.Name); |
49 |
__RunTest(); |
50 |
return true; |
51 |
} |
52 |
catch (Exception ex) |
53 |
{ |
54 |
WriteShortErrorMessage(ex); |
55 |
WriteExtendedErrorMessage(ex); |
56 |
return false; |
57 |
} |
58 |
} |
59 |
protected abstract void __RunTest(); |
60 |
public virtual string Name { get { return this.GetType().Name; } } |
61 |
|
62 |
[Conditional("ENABLE_EXTENDED_ERROR_MESSAGES")] |
63 |
private void WriteExtendedErrorMessage(Exception ex) |
64 |
{ |
65 |
Console.WriteLine("Extended Failure Reason:{0}{1}", System.Environment.NewLine, ex.ToString().Replace(ex.Message, "")); |
66 |
} |
67 |
|
68 |
[Conditional("ENABLE_SHORT_ERROR_MESSAGES")] |
69 |
private void WriteShortErrorMessage(Exception ex) |
70 |
{ |
71 |
Console.WriteLine("Failure Reason:{0}{1}", System.Environment.NewLine, ex.Message); |
72 |
} |
73 |
} |
74 |
#endregion |
75 |
|
76 |
#region unit test class implementations |
77 |
private class ConfigurationUnitTest : UnitTest |
78 |
{ |
79 |
protected override void __RunTest() |
80 |
{ |
81 |
Configuration.UnitTest(); |
82 |
// Verify |
83 |
Console.WriteLine("Configuration.MIN_TEMP: '{0}'", Configuration.MIN_TEMP); |
84 |
Console.WriteLine("Configuration.MAX_TEMP: '{0}'", Configuration.MAX_TEMP); |
85 |
Console.WriteLine("Configuration.DEFAULT_TEMP: '{0}'", Configuration.DEFAULT_TEMP); |
86 |
Console.WriteLine("Configuration.DEFAULT_MODE: '{0}'", Configuration.DEFAULT_MODE); |
87 |
} |
88 |
public override string Name |
89 |
{ |
90 |
get |
91 |
{ |
92 |
return "Configuration Test"; |
93 |
} |
94 |
} |
95 |
} |
96 |
private class ThermistatControlsUnitTest : UnitTest |
97 |
{ |
98 |
protected override void __RunTest() |
99 |
{ |
100 |
|
101 |
Console.Write("Testing GetButtonByValue(int button): (0): "); |
102 |
var b = ThermistatControls.GetButtonByValue(0); |
103 |
Console.WriteLine("'{0}'", b.ToString()); |
104 |
|
105 |
Console.Write("Testing GetButtonByValue(ACControls button): (ACControls.POWER): "); |
106 |
b = ThermistatControls.GetButtonByValue(ACControls.POWER); |
107 |
Console.WriteLine("'{0}'", b.ToString()); |
108 |
|
109 |
Console.Write("Testing GetButtonByName(string button): (\"POWER\"): "); |
110 |
b = ThermistatControls.GetButtonByName("POWER"); |
111 |
Console.WriteLine("'{0}'", b.ToString()); |
112 |
|
113 |
Console.Write("Testing GetButtonByName(string button): (\"power\"): "); |
114 |
b = ThermistatControls.GetButtonByName("power"); |
115 |
Console.WriteLine("'{0}'", b.ToString()); |
116 |
} |
117 |
public override string Name |
118 |
{ |
119 |
get |
120 |
{ |
121 |
return "Thermistat Controls Test"; |
122 |
} |
123 |
} |
124 |
} |
125 |
#endregion |
126 |
|
127 |
#endregion |
128 |
} |
129 |
} |