浏览代码

[tests] add test using CompilationServer.invalidateModule

Rudy Ges 7 月之前
父节点
当前提交
4c52d71734

+ 38 - 0
tests/server/src/cases/issues/Issue12001.hx

@@ -1,5 +1,7 @@
 package cases.issues;
 
+import utest.Async;
+
 class Issue12001 extends TestCase {
 	function testDefineType(_) {
 		vfs.putContent("Macro.hx", getTemplate("issues/Issue12001/Macro.hx"));
@@ -50,6 +52,24 @@ class Issue12001 extends TestCase {
 		assertErrorMessage("Cannot redefine module Bar");
 	}
 
+	@:async
+	@:timeout(3000)
+	function testRedefineModule(async:Async) {
+		vfs.putContent("Macro.hx", getTemplate("issues/Issue12001/Macro.hx"));
+		vfs.putContent("Main.hx", getTemplate("issues/Issue12001/Main2.hx"));
+		var args = ["-main", "Main", "--interp", "--macro", "Macro.redefineModule()"];
+		var i = 0;
+		function test() {
+			runHaxe(args, () -> {
+				assertSuccess();
+				assertHasPrint("Foobar.test() = " + i);
+				if (++i >= 5) async.done();
+				else test();
+			});
+		}
+		test();
+	}
+
 	function testAfterTyping(_) {
 		vfs.putContent("Macro.hx", getTemplate("issues/Issue12001/Macro.hx"));
 		vfs.putContent("Empty.hx", getTemplate("Empty.hx"));
@@ -61,4 +81,22 @@ class Issue12001 extends TestCase {
 		Assert.isFalse(0 == errorMessages.length);
 		assertErrorMessage("Cannot redefine module Baz");
 	}
+
+	@:async
+	@:timeout(3000)
+	function testRedefineAfterTyping(async:Async) {
+		vfs.putContent("Macro.hx", getTemplate("issues/Issue12001/Macro.hx"));
+		vfs.putContent("Empty.hx", getTemplate("Empty.hx"));
+		var args = ["-main", "Empty", "--interp", "--macro", "Macro.hookRedefine()"];
+		var i = 0;
+		function test() {
+			runHaxe(args, () -> {
+				assertSuccess();
+				assertHasPrint("Foobaz.test() = " + i);
+				if (++i >= 5) async.done();
+				else test();
+			});
+		}
+		test();
+	}
 }

+ 38 - 1
tests/server/test/templates/issues/Issue12001/Macro.hx

@@ -1,3 +1,4 @@
+import haxe.macro.CompilationServer;
 import haxe.macro.Context;
 
 function defineType() {
@@ -28,6 +29,23 @@ function defineModule() {
 	});
 }
 
+@:persistent var i = 0;
+function redefineModule() {
+	Context.onAfterInitMacros(() -> {
+		CompilationServer.invalidateModule("Foobar");
+
+		Context.defineModule("Foobar", [{
+			pos: Context.currentPos(),
+			pack: [],
+			name: "Foobar",
+			kind: TDClass(null, null, false, false, false),
+			fields: (macro class Foobar {
+				public static function test() Sys.println("Foobar.test() = " + $v{i++});
+			}).fields
+		}]);
+	});
+}
+
 function hook() {
 	var generated = false;
 	Context.onAfterTyping((_) -> {
@@ -44,6 +62,25 @@ function hook() {
 			}).fields
 		}]);
 	});
+}
 
-	return null;
+@:persistent var j = 0;
+function hookRedefine() {
+	var generated = false;
+	Context.onAfterTyping((_) -> {
+		if (generated) return;
+		generated = true;
+
+		CompilationServer.invalidateModule("Foobaz");
+
+		Context.defineModule("Foobaz", [{
+			pos: Context.currentPos(),
+			pack: [],
+			name: "Foobaz",
+			kind: TDClass(null, null, false, false, false),
+			fields: (macro class Foobaz {
+				public static function __init__() Sys.println("Foobaz.test() = " + $v{j++});
+			}).fields
+		}]);
+	});
 }

+ 3 - 0
tests/server/test/templates/issues/Issue12001/Main2.hx

@@ -0,0 +1,3 @@
+function main() {
+	Foobar.test();
+}