Forráskód Böngészése

Port display tests to utest (#7895)

* [tests] start on porting display tests to utest

* [tests] fix position

* dodge utest problem

* [tests] green

* [tests] need utest earlier

* [tests] avoid utest variance problem
Simon Krajewski 6 éve
szülő
commit
4340d200af

+ 1 - 0
.gitignore

@@ -117,3 +117,4 @@ tests/sourcemaps/bin
 /*_plugin.ml
 tests/benchs/export/
 tests/benchs/dump/
+tests/display/.unittest/

+ 8 - 1
tests/display/.vscode/settings.json

@@ -2,5 +2,12 @@
 	"haxe.displayConfigurations": [
 		{"label": "IssueTemplate", "args": ["build.hxml", "-D", "test=IssueTemplate"]}
 	],
-	"haxe.enableCompilationServer": false
+	"haxe.enableCompilationServer": false,
+	"haxeTestExplorer.testCommand": [
+		"${haxe}",
+		"build.hxml",
+		"-lib",
+		"test-adapter"
+	],
+	"haxeTestExplorer.launchConfiguration": "Haxe-eval"
 }

+ 2 - 2
tests/display/build.hxml

@@ -1,5 +1,5 @@
 -p src
 --main Main
+-lib utest
 --interp
--D use-rtti-doc
-# -D test=Issue7877
+-D use-rtti-doc

+ 12 - 49
tests/display/src/DisplayTestCase.hx

@@ -1,14 +1,13 @@
+import utest.Assert;
 import Types;
 
 using Lambda;
 
 @:autoBuild(Macro.buildTestCase())
-class DisplayTestCase {
+class DisplayTestCase implements utest.ITest {
 	var ctx:DisplayTestContext;
-	var methods:Array<Void->Void>;
-	var numTests:Int;
-	var numFailures:Int;
-	var testName:String;
+
+	public function new() { }
 
 	// api
 	inline function pos(name) return ctx.pos(name);
@@ -22,54 +21,29 @@ class DisplayTestCase {
 	inline function metadataDoc(pos1) return ctx.metadataDoc(pos1);
 	inline function diagnostics() return ctx.diagnostics();
 
-	inline function noCompletionPoint(f) return ctx.noCompletionPoint(f);
+	inline function noCompletionPoint(f) return ctx.hasErrorMessage(f, "No completion point");
+	inline function typeNotFound(f, typeName) return ctx.hasErrorMessage(f, "Type not found : " + typeName);
 
-	function assert(v:Bool) if (!v) throw "assertion failed";
+	function assert(v:Bool) Assert.isTrue(v);
 
 	function eq<T>(expected:T, actual:T, ?pos:haxe.PosInfos) {
-		numTests++;
-		if (expected != actual) {
-			numFailures++;
-			report("Assertion failed", pos);
-			report("Expected: " + expected, pos);
-			report("Actual:   " + actual, pos);
-		}
+		Assert.equals(expected, actual, pos);
 	}
 
 	function arrayEq<T>(expected:Array<T>, actual:Array<T>, ?pos:haxe.PosInfos) {
-		numTests++;
-		var leftover = expected.copy();
-		for (actual in actual) {
-			if (!leftover.remove(actual)) {
-				numFailures++;
-				report("Result not part of expected Array:", pos);
-				report(Std.string(actual), pos);
-			}
-		}
-		for (leftover in leftover) {
-			numFailures++;
-			report("Expected result was not part of actual Array:", pos);
-			report(Std.string(leftover), pos);
-			return;
-		}
+		Assert.same(expected, actual, pos);
 	}
 
 	function arrayCheck<T>(expected:Array<T>, actual:Array<T>, f : T -> String, ?pos:haxe.PosInfos) {
 		var expected = [for (expected in expected) f(expected) => expected];
 		for (actual in actual) {
 			var key = f(actual);
-			if (!expected.exists(key)) {
-				numFailures++;
-				report("Result not part of expected Array:", pos);
-				report(Std.string(actual), pos);
-			}
+			Assert.isTrue(expected.exists(key), "Result not part of expected Array: " + Std.string(actual), pos);
 			expected.remove(key);
 		}
 
 		for (expected in expected) {
-			numFailures++;
-			report("Expected result was not part of actual Array:", pos);
-			report(Std.string(expected), pos);
+			Assert.fail("Expected result was not part of actual Array: " + Std.string(expected), pos);
 			return;
 		}
 	}
@@ -100,17 +74,6 @@ class DisplayTestCase {
 	}
 
 	function report(message, pos:haxe.PosInfos) {
-		haxe.Log.trace(message, pos);
-	}
-
-	public function run() {
-		for (method in methods) {
-			method();
-		}
-		return {
-			testName: testName,
-			numTests: numTests,
-			numFailures: numFailures
-		}
+		Assert.fail(message, pos);
 	}
 }

+ 3 - 2
tests/display/src/DisplayTestContext.hx

@@ -82,12 +82,12 @@ class DisplayTestContext {
 		return if (result == null) [] else result.diagnostics;
 	}
 
-	public function noCompletionPoint(f:Void -> Void):Bool {
+	public function hasErrorMessage(f:Void -> Void, message:String) {
 		return try {
 			f();
 			false;
 		} catch(exc:HaxeInvocationException) {
-			return exc.message.indexOf("No completion point") != -1;
+			return exc.message.indexOf(message) != -1;
 		}
 	}
 
@@ -95,6 +95,7 @@ class DisplayTestContext {
 		var args = [
 			"-cp", "src",
 			"-D", "display-stdin",
+			"-lib", "utest",
 			"--display",
 			source.path + "@" + displayPart,
 		];

+ 6 - 13
tests/display/src/Macro.hx

@@ -44,20 +44,13 @@ class Macro {
 					}
 				}
 			}
-			testCases.push(macro function() {
-				ctx = new DisplayTestContext($v{filename}, $v{field.name}, $v{src}, $markers);
-				$i{field.name}();
-			});
-		}
 
-		fields.push((macro class {
-			public function new() {
-				testName = $v{c.name};
-				numTests = 0;
-				numFailures = 0;
-				this.methods = $a{testCases};
+			switch (field.kind) {
+				case FFun(f) if (f.expr != null):
+					f.expr = macro @:pos(f.expr.pos) { ctx = new DisplayTestContext($v{filename}, $v{field.name}, $v{src}, $markers); ${f.expr} };
+				case _:
 			}
-		}).fields[0]);
+		}
 
 		return fields;
 	}
@@ -77,7 +70,7 @@ class Macro {
 				var p = new haxe.io.Path(file);
 				if (p.ext == "hx") {
 					var tp = {pack: pack, name: p.file};
-					cases.push(macro { name:$v{tp.name}, exec:new $tp() });
+					cases.push(macro new $tp());
 				} else if(Path.join([path, file]).isDirectory()) {
 					loop(pack.concat([file]));
 				}

+ 1 - 21
tests/display/src/Main.hx

@@ -1,25 +1,5 @@
 class Main {
 	static function main() {
-		var tests = Macro.getCases("cases");
-		var numTests = 0;
-		var numFailures = 0;
-		for (test in tests) {
-			try {
-				Sys.print(test.name);
-				var result = test.exec.run();
-				numTests += result.numTests;
-				numFailures += result.numFailures;
-			    Sys.println(' ${result.numTests} tests, ${result.numFailures} failures');
-			} catch(e:DisplayTestContext.HaxeInvocationException) {
-				Sys.println("Error:      " + e.message);
-				Sys.println("Field name: " + e.fieldName);
-				Sys.println("Arguments:  " + e.arguments.join(" "));
-				Sys.println("Source: " + e.source);
-				numTests++;
-				numFailures++;
-			}
-		}
-		Sys.println('Finished with $numTests tests, $numFailures failures');
-		Sys.exit(numFailures == 0 ? 0 : 1);
+		utest.UTest.run(Macro.getCases("cases"));
 	}
 }

+ 0 - 1
tests/display/src/cases/Issue6068.hx

@@ -17,7 +17,6 @@ class Issue6068 extends DisplayTestCase {
 	}
 
 	function check(fn) {
-		numTests++;
 		var result = try {
 				fn();
 				false;

+ 1 - 0
tests/display/src/cases/Issue7072.hx

@@ -22,5 +22,6 @@ class Issue7072 extends DisplayTestCase {
 		// eq("foo", results[0].name);
 		// eq("bar", results[1].name);
 		// eq("foobar", results[2].name);
+		eq(true, true); // TODO
 	}
 }

+ 1 - 0
tests/display/src/cases/Issue7082.hx

@@ -11,5 +11,6 @@ class Issue7082 extends DisplayTestCase {
 	**/
 	function test() {
 		fields(pos(1));
+		eq(true, true); // TODO
 	}
 }

