tiagolr / asynctests

Extends haxe unit tests, providing async tests.
9 stars 1 forks source link

Async-Tests

This library provides async unit tests by extending haxe standard unit tests.

Install

haxelib install async-tests

Example

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);

Notes