Skip to content

Instantly share code, notes, and snippets.

@jonnyreeves
Created September 22, 2011 16:05
Show Gist options
  • Save jonnyreeves/1235171 to your computer and use it in GitHub Desktop.
Save jonnyreeves/1235171 to your computer and use it in GitHub Desktop.
AS3Signals FlexUnit 4 Async Assertion Helper
package uk.co.jonnyreeves.flexunit
{
import flash.events.EventDispatcher;
import mx.events.MetadataEvent;
import org.flexunit.async.Async;
import org.osflash.signals.ISignal;
public function assertAfterSignal(testCase : Object, signal : ISignal, handler : Function, timeout : int = 500) : void
{
const dispatcher : EventDispatcher = new EventDispatcher();
// This method will be invoked when the FlexUnit AsyncHandler is invoked.
const eventHandler : Function = function(event : MetadataEvent, passThru : Object = null) : void {
handler.apply(null, event.info as Array);
};
// This helper is responsible for dispatching the event which will trip the FlexUnit Async apparatus. We
// Use a MetadataEvent simply because it's built in and offers the ability to pass some arbitrary data along
// with it.
const dispatchAssertEvent : Function = function(args : Array) : void {
dispatcher.dispatchEvent(new MetadataEvent("onSignal", false, false, args));
}
// Now the fun beings - AS3Signals doesn't support the use of splats (aka ...(rest) arguments) so we are
// forced to create a switch which closley resembles some modern art. This allows us to register a function
// to the Signal which will dispatch the 'onSignal' event and thereby invoke the FlexUnit Async apparatus.
switch (handler.length)
{
case 0:
signal.addOnce(function() : void { dispatchAssertEvent(null); });
break;
case 1:
signal.addOnce(function(arg1 : *) : void { dispatchAssertEvent(arguments); });
break;
case 2:
signal.addOnce(function(arg1 : *, arg2 : *) : void { dispatchAssertEvent(arguments); });
break;
case 3:
signal.addOnce(function(arg1 : *, arg2 : *, arg3 : *) : void { dispatchAssertEvent(arguments); });
break;
case 4:
signal.addOnce(function(arg1 : *, arg2 : *, arg3 : *, arg4 : *) : void { dispatchAssertEvent(arguments); });
break;
case 5:
signal.addOnce(function(arg1 : *, arg2 : *, arg3 : *, arg4 : *, arg5 : *) : void { dispatchAssertEvent(arguments); });
break;
case 6:
signal.addOnce(function(arg1 : *, arg2 : *, arg3 : *, arg4 : *, arg5 : *, arg6: *) : void { dispatchAssertEvent(arguments); });
break;
case 7:
signal.addOnce(function(arg1 : *, arg2 : *, arg3 : *, arg4 : *, arg5 : *, arg6: *, arg7 : *) : void { dispatchAssertEvent(arguments); });
break;
case 8:
signal.addOnce(function(arg1 : *, arg2 : *, arg3 : *, arg4 : *, arg5 : *, arg6: *, arg7 : *, arg8 : *) : void { dispatchAssertEvent(arguments); });
break;
default:
throw new Error("Seriously WTF?!");
break;
}
// Now bind the FlexUnit Event Listener to our mock dispatcher/'onSignal' event
Async.handleEvent(testCase, dispatcher, "onSignal", eventHandler, timeout)
}
}
@robertpenner
Copy link

V0.9 of AS3 Signals allows ...rest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment