Wednesday, May 13, 2009

FlexUnit assertNotEquals

why is there an assertEquals and no assertNotEquals in FlexUnit ?
there may be a reasonable explanation, but anyway I needed it, so here it is in case someone searches for that

here is my AssertWithNotEquals.as :

package flexunit.framework
{
public class AssertWithNotEquals extends Assert
{
public static function assertNotEquals(...rest):void
{
if ( rest.length == 3 ){
failEquals( rest[0], rest[1], rest[2] );
}else{
failEquals( "", rest[0], rest[1] );
}
}

private static function failEquals( message:String, expected:Object, actual:Object ):void
{
if ( expected == actual )
{
if ( message.length > 0 ) message = message + " - ";
throw new AssertionFailedError( message + " expected not to be the same, but was <" + actual + ">" );
}
}
}
}

3 comments:

schief zine said...
This comment has been removed by a blog administrator.
Peter said...

Nice, but the message comes out clunky: "not expected <Hello> and was <Hello>". I'd use something along the lines "expected something different from <Hello> but was the same".

Raz-L said...

good point. thx