+ 1 - 1
tests/display/src/cases/Issue7086.hx

@@ -9,6 +9,6 @@ class Issue7086 extends DisplayTestCase {
 	}
 	**/
 	function test() {
-		noCompletionPoint(toplevel.bind(pos(1)));
+		assert(noCompletionPoint(toplevel.bind(pos(1))));
 	}
 }

+ 1 - 1
tests/display/src/cases/Issue7878.hx

@@ -9,6 +9,6 @@ class Issue7878 extends DisplayTestCase {
 }
 	**/
 	function test() {
-		// TODO: need assertError or something
+		assert(typeNotFound(type.bind(pos(1)), "SomethingUnknown"));
 	}
 }

+ 1 - 1
tests/display/src/cases/IssueTemplate.hx

@@ -9,6 +9,6 @@ class IssueTemplate extends DisplayTestCase {
 	}
 	**/
 	function test() {
-
+		eq(true, true);
 	}
 }

+ 1 - 0
tests/display/src/cases/Toplevel.hx

@@ -116,6 +116,7 @@ class Toplevel extends DisplayTestCase {
 		// var typesCompletion = toplevel(pos(1));
 		// eq(true, hasToplevel(typesCompletion, "type", "Array"));
 		// eq(true, hasToplevel(typesCompletion, "package", "haxe"));
+		eq(true, true); // TODO
 	}
 
 	/**

+ 2 - 1
tests/runci/targets/Macro.hx

@@ -10,6 +10,8 @@ class Macro {
 	static public function run(args:Array<String>) {
 		runCommand("haxe", ["compile-macro.hxml"].concat(args));
 
+		haxelibInstall("utest");
+
 		changeDirectory(displayDir);
 		runCommand("haxe", ["build.hxml"]);
 
@@ -25,7 +27,6 @@ class Macro {
 		runCommand("haxe", ["compile.hxml"]);
 
 		changeDirectory(sysDir);
-		haxelibInstall("utest");
 		runCommand("haxe", ["compile-macro.hxml"]);
 		runCommand("haxe", ["compile-each.hxml", "--run", "Main"]);
 	}