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