This library provides async unit tests by extending haxe standard unit tests.
haxelib install async-tests
This lib consists of two files:
Its usage is almost identical to using standard haxe unit tests
// Test runner class
class MyTestRunner {
static function main() {
var r = new async.tests.AsyncTestRunner(onComplete);
r.add(new MyTestCase())
r.run();
}
// function called when all tests finish
static function onComplete() {
#if (cpp || neko || php)
Sys.exit(0);
#end
}
}
AsyncTestRunner constructor receives a callback to onComplete
, so it can notify the when all tests finish.
class MyTestCase extends AsyncTestCase {
function testSampleAsync() {
// test only ends when async created is executed or timesout
urlLoader.addEventListener(Event.COMPLETE, createAsync(onAssetsLoaded, 300));
}
function onAssetsLoaded(o:Dynamic) {
var event = cast(o, Event);
assertTrue(event.type == Event.COMPLETE);
}
}
AsyncTestCase has the special method:
createAsync(callback:Dynamic->Void, timeout:Int):Dynamic->Void
When createAsync(method)
is called, it creates another method wrapped around the original, and keeps track how mutch time it takes to invoke it.
// another alternative to perform same action
var t = createAsync(onTestsLoaded, 300);
urdLoader.addEventListener(Event.COMPLETE, t);
createAsync()
is called, the timeout starts counting immediatelly, so always create "asyncs" right before invoking an operation that uses them.createAsync()
have the signature Dynamic->Void
(other signatures maybe available in the future).