2
2
* @file Assert.cs
3
3
*
4
4
* Static assertion methods for verifying test expectations.
5
- * Each test throws a TestException if it fails.
5
+ * NOTE: It is important that each test throw its own TestException if it fails,
6
+ * as this gives us more accurate call stack reporting.
7
+ *
6
8
*/
7
9
8
10
using System ;
@@ -26,7 +28,7 @@ public static Exception ExpectedException
26
28
/**
27
29
* Set the exception that is expected to be triggered by a unit test case.
28
30
*
29
- * @param Exception ex, the expection to expect.
31
+ * @param ex, the expection to expect.
30
32
*/
31
33
public static void ExpectException ( Exception ex )
32
34
{
@@ -36,18 +38,23 @@ public static void ExpectException(Exception ex)
36
38
/**
37
39
* Throw an exception if the boolean expression is not true.
38
40
*
39
- * @param bool boolean, the expression to test.
41
+ * @param boolean, the expression to test.
40
42
*/
41
43
public static void True ( bool boolean )
42
44
{
43
- True ( boolean , "Expected True, got False." ) ;
45
+ // If not true
46
+ if ( true != boolean )
47
+ {
48
+ // Test failed
49
+ throw new TestException ( "Expected True, got False." ) ;
50
+ }
44
51
}
45
52
46
53
/**
47
54
* Throw an exception if the boolean expression is not true.
48
55
*
49
- * @param bool boolean, boolean expression to evaluate.
50
- * @param string msg, error message to display if test fails.
56
+ * @param boolean, boolean expression to evaluate.
57
+ * @param msg, error message to display if test fails.
51
58
*/
52
59
public static void True ( bool boolean , string msg )
53
60
{
@@ -62,18 +69,23 @@ public static void True(bool boolean, string msg)
62
69
/**
63
70
* Throw an exception if the boolean expression is not false.
64
71
*
65
- * @param bool boolean, the expression to test.
72
+ * @param boolean, the expression to test.
66
73
*/
67
74
public static void False ( bool boolean )
68
75
{
69
- False ( boolean , "Expected False, got True." ) ;
76
+ // If not false
77
+ if ( false != boolean )
78
+ {
79
+ // Test failed
80
+ throw new TestException ( "Expected False, got True." ) ;
81
+ }
70
82
}
71
83
72
84
/**
73
85
* Throw an exception if the boolean expression is not false.
74
86
*
75
- * @param bool boolean, boolean expression to evaluate.
76
- * @param string msg, error message to display if test fails.
87
+ * @param boolean, boolean expression to evaluate.
88
+ * @param msg, error message to display if test fails.
77
89
*/
78
90
public static void False ( bool boolean , string msg )
79
91
{
@@ -88,18 +100,23 @@ public static void False(bool boolean, string msg)
88
100
/**
89
101
* Throw an exception if the object is not null.
90
102
*
91
- * @param Object obj, the object to test.
103
+ * @param obj, the object to test.
92
104
*/
93
105
public static void Null ( Object obj )
94
106
{
95
- Null ( obj , "Expected Null object, got " + obj ) ;
107
+ // If not null
108
+ if ( null != obj )
109
+ {
110
+ // Test failed
111
+ throw new TestException ( "Expected Null object, got " + obj ) ;
112
+ }
96
113
}
97
114
98
115
/**
99
116
* Throw an exception if the object is not null.
100
117
*
101
- * @param Object obj, the object to test.
102
- * @param string msg, error message to display if test fails.
118
+ * @param obj, the object to test.
119
+ * @param msg, error message to display if test fails.
103
120
*/
104
121
public static void Null ( Object obj , string msg )
105
122
{
@@ -114,18 +131,23 @@ public static void Null(Object obj, string msg)
114
131
/**
115
132
* Throw an exception if the object is null.
116
133
*
117
- * @param Object obj, the object to test.
134
+ * @param obj, the object to test.
118
135
*/
119
136
public static void NotNull ( Object obj )
120
137
{
121
- NotNull ( obj , "The object is null." ) ;
138
+ // If null
139
+ if ( null == obj )
140
+ {
141
+ // Test failed
142
+ throw new TestException ( "The object is null." ) ;
143
+ }
122
144
}
123
145
124
146
/**
125
147
* Throw an exception if the object is null.
126
148
*
127
- * @param Object obj, the object to test.
128
- * @param string msg, error message to display if test fails.
149
+ * @param obj, the object to test.
150
+ * @param msg, error message to display if test fails.
129
151
*/
130
152
public static void NotNull ( Object obj , string msg )
131
153
{
@@ -140,8 +162,8 @@ public static void NotNull(Object obj, string msg)
140
162
/**
141
163
* Throw an exception if the two integers are not not equal.
142
164
*
143
- * @param int wanted, the value we expected.
144
- * @param int got, the value we actually received.
165
+ * @param wanted, the value we expected.
166
+ * @param got, the value we actually received.
145
167
*/
146
168
public static void Equal ( int wanted , int got )
147
169
{
@@ -156,9 +178,9 @@ public static void Equal(int wanted, int got)
156
178
/**
157
179
* Throw an exception if the two integers are not not equal.
158
180
*
159
- * @param int wanted, the value we expected.
160
- * @param int got, the value we actually received.
161
- * @param string msg, error message to display if test fails.
181
+ * @param wanted, the value we expected.
182
+ * @param got, the value we actually received.
183
+ * @param msg, error message to display if test fails.
162
184
*/
163
185
public static void Equal ( int wanted , int got , string msg )
164
186
{
@@ -179,8 +201,8 @@ public static void Equal(int wanted, int got, string msg)
179
201
* http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
180
202
* http://realtimecollisiondetection.net/blog/?p=89
181
203
*
182
- * @param float wanted, the value we expected.
183
- * @param float got, the value we actually received.
204
+ * @param wanted, the value we expected.
205
+ * @param got, the value we actually received.
184
206
*/
185
207
public static void Equal ( float wanted , float got )
186
208
{
@@ -201,9 +223,9 @@ public static void Equal(float wanted, float got)
201
223
* http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
202
224
* http://realtimecollisiondetection.net/blog/?p=89
203
225
*
204
- * @param float wanted, the value we expected.
205
- * @param float got, the value we actually received.
206
- * @param string msg, error message to display if test fails.
226
+ * @param wanted, the value we expected.
227
+ * @param got, the value we actually received.
228
+ * @param msg, error message to display if test fails.
207
229
*/
208
230
public static void Equal ( float wanted , float got , string msg )
209
231
{
@@ -218,8 +240,8 @@ public static void Equal(float wanted, float got, string msg)
218
240
/**
219
241
* Throw an exception if the two strings are not not equal.
220
242
*
221
- * @param string wanted, the value we expected.
222
- * @param string got, the value we actually received.
243
+ * @param wanted, the value we expected.
244
+ * @param got, the value we actually received.
223
245
*/
224
246
public static void Equal ( string wanted , string got )
225
247
{
@@ -234,9 +256,9 @@ public static void Equal(string wanted, string got)
234
256
/**
235
257
* Throw an exception if the two strings are not not equal.
236
258
*
237
- * @param string wanted, the value we expected.
238
- * @param string got, the value we actually received.
239
- * @param string msg, error message to display if test fails.
259
+ * @param wanted, the value we expected.
260
+ * @param got, the value we actually received.
261
+ * @param msg, error message to display if test fails.
240
262
*/
241
263
public static void Equal ( string wanted , string got , string msg )
242
264
{
@@ -251,8 +273,8 @@ public static void Equal(string wanted, string got, string msg)
251
273
/**
252
274
* Throw an exception if the two bools are not not equal.
253
275
*
254
- * @param bool wanted, the value we expected.
255
- * @param bool got, the value we actually received.
276
+ * @param wanted, the value we expected.
277
+ * @param got, the value we actually received.
256
278
*/
257
279
public static void Equal ( bool wanted , bool got )
258
280
{
@@ -267,9 +289,9 @@ public static void Equal(bool wanted, bool got)
267
289
/**
268
290
* Throw an exception if the two bools are not not equal.
269
291
*
270
- * @param bool wanted, the value we expected.
271
- * @param bool got, the value we actually received.
272
- * @param string msg, error message to display if test fails.
292
+ * @param wanted, the value we expected.
293
+ * @param got, the value we actually received.
294
+ * @param msg, error message to display if test fails.
273
295
*/
274
296
public static void Equal ( bool wanted , bool got , string msg )
275
297
{
@@ -285,33 +307,33 @@ public static void Equal(bool wanted, bool got, string msg)
285
307
* Throw an exception if the exceptions do not match.
286
308
* NOTE: We only test the type and message of the exceptions.
287
309
*
288
- * @param Exception wanted, the exception we expected.
289
- * @param Exception got, the exception we actually caught.
310
+ * @param wanted, the exception we expected.
311
+ * @param got, the exception we actually caught.
290
312
*/
291
313
public static void Equal ( Exception wanted , Exception got )
292
314
{
293
- Equal ( wanted , got , "Exceptions do not match.\n \t Expected " + wanted + ",\n \t Got " + got ) ;
315
+ // If types or messages not equal
316
+ if ( ( wanted . GetType ( ) != got . GetType ( ) ) ||
317
+ ( wanted . Message != got . Message ) )
318
+ {
319
+ // Test failed
320
+ throw new TestException ( "Exceptions do not match.\n \t Expected " + wanted + ",\n \t Got " + got ) ;
321
+ }
294
322
}
295
323
296
324
/**
297
325
* Throw an exception if the exceptions do not match.
298
326
* NOTE: We only test the type and message of the exceptions.
299
327
*
300
- * @param Exception wanted, the exception we expected.
301
- * @param Exception got, the exception we actually caught.
302
- * @param string msg, error message to display if test fails.
328
+ * @param wanted, the exception we expected.
329
+ * @param got, the exception we actually caught.
330
+ * @param msg, error message to display if test fails.
303
331
*/
304
332
public static void Equal ( Exception wanted , Exception got , string msg )
305
333
{
306
- // If types not equal
307
- if ( wanted . GetType ( ) != got . GetType ( ) )
308
- {
309
- // Test failed
310
- throw new TestException ( msg ) ;
311
- }
312
-
313
- // If the messages do not match
314
- if ( wanted . Message != got . Message )
334
+ // If types or messages not equal
335
+ if ( ( wanted . GetType ( ) != got . GetType ( ) ) ||
336
+ ( wanted . Message != got . Message ) )
315
337
{
316
338
// Test failed
317
339
throw new TestException ( msg ) ;
@@ -321,8 +343,8 @@ public static void Equal(Exception wanted, Exception got, string msg)
321
343
/**
322
344
* Throw an exception if the two objects are not not equal.
323
345
*
324
- * @param object wanted, the value we expected.
325
- * @param object got, the value we actually received.
346
+ * @param wanted, the value we expected.
347
+ * @param got, the value we actually received.
326
348
*/
327
349
public static void Equal ( Object wanted , Object got )
328
350
{
@@ -337,9 +359,9 @@ public static void Equal(Object wanted, Object got)
337
359
/**
338
360
* Throw an exception if the two objects are not not equal.
339
361
*
340
- * @param object wanted, the value we expected.
341
- * @param object got, the value we actually received.
342
- * @param string msg, error message to display if test fails.
362
+ * @param wanted, the value we expected.
363
+ * @param got, the value we actually received.
364
+ * @param msg, error message to display if test fails.
343
365
*/
344
366
public static void Equal ( Object wanted , Object got , string msg )
345
367
{
0 commit comments