浏览代码

Properly exit with non-zero exit code when test fails.

Andy Li 11 年之前
父节点
当前提交
b44bdfdcc1
共有 1 个文件被更改,包括 41 次插入10 次删除
  1. 41 10
      tests/unit/Test.hx

+ 41 - 10
tests/unit/Test.hx

@@ -12,15 +12,20 @@ class Test #if swf_mark implements mt.Protect #end {
 
 	function eq<T>( v : T, v2 : T, ?pos ) {
 		count++;
-		if( v != v2 ) report(Std.string(v)+" should be "+Std.string(v2),pos);
+		if( v != v2 ) {
+			report(Std.string(v)+" should be "+Std.string(v2),pos);
+			success = false;
+		}
 	}
 
 	function feq( v : Float, v2 : Float, ?pos ) {
 		count++;
 		if (!Math.isFinite(v) || !Math.isFinite(v2))
 			eq(v, v2, pos);
-		else if ( Math.abs(v - v2) > 1e-15 )
+		else if ( Math.abs(v - v2) > 1e-15 ) {
 			report(v+" should be "+v2,pos);
+			success = false;
+		}
 	}
 
 	function t( v, ?pos ) {
@@ -40,6 +45,7 @@ class Test #if swf_mark implements mt.Protect #end {
 		try {
 			f();
 			report("No exception occured",pos);
+			success = false;
 		} catch( e : Dynamic ) {
 		}
 	}
@@ -58,30 +64,39 @@ class Test #if swf_mark implements mt.Protect #end {
 			if( v == v2 )
 				return;
 		report(v+" not in "+Std.string(values),pos);
+		success = false;
 	}
 
 	function hf(c:Class<Dynamic>, n:String, ?pos:haxe.PosInfos) {
 		Test.count++;
-		if (!Lambda.has(Type.getInstanceFields(c), n))
+		if (!Lambda.has(Type.getInstanceFields(c), n)) {
 			Test.report(Type.getClassName(c) + " should have member field " +n, pos);
+			success = false;
+		}
 	}
 
 	function nhf(c:Class<Dynamic>, n:String, ?pos:haxe.PosInfos) {
 		Test.count++;
-		if (Lambda.has(Type.getInstanceFields(c), n))
+		if (Lambda.has(Type.getInstanceFields(c), n)) {
 			Test.report(Type.getClassName(c) + " should not have member field " +n, pos);
+			success = false;
+		}
 	}
 
 	function hsf(c:Class<Dynamic> , n:String, ?pos:haxe.PosInfos) {
 		Test.count++;
-		if (!Lambda.has(Type.getClassFields(c), n))
+		if (!Lambda.has(Type.getClassFields(c), n)) {
 			Test.report(Type.getClassName(c) + " should have static field " +n, pos);
+			success = false;
+		}
 	}
 
 	function nhsf(c:Class<Dynamic> , n:String, ?pos:haxe.PosInfos) {
 		Test.count++;
-		if (Lambda.has(Type.getClassFields(c), n))
+		if (Lambda.has(Type.getClassFields(c), n)) {
 			Test.report(Type.getClassName(c) + " should not have static field " +n, pos);
+			success = false;
+		}
 	}
 
 	function infos( m : String ) {
@@ -98,10 +113,13 @@ class Test #if swf_mark implements mt.Protect #end {
 			count++;
 			if( !asyncWaits.remove(pos) ) {
 				report("Double async result",pos);
+				success = false;
 				return;
 			}
-			if( v != v2 )
+			if( v != v2 ) {
 				report(v2+" should be "+v,pos);
+				success = false;
+			}
 			checkDone();
 		});
 	}
@@ -116,16 +134,21 @@ class Test #if swf_mark implements mt.Protect #end {
 			count++;
 			if( asyncWaits.remove(pos) )
 				checkDone();
-			else
+			else {
 				report("Multiple async events",pos);
+				success = false;
+			}
 		});
 		f(args,function(v) {
 			count++;
 			if( asyncWaits.remove(pos) ) {
 				report("No exception occured",pos);
+				success = false;
 				checkDone();
-			} else
+			} else {
 				report("Multiple async events",pos);
+				success = false;
+			}
 		});
 	}
 
@@ -141,6 +164,7 @@ class Test #if swf_mark implements mt.Protect #end {
 	static var asyncCache = new Array<Void -> Void>();
 	static var AMAX = 3;
 	static var timer : haxe.Timer;
+	static var success = true;
 
 	dynamic static function report( msg : String, ?pos : haxe.PosInfos ) {
 		if( reportInfos != null ) {
@@ -171,8 +195,10 @@ class Test #if swf_mark implements mt.Protect #end {
 	static function asyncTimeout() {
 		if( asyncWaits.length == 0 )
 			return;
-		for( pos in asyncWaits )
+		for( pos in asyncWaits ) {
 			report("TIMEOUT",pos);
+			success = false;
+		}
 		asyncWaits = new Array();
 		checkDone();
 	}
@@ -196,6 +222,7 @@ class Test #if swf_mark implements mt.Protect #end {
 		try msg = Std.string(e) catch( e : Dynamic ) {};
 		reportCount = 0;
 		report("ABORTED : "+msg+" in "+context);
+		success = false;
 		reportInfos = null;
 		trace("STACK :\n"+stack);
 	}
@@ -298,6 +325,10 @@ class Test #if swf_mark implements mt.Protect #end {
 			onError(e,"ABORTED",Type.getClassName(current));
 		}
 		#end
+
+		#if sys
+		Sys.exit(success ? 0 : 1);
+		#end
 	}
 
 }