src/asunit/framework/Assert.as in asunit4-4.2.2.pre vs src/asunit/framework/Assert.as in asunit4-4.2.3.pre

- old
+ new

@@ -110,10 +110,41 @@ } throw new AssertionFailedError("expected error type:<" + getQualifiedClassName(errorType) + "> but none was thrown." ); } /** + * Asserts that the provided block throws an exception that matches + * the type and message provided. + * + * <listing> + * public function testFailingCode():void { + * assertThrows(CustomError, "Invalid state", function():void { + * var instance:Sprite = new Sprite(); + * instance.callMethodThatThrows(); + * }); + * } + * </listing> + **/ + static public function assertThrowsWithMessage(errorType:Class, errorMessage:String, block:Function):void { + try { + block.call(); + } + catch(e:Error) { + if(!(e is errorType)) { + throw new AssertionFailedError("expected error type:<" + getQualifiedClassName(errorType) + +"> but was:<" + getQualifiedClassName(e) + ">"); + } + if(e.message != errorMessage) { + throw new AssertionFailedError("expected error message:<" + errorMessage + +"> but was:<" + e.message + ">"); + } + return; + } + throw new AssertionFailedError("expected error type:<" + getQualifiedClassName(errorType) + "> with message:<" + errorMessage + "> but none was thrown." ); + } + + /** * Asserts that two objects are equal. If they are not * an AssertionFailedError is thrown with the given message. * * This assertion should be (by far) the one you use the most. * It automatically provides useful information about what @@ -186,10 +217,34 @@ if(actual == null) { throw new AssertionFailedError(message + "expected not null but was:<" + actual + ">"); } } + + static public function assertMatches(...args:Array):void { + var message:String; + var expr:RegExp; + var content:String; + + if(args.length == 2) { + message = ""; + expr = args[0]; + content = args[1]; + } + else if(args.length == 3) { + message = args[0]; + expr = args[1]; + content = args[2]; + } + else { + throw new IllegalOperationError("Invalid argument count"); + } + if(!content.match(expr)) { + fail("Unable to match [" + expr + "] in content: [" + content + "]"); + } + } + /** * Asserts that an object is null. If it is not * an AssertionFailedError is thrown with the given message. */ static public function assertNull(...args:Array):void { @@ -371,27 +426,37 @@ } // from here on: expected != null && actual != null if (expected.length != actual.length) { failNotEquals(message, expected, actual); } - for (var i : int = 0; i < expected.length; i++) { - var foundMatch : Boolean = false; - var expectedMember : Object = expected[i]; - for (var j : int = 0; j < actual.length; j++) { - var actualMember : Object = actual[j]; + + var unusedPotentialMatches:Array = actual.slice(); + + var iLength:uint = expected.length; + var jLength:uint; + + searchingForExpectedItems: + for (var i:int = 0; i < iLength; i++) + { + var expectedMember : Object = expected[i]; + jLength = unusedPotentialMatches.length; + + checkingAgainstActualItems: + for (var j : int = 0; j < jLength; j++) { + var actualMember : Object = unusedPotentialMatches[j]; try { assertEquals(expectedMember, actualMember); - foundMatch = true; - break; + unusedPotentialMatches.splice(j, 1); + continue searchingForExpectedItems; } catch (e : AssertionFailedError) { // no match, try next } } - if (!foundMatch) { - failNotEquals("Found no match for " + expectedMember + ";", expected, actual); - } - } + + failNotEquals("Found no match for " + expectedMember + ";", expected, actual); + + } } static private function failNotEquals(message:String, expected:Object, actual:Object):void { fail(format(message, expected, actual)); }