Mercurial > hg > audiodb
comparison bindings/as3/ext/asunit/framework/Assert.as @ 732:3a0b9700b3d2
* Initial AS3 commit
author | mas01mj |
---|---|
date | Tue, 14 Sep 2010 16:47:10 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
731:65134dd772fc | 732:3a0b9700b3d2 |
---|---|
1 package asunit.framework { | |
2 import asunit.errors.AssertionFailedError; | |
3 | |
4 import flash.utils.getQualifiedClassName; | |
5 | |
6 import flash.errors.IllegalOperationError; | |
7 import flash.events.EventDispatcher; | |
8 | |
9 /** | |
10 * A set of assert methods. Messages are only displayed when an assert fails. | |
11 */ | |
12 | |
13 public class Assert extends EventDispatcher { | |
14 /** | |
15 * Protect constructor since it is a static only class | |
16 */ | |
17 public function Assert() { | |
18 } | |
19 | |
20 /** | |
21 * Asserts that a condition is true. If it isn't it throws | |
22 * an AssertionFailedError with the given message. | |
23 */ | |
24 static public function assertTrue(...args:Array):void { | |
25 var message:String; | |
26 var condition:Boolean; | |
27 | |
28 if(args.length == 1) { | |
29 message = ""; | |
30 condition = Boolean(args[0]); | |
31 } | |
32 else if(args.length == 2) { | |
33 message = args[0]; | |
34 condition = Boolean(args[1]); | |
35 } | |
36 else { | |
37 throw new IllegalOperationError("Invalid argument count"); | |
38 } | |
39 | |
40 if(!condition) { | |
41 fail(message); | |
42 } | |
43 } | |
44 /** | |
45 * Asserts that a condition is false. If it isn't it throws | |
46 * an AssertionFailedError with the given message. | |
47 */ | |
48 static public function assertFalse(...args:Array):void { | |
49 var message:String; | |
50 var condition:Boolean; | |
51 | |
52 if(args.length == 1) { | |
53 message = ""; | |
54 condition = Boolean(args[0]); | |
55 } | |
56 else if(args.length == 2) { | |
57 message = args[0]; | |
58 condition = Boolean(args[1]); | |
59 } | |
60 else { | |
61 throw new IllegalOperationError("Invalid argument count"); | |
62 } | |
63 | |
64 assertTrue(message, !condition); | |
65 } | |
66 /** | |
67 * Fails a test with the given message. | |
68 * | |
69 * @example This method can be called anytime you want to break out and fail | |
70 * the current test. | |
71 * | |
72 * <listing> | |
73 * public function testSomething():void { | |
74 * var instance:MyClass = new MyClass(); | |
75 * if(instance.foo()) { | |
76 * fail('The foo should not have been there'); | |
77 * } | |
78 * } | |
79 * </listing> | |
80 */ | |
81 static public function fail(message:String):void { | |
82 throw new AssertionFailedError(message); | |
83 } | |
84 | |
85 /** | |
86 * Asserts that the provided block throws an exception that matches | |
87 * the type provided. | |
88 * | |
89 * <listing> | |
90 * public function testFailingCode():void { | |
91 * assertThrows(CustomError, function():void { | |
92 * var instance:Sprite = new Sprite(); | |
93 * instance.callMethodThatThrows(); | |
94 * }); | |
95 * } | |
96 * </listing> | |
97 **/ | |
98 static public function assertThrows(errorType:Class, block:Function):void { | |
99 try { | |
100 block.call(); | |
101 fail("assertThrows block did not throw an expected exception"); | |
102 } | |
103 catch(e:Error) { | |
104 if(!(e is errorType)) { | |
105 fail("assertThrows did not throw the expected error type, instead threw: " + getQualifiedClassName(e)); | |
106 } | |
107 } | |
108 } | |
109 | |
110 /** | |
111 * Asserts that two objects are equal. If they are not | |
112 * an AssertionFailedError is thrown with the given message. | |
113 * | |
114 * This assertion should be (by far) the one you use the most. | |
115 * It automatically provides useful information about what | |
116 * the failing values were. | |
117 * | |
118 * <listing> | |
119 * public function testNames():void { | |
120 * var name1:String = "Federico Aubele"; | |
121 * var name2:String = "Frederico Aubele"; | |
122 * | |
123 * assertEquals(name1, name2); | |
124 * } | |
125 * </listing> | |
126 */ | |
127 static public function assertEquals(...args:Array):void { | |
128 var message:String; | |
129 var expected:Object; | |
130 var actual:Object; | |
131 | |
132 if(args.length == 2) { | |
133 message = ""; | |
134 expected = args[0]; | |
135 actual = args[1]; | |
136 } | |
137 else if(args.length == 3) { | |
138 message = args[0]; | |
139 expected = args[1]; | |
140 actual = args[2]; | |
141 } | |
142 else { | |
143 throw new IllegalOperationError("Invalid argument count"); | |
144 } | |
145 | |
146 if(expected == null && actual == null) { | |
147 return; | |
148 } | |
149 | |
150 try { | |
151 if(expected != null && expected.equals(actual)) { | |
152 return; | |
153 } | |
154 } | |
155 catch(e:Error) { | |
156 if(expected != null && expected == actual) { | |
157 return; | |
158 } | |
159 } | |
160 | |
161 failNotEquals(message, expected, actual); | |
162 } | |
163 /** | |
164 * Asserts that an object isn't null. If it is | |
165 * an AssertionFailedError is thrown with the given message. | |
166 */ | |
167 static public function assertNotNull(...args:Array):void { | |
168 var message:String; | |
169 var object:Object; | |
170 | |
171 if(args.length == 1) { | |
172 message = ""; | |
173 object = args[0]; | |
174 } | |
175 else if(args.length == 2) { | |
176 message = args[0]; | |
177 object = args[1]; | |
178 } | |
179 else { | |
180 throw new IllegalOperationError("Invalid argument count"); | |
181 } | |
182 | |
183 assertTrue(message, object != null); | |
184 } | |
185 /** | |
186 * Asserts that an object is null. If it is not | |
187 * an AssertionFailedError is thrown with the given message. | |
188 */ | |
189 static public function assertNull(...args:Array):void { | |
190 var message:String; | |
191 var object:Object; | |
192 | |
193 if(args.length == 1) { | |
194 message = ""; | |
195 object = args[0]; | |
196 } | |
197 else if(args.length == 2) { | |
198 message = args[0]; | |
199 object = args[1]; | |
200 } | |
201 else { | |
202 throw new IllegalOperationError("Invalid argument count"); | |
203 } | |
204 | |
205 assertTrue(message, object == null); | |
206 } | |
207 /** | |
208 * Asserts that two objects refer to the same object. If they are not | |
209 * an AssertionFailedError is thrown with the given message. | |
210 */ | |
211 static public function assertSame(...args:Array):void { | |
212 var message:String; | |
213 var expected:Object; | |
214 var actual:Object; | |
215 | |
216 if(args.length == 2) { | |
217 message = ""; | |
218 expected = args[0]; | |
219 actual = args[1]; | |
220 } | |
221 else if(args.length == 3) { | |
222 message = args[0]; | |
223 expected = args[1]; | |
224 actual = args[2]; | |
225 } | |
226 else { | |
227 throw new IllegalOperationError("Invalid argument count"); | |
228 } | |
229 | |
230 if(expected === actual) { | |
231 return; | |
232 } | |
233 failNotSame(message, expected, actual); | |
234 } | |
235 /** | |
236 * Asserts that two objects do not refer to the same object. If they do, | |
237 * an AssertionFailedError is thrown with the given message. | |
238 */ | |
239 static public function assertNotSame(...args:Array):void { | |
240 var message:String; | |
241 var expected:Object; | |
242 var actual:Object; | |
243 | |
244 if(args.length == 2) { | |
245 message = ""; | |
246 expected = args[0]; | |
247 actual = args[1]; | |
248 } | |
249 else if(args.length == 3) { | |
250 message = args[0]; | |
251 expected = args[1]; | |
252 actual = args[2]; | |
253 } | |
254 else { | |
255 throw new IllegalOperationError("Invalid argument count"); | |
256 } | |
257 | |
258 if(expected === actual) | |
259 failSame(message); | |
260 } | |
261 | |
262 /** | |
263 * Asserts that two numerical values are equal within a tolerance range. | |
264 * If they are not an AssertionFailedError is thrown with the given message. | |
265 */ | |
266 static public function assertEqualsFloat(...args:Array):void { | |
267 var message:String; | |
268 var expected:Number; | |
269 var actual:Number; | |
270 var tolerance:Number = 0; | |
271 | |
272 if(args.length == 3) { | |
273 message = ""; | |
274 expected = args[0]; | |
275 actual = args[1]; | |
276 tolerance = args[2]; | |
277 } | |
278 else if(args.length == 4) { | |
279 message = args[0]; | |
280 expected = args[1]; | |
281 actual = args[2]; | |
282 tolerance = args[3]; | |
283 } | |
284 else { | |
285 throw new IllegalOperationError("Invalid argument count"); | |
286 } | |
287 if (isNaN(tolerance)) tolerance = 0; | |
288 if(Math.abs(expected - actual) <= tolerance) { | |
289 return; | |
290 } | |
291 failNotEquals(message, expected, actual); | |
292 } | |
293 | |
294 /** | |
295 * Asserts that two arrays have the same length and contain the same | |
296 * objects in the same order. If the arrays are not equal by this | |
297 * definition an AssertionFailedError is thrown with the given message. | |
298 */ | |
299 static public function assertEqualsArrays(...args:Array):void { | |
300 var message:String; | |
301 var expected:Array; | |
302 var actual:Array; | |
303 | |
304 if(args.length == 2) { | |
305 message = ""; | |
306 expected = args[0]; | |
307 actual = args[1]; | |
308 } | |
309 else if(args.length == 3) { | |
310 message = args[0]; | |
311 expected = args[1]; | |
312 actual = args[2]; | |
313 } | |
314 else { | |
315 throw new IllegalOperationError("Invalid argument count"); | |
316 } | |
317 | |
318 if (expected == null && actual == null) { | |
319 return; | |
320 } | |
321 if ((expected == null && actual != null) || (expected != null && actual == null)) { | |
322 failNotEquals(message, expected, actual); | |
323 } | |
324 // from here on: expected != null && actual != null | |
325 if (expected.length != actual.length) { | |
326 failNotEquals(message, expected, actual); | |
327 } | |
328 for (var i : int = 0; i < expected.length; i++) { | |
329 assertEquals(expected[i], actual[i]); | |
330 } | |
331 } | |
332 | |
333 /** | |
334 * Asserts that two arrays have the same length and contain the same | |
335 * objects. The order of the objects in the arrays is ignored. If they | |
336 * are not equal by this definition an AssertionFailedError is thrown | |
337 * with the given message. | |
338 */ | |
339 static public function assertEqualsArraysIgnoringOrder(...args:Array):void { | |
340 var message:String; | |
341 var expected:Array; | |
342 var actual:Array; | |
343 | |
344 if(args.length == 2) { | |
345 message = ""; | |
346 expected = args[0]; | |
347 actual = args[1]; | |
348 } | |
349 else if(args.length == 3) { | |
350 message = args[0]; | |
351 expected = args[1]; | |
352 actual = args[2]; | |
353 } | |
354 else { | |
355 throw new IllegalOperationError("Invalid argument count"); | |
356 } | |
357 | |
358 if (expected == null && actual == null) { | |
359 return; | |
360 } | |
361 if ((expected == null && actual != null) || (expected != null && actual == null)) { | |
362 failNotEquals(message, expected, actual); | |
363 } | |
364 // from here on: expected != null && actual != null | |
365 if (expected.length != actual.length) { | |
366 failNotEquals(message, expected, actual); | |
367 } | |
368 for (var i : int = 0; i < expected.length; i++) { | |
369 var foundMatch : Boolean = false; | |
370 var expectedMember : Object = expected[i]; | |
371 for (var j : int = 0; j < actual.length; j++) { | |
372 var actualMember : Object = actual[j]; | |
373 try { | |
374 assertEquals(expectedMember, actualMember); | |
375 foundMatch = true; | |
376 break; | |
377 } | |
378 catch (e : AssertionFailedError) { | |
379 // no match, try next | |
380 } | |
381 } | |
382 if (!foundMatch) { | |
383 failNotEquals("Found no match for " + expectedMember + ";", expected, actual); | |
384 } | |
385 } | |
386 } | |
387 | |
388 | |
389 static private function failSame(message:String):void { | |
390 var formatted:String = ""; | |
391 if(message != null) { | |
392 formatted = message + " "; | |
393 } | |
394 fail(formatted + "expected not same"); | |
395 } | |
396 | |
397 static private function failNotSame(message:String, expected:Object, actual:Object):void { | |
398 var formatted:String = ""; | |
399 if(message != null) { | |
400 formatted = message + " "; | |
401 } | |
402 fail(formatted + "expected same:<" + expected + "> was not:<" + actual + ">"); | |
403 } | |
404 | |
405 static private function failNotEquals(message:String, expected:Object, actual:Object):void { | |
406 fail(format(message, expected, actual)); | |
407 } | |
408 | |
409 static private function format(message:String, expected:Object, actual:Object):String { | |
410 var formatted:String = ""; | |
411 if(message != null) { | |
412 formatted = message + " "; | |
413 } | |
414 return formatted + "expected:<" + expected + "> but was:<" + actual + ">"; | |
415 } | |
416 } | |
417 } |