Skip to content

Commit

Permalink
Port display tests to utest (#7895)
Browse files Browse the repository at this point in the history
* [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
  • Loading branch information
Simn authored Feb 28, 2019
1 parent bc63451 commit 4340d20
Show file tree
Hide file tree
Showing 15 changed files with 41 additions and 93 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,4 @@ tests/sourcemaps/bin
/*_plugin.ml
tests/benchs/export/
tests/benchs/dump/
tests/display/.unittest/
9 changes: 8 additions & 1 deletion tests/display/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
4 changes: 2 additions & 2 deletions tests/display/build.hxml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-p src
--main Main
-lib utest
--interp
-D use-rtti-doc
# -D test=Issue7877
-D use-rtti-doc
61 changes: 12 additions & 49 deletions tests/display/src/DisplayTestCase.hx
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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);
}
}
5 changes: 3 additions & 2 deletions tests/display/src/DisplayTestContext.hx
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,20 @@ 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;
}
}

function callHaxe(displayPart:String):String {
var args = [
"-cp", "src",
"-D", "display-stdin",
"-lib", "utest",
"--display",
source.path + "@" + displayPart,
];
Expand Down
19 changes: 6 additions & 13 deletions tests/display/src/Macro.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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]));
}
Expand Down
22 changes: 1 addition & 21 deletions tests/display/src/Main.hx
Original file line number Diff line number Diff line change
@@ -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"));
}
}
1 change: 0 additions & 1 deletion tests/display/src/cases/Issue6068.hx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class Issue6068 extends DisplayTestCase {
}

function check(fn) {
numTests++;
var result = try {
fn();
false;
Expand Down
1 change: 1 addition & 0 deletions tests/display/src/cases/Issue7072.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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 change: 1 addition & 0 deletions tests/display/src/cases/Issue7082.hx
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ class Issue7082 extends DisplayTestCase {
**/
function test() {
fields(pos(1));
eq(true, true); // TODO
}
}
2 changes: 1 addition & 1 deletion tests/display/src/cases/Issue7086.hx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ class Issue7086 extends DisplayTestCase {
}
**/
function test() {
noCompletionPoint(toplevel.bind(pos(1)));
assert(noCompletionPoint(toplevel.bind(pos(1))));
}
}
2 changes: 1 addition & 1 deletion tests/display/src/cases/Issue7878.hx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ class Issue7878 extends DisplayTestCase {
}
**/
function test() {
// TODO: need assertError or something
assert(typeNotFound(type.bind(pos(1)), "SomethingUnknown"));
}
}
2 changes: 1 addition & 1 deletion tests/display/src/cases/IssueTemplate.hx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ class IssueTemplate extends DisplayTestCase {
}
**/
function test() {

eq(true, true);
}
}
1 change: 1 addition & 0 deletions tests/display/src/cases/Toplevel.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/**
Expand Down
3 changes: 2 additions & 1 deletion tests/runci/targets/Macro.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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"]);

Expand All @@ -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"]);
}
Expand Down

0 comments on commit 4340d20

Please sign in to comment.