/[ThermoControl]/trunk/libThermoControl/UnitTester.cs
ViewVC logotype

Contents of /trunk/libThermoControl/UnitTester.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 20 - (show annotations) (download)
Wed Nov 13 02:22:43 2013 UTC (7 years, 7 months ago) by william
File size: 5488 byte(s)

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 List<UnitTest> list = new List<UnitTest>();
37 list.Add(new ThermistatControlsUnitTest());
38 list.Add(new ConfigurationUnitTest());
39 list.Add(new ControllerUnitTest());
40 list.Reverse();
41 list.ForEach(p => tests.Push(p));
42 list.Clear();
43 list = null;
44 }
45
46 #region sub-classes
47 #region Base UnitTest class
48 private abstract class UnitTest
49 {
50 public bool RunTest()
51 {
52 try
53 {
54 Console.WriteLine("Running Unit Test: '{0}'", this.Name);
55 __RunTest();
56 return true;
57 }
58 catch (Exception ex)
59 {
60 WriteShortErrorMessage(ex);
61 WriteExtendedErrorMessage(ex);
62 return false;
63 }
64 }
65 protected abstract void __RunTest();
66 public virtual string Name { get { return this.GetType().Name; } }
67
68 [Conditional("ENABLE_EXTENDED_ERROR_MESSAGES")]
69 private void WriteExtendedErrorMessage(Exception ex)
70 {
71 Console.WriteLine("Extended Failure Reason:{0}{1}", System.Environment.NewLine, ex.ToString().Replace(ex.Message, ""));
72 }
73
74 [Conditional("ENABLE_SHORT_ERROR_MESSAGES")]
75 private void WriteShortErrorMessage(Exception ex)
76 {
77 Console.WriteLine("Failure Reason:{0}{1}", System.Environment.NewLine, ex.Message);
78 }
79 }
80 #endregion
81
82 #region unit test class implementations
83 private class ControllerUnitTest : UnitTest
84 {
85 protected override void __RunTest()
86 {
87 Controller controller = new Controller();
88 controller.SetTemperature(65);
89 controller.SetTemperature(71);
90 controller.SetDefaultTemperature();
91 }
92 public override string Name
93 {
94 get
95 {
96 return "Controller Test";
97 }
98 }
99 }
100 private class ConfigurationUnitTest : UnitTest
101 {
102 protected override void __RunTest()
103 {
104 Configuration.UnitTest();
105 // Verify
106 Console.WriteLine("Configuration.THERMISTAT_DEVICE: '{0}'", Configuration.THERMISTAT_DEVICE);
107 Console.WriteLine("Configuration.MIN_TEMP: '{0}'", Configuration.MIN_TEMP);
108 Console.WriteLine("Configuration.MAX_TEMP: '{0}'", Configuration.MAX_TEMP);
109 Console.WriteLine("Configuration.DEFAULT_TEMP: '{0}'", Configuration.DEFAULT_TEMP);
110 Console.WriteLine("Configuration.DEFAULT_MODE: '{0}'", Configuration.DEFAULT_MODE);
111 Console.WriteLine("Configuration.DEFAULT_FAN_SPEED: '{0}'", Configuration.DEFAULT_FAN_SPEED);
112 }
113 public override string Name
114 {
115 get
116 {
117 return "Configuration Test";
118 }
119 }
120 }
121 private class ThermistatControlsUnitTest : UnitTest
122 {
123 protected override void __RunTest()
124 {
125
126 Console.Write("Testing GetButtonByValue(int button): (0): ");
127 var b = ThermistatControls.GetButtonByValue(0);
128 Console.WriteLine("'{0}'", b.ToString());
129
130 Console.Write("Testing GetButtonByValue(ACControls button): (ACControls.POWER): ");
131 b = ThermistatControls.GetButtonByValue(ACControls.POWER);
132 Console.WriteLine("'{0}'", b.ToString());
133
134 Console.Write("Testing GetButtonByName(string button): (\"POWER\"): ");
135 b = ThermistatControls.GetButtonByName("POWER");
136 Console.WriteLine("'{0}'", b.ToString());
137
138 Console.Write("Testing GetButtonByName(string button): (\"power\"): ");
139 b = ThermistatControls.GetButtonByName("power");
140 Console.WriteLine("'{0}'", b.ToString());
141 }
142 public override string Name
143 {
144 get
145 {
146 return "Thermistat Controls Test";
147 }
148 }
149 }
150 #endregion
151
152 #endregion
153 }
154 }

  ViewVC Help
Powered by ViewVC 1.1.22