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

Contents of /trunk/libThermoControl/UnitTester.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 27 - (show annotations) (download)
Wed Nov 13 04:19:12 2013 UTC (6 years, 10 months ago) by william
File size: 5826 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 //int e_temp = controller.ExternalTemp;
92
93 int temp = controller.GetTemperatureAdjustment();
94 controller.SetTemperature(temp);
95 controller.SetToDefaultTemperature();
96 }
97 public override string Name
98 {
99 get
100 {
101 return "Controller Test";
102 }
103 }
104 }
105 private class ConfigurationUnitTest : UnitTest
106 {
107 protected override void __RunTest()
108 {
109 Configuration.UnitTest();
110 // Verify
111 Console.WriteLine("Configuration.WEATHER_URL: '{0}'", Configuration.WEATHER_URL);
112 Console.WriteLine("Configuration.THERMISTAT_DEVICE: '{0}'", Configuration.THERMISTAT_DEVICE);
113 Console.WriteLine("Configuration.MIN_TEMP: '{0}'", Configuration.MIN_TEMP);
114 Console.WriteLine("Configuration.MAX_TEMP: '{0}'", Configuration.MAX_TEMP);
115 Console.WriteLine("Configuration.DEFAULT_TEMP: '{0}'", Configuration.DEFAULT_TEMP);
116 Console.WriteLine("Configuration.DEFAULT_MODE: '{0}'", Configuration.DEFAULT_MODE);
117 Console.WriteLine("Configuration.DEFAULT_FAN_SPEED: '{0}'", Configuration.DEFAULT_FAN_SPEED);
118 }
119 public override string Name
120 {
121 get
122 {
123 return "Configuration Test";
124 }
125 }
126 }
127 private class ThermistatControlsUnitTest : UnitTest
128 {
129 protected override void __RunTest()
130 {
131
132 Console.Write("Testing GetButtonByValue(int button): (0): ");
133 var b = ThermistatControls.GetButtonByValue(0);
134 Console.WriteLine("'{0}'", b.ToString());
135
136 Console.Write("Testing GetButtonByValue(ACControls button): (ACControls.POWER): ");
137 b = ThermistatControls.GetButtonByValue(ACControls.POWER);
138 Console.WriteLine("'{0}'", b.ToString());
139
140 Console.Write("Testing GetButtonByName(string button): (\"POWER\"): ");
141 b = ThermistatControls.GetButtonByName("POWER");
142 Console.WriteLine("'{0}'", b.ToString());
143
144 Console.Write("Testing GetButtonByName(string button): (\"power\"): ");
145 b = ThermistatControls.GetButtonByName("power");
146 Console.WriteLine("'{0}'", b.ToString());
147 }
148 public override string Name
149 {
150 get
151 {
152 return "Thermistat Controls Test";
153 }
154 }
155 }
156 #endregion
157
158 #endregion
159 }
160 }

  ViewVC Help
Powered by ViewVC 1.1.22