Skip to content

Commit aef8f2c

Browse files
committed
[tests] test catching errors
1 parent a7c5cd4 commit aef8f2c

File tree

3 files changed

+72
-2
lines changed

3 files changed

+72
-2
lines changed

tests/server/src/cases/issues/Issue12001.hx

+26
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@ class Issue12001 extends TestCase {
1515
assertSuccess();
1616
}
1717

18+
function testRedefineTypeCatchError(_) {
19+
vfs.putContent("Macro.hx", getTemplate("issues/Issue12001/Macro.hx"));
20+
vfs.putContent("Empty.hx", getTemplate("Empty.hx"));
21+
var args = ["-main", "Empty", "--macro", "Macro.redefineTypeCatchError()"];
22+
runHaxe(args);
23+
assertSuccess();
24+
25+
runHaxe(args);
26+
assertSuccess();
27+
assertHasPrint("Macro.hx:56: TInst(Foobar,[])");
28+
assertHasPrint("Macro.hx:69: Cannot redefine module Foobar");
29+
}
30+
1831
@:async
1932
@:timeout(3000)
2033
function testRedefineType(async:Async) {
@@ -46,6 +59,19 @@ class Issue12001 extends TestCase {
4659
assertSuccess();
4760
}
4861

62+
function testRedefineModuleCatchError(_) {
63+
vfs.putContent("Macro.hx", getTemplate("issues/Issue12001/Macro.hx"));
64+
vfs.putContent("Empty.hx", getTemplate("Empty.hx"));
65+
var args = ["-main", "Empty", "--macro", "Macro.redefineModuleCatchError()"];
66+
runHaxe(args);
67+
assertSuccess();
68+
69+
runHaxe(args);
70+
assertSuccess();
71+
assertHasPrint("Macro.hx:77: TInst(Foobaz,[])");
72+
assertHasPrint("Macro.hx:90: Cannot redefine module Foobaz");
73+
}
74+
4975
@:async
5076
@:timeout(3000)
5177
function testRedefineModule(async:Async) {

tests/server/test/templates/issues/Issue12001/Macro.hx

+43
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import haxe.macro.Context;
2+
import haxe.macro.Expr.Error;
23

34
@:persistent var i = 0;
45
function defineType() {
@@ -48,3 +49,45 @@ function hookRedefine() {
4849
}]);
4950
});
5051
}
52+
53+
@:persistent var l = 0;
54+
function redefineTypeCatchError() {
55+
Context.onAfterInitMacros(() -> {
56+
if (l > 0) trace(Context.getType("Foobar"));
57+
58+
try {
59+
l++;
60+
Context.defineType({
61+
pos: Context.currentPos(),
62+
pack: [],
63+
name: "Foobar",
64+
kind: TDClass(null, null, false, false, false),
65+
fields: []
66+
});
67+
} catch (e:Error) {
68+
if (l == 0) throw e;
69+
trace(e.message);
70+
}
71+
});
72+
}
73+
74+
@:persistent var m = 0;
75+
function redefineModuleCatchError() {
76+
Context.onAfterInitMacros(() -> {
77+
if (m > 0) trace(Context.getType("Foobaz"));
78+
79+
try {
80+
m++;
81+
Context.defineModule("Foobaz", [{
82+
pos: Context.currentPos(),
83+
pack: [],
84+
name: "Foobaz",
85+
kind: TDClass(null, null, false, false, false),
86+
fields: []
87+
}]);
88+
} catch (e:Error) {
89+
if (m == 0) throw e;
90+
trace(e.message);
91+
}
92+
});
93+
}

tests/server/test/templates/issues/Issue12001/Macro1.hx

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import haxe.macro.CompilationServer;
22
import haxe.macro.Context;
3+
import haxe.macro.Expr.Error;
34

45
function hookInvalidateError() {
56
Context.onAfterTyping((_) -> {
@@ -11,8 +12,8 @@ function hookInvalidateCatch() {
1112
Context.onAfterTyping((_) -> {
1213
try {
1314
CompilationServer.invalidateModule("Empty");
14-
} catch (e:Dynamic) {
15-
Sys.println(Std.string(e));
15+
} catch (e:Error) {
16+
Sys.println(e.message);
1617
}
1718
});
1819
}

0 commit comments

Comments
 (0)