Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify #17

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 10 additions & 32 deletions src/tink/testrunner/Assertions.hx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import haxe.PosInfos;

using tink.CoreApi;

private typedef Impl = Stream<Assertion #if pure , Error #end>;
private typedef Impl = Stream<Assertion, Error>;

@:forward @:transitive
abstract Assertions(Impl) from Impl to Impl {
Expand All @@ -21,59 +21,37 @@ abstract Assertions(Impl) from Impl to Impl {
public static inline function ofPromiseArray(o:Promise<Array<Assertion>>):Assertions {
return o.next(function(o):Impl return o.iterator());
}

@:from
public static function ofFutureAssertion(p:Future<Assertion>):Assertions {
#if (java && pure) // HACK: somehow this passes the java native compilation
return Stream.flatten(p.map(function(a):Stream<Dynamic, Dynamic> return Stream.single(a)));
#else
return p.map(function(a) return Success(ofAssertion(a)));
#end
}

@:from
public static function ofFutureAssertions(p:Future<Assertions>):Assertions {
return p.map(Success);
}

@:from
public static function ofSurpriseAssertion(p:Surprise<Assertion, Error>):Assertions {
#if (java && pure) // HACK: somehow this passes the java native compilation
return Stream.flatten(p.map(function(o):Stream<Dynamic, Dynamic> return switch o {
case Success(a): Stream.single(a);
case Failure(e): Stream.ofError(e);
}));
#else
return p >> function(o:Assertion) return ofAssertion(o);
#end
return Promise.lift(p).next(ofAssertion);
}

@:from
public static inline function ofOutcomeAssertions(o:Outcome<Assertions, Error>):Assertions {
return ofSurpriseAssertions(Future.sync(o));
}

@:from
public static inline function ofPromiseAssertions(p:Promise<Assertions>):Assertions {
return ofSurpriseAssertions(p);
}

@:from
public static inline function ofSurpriseAssertions(p:Surprise<Assertions, Error>):Assertions {
#if pure
#if java // HACK: somehow this passes the java native compilation
return Stream.flatten(p.map(function(o):Stream<Dynamic, Dynamic> return switch o {
case Success(a): (a:Stream<Assertion, Error>);
case Failure(e): Stream.ofError(e);
}));
#else
return Stream.promise((p:Surprise<Impl, Error>));
#end
#else
return Stream.later((p:Surprise<Impl, Error>));
#end
return Stream.promise((p:Surprise<Impl, Error>));
}

#if tink_unittest
// TODO: use solution from https://github.com/HaxeFoundation/haxe/issues/9611
@:from
Expand Down
41 changes: 21 additions & 20 deletions src/tink/testrunner/Runner.hx
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ using tink.testrunner.Runner.TimeoutHelper;
using tink.CoreApi;

class Runner {

public static function exit(result:BatchResult) {
Helper.exit(result.summary().failures.length);
}

public static function run(batch:Batch, ?reporter:Reporter, ?timers:TimerManager):Future<BatchResult> {

if(reporter == null) reporter = new BasicReporter();
if(timers == null) {
#if ((haxe_ver >= 3.3) || flash || js || openfl)
timers = new HaxeTimerManager();
#end
}

var includeMode = false;
for(s in batch.suites) {
if(includeMode) break;
Expand All @@ -34,8 +34,8 @@ class Runner {
break;
}
}
return Future.async(function(cb) {

return Future.irreversible(function(cb) {
reporter.report(BatchStart).handle(function(_) {
var iter = batch.suites.iterator();
var results:BatchResult = [];
Expand All @@ -54,17 +54,17 @@ class Runner {
});
});
}


static function runSuite(suite:Suite, reporter:Reporter, timers:TimerManager, includeMode:Bool):Future<SuiteResult> {
return Future.async(function(cb) {
return Future.irreversible(function(cb) {
var cases = suite.getCasesToBeRun(includeMode);
var hasCases = cases.length > 0;
reporter.report(SuiteStart(suite.info, hasCases)).handle(function(_) {

function setup() return hasCases ? suite.setup() : Promise.NOISE;
function teardown() return hasCases ? suite.teardown() : Promise.NOISE;

var iter = suite.cases.iterator();
var results = [];
function next() {
Expand All @@ -91,22 +91,23 @@ class Runner {
});
});
}

static function runCase(caze:Case, suite:Suite, reporter:Reporter, timers:TimerManager, shouldRun:Bool):Future<CaseResult> {
return Future.async(function(cb) {
return Future.irreversible(function(cb) {
if(shouldRun) {
reporter.report(CaseStart(caze.info, shouldRun)).handle(function(_) {
suite.before().timeout(caze.timeout, timers, caze.pos)
.next(function(_) {
var assertions = [];
return caze.execute().forEach(function(a) {
return
caze.execute().forEach(a -> {
assertions.push(a);
return reporter.report(Assertion(a)).map(function(_) return Resume);
reporter.report(Assertion(a)).map(_ -> None);
})
.next(function(o):Outcome<Array<Assertion>, Error> return switch o {
case Depleted: Success(assertions);
case Halted(_): throw 'unreachable';
case Failed(e): Failure(e);
case Done: Success(assertions);
case Failed(_, e): Failure(e);
default: throw 'unreachable';
})
.timeout(caze.timeout, timers);
})
Expand Down Expand Up @@ -134,12 +135,12 @@ class Runner {
}
});
}

}

class TimeoutHelper {
public static function timeout<T>(promise:Promise<T>, ms:Int, timers:TimerManager, ?pos:PosInfos):Promise<T> {
return Future.async(function(cb) {
return Future.irreversible(function(cb) {
var done = false;
var timer = null;
var link = promise.handle(function(o) {
Expand Down