-
-
Notifications
You must be signed in to change notification settings - Fork 657
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[tests] Compilation server vs defineModule/defineType (#11159)
* [tests] server tests - basic support for @:variant * [tests] add server test for compilation server safe type building * [tests] server tests - error handling for @:variant * [tests] add debug info * [tests] temp change of build params * [tests] add more debug data * [tests] better debug data on failures * [tests] reenable other server tests
- Loading branch information
Showing
7 changed files
with
333 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
package cases; | ||
|
||
import haxe.display.Display; | ||
import haxe.display.FsPath; | ||
import haxe.display.Server; | ||
import utest.Assert; | ||
|
||
using StringTools; | ||
using Lambda; | ||
|
||
class CsSafeTypeBuilding extends TestCase { | ||
var originalContent:String; | ||
|
||
override public function setup(async:utest.Async) { | ||
super.setup(async); | ||
|
||
originalContent = ""; | ||
vfs.putContent("Bar.hx", getTemplate("csSafeTypeBuilding/Bar.hx")); | ||
vfs.putContent("Baz.hx", getTemplate("csSafeTypeBuilding/Baz.hx")); | ||
vfs.putContent("Foo.hx", getTemplate("csSafeTypeBuilding/Foo.hx")); | ||
vfs.putContent("Macro.macro.hx", getTemplate("csSafeTypeBuilding/Macro.macro.hx")); | ||
vfs.putContent("Main.hx", getTemplate("csSafeTypeBuilding/Main.hx")); | ||
} | ||
|
||
#if debug | ||
var failed:Bool; | ||
function _assertHasPrint(s:String, ?pos:haxe.PosInfos) { | ||
if (!assertHasPrint(s)) { | ||
failed = true; | ||
haxe.Log.trace("Fail: doesn't contain \"" + s + "\"", pos); | ||
} | ||
} | ||
#end | ||
|
||
function assertResult(target:String) { | ||
#if debug | ||
failed = false; | ||
var assertHasPrint = _assertHasPrint; | ||
#end | ||
assertSuccess(); | ||
|
||
// Make sure all types are generated | ||
assertHasPrint("[runtime] Hello from Bar"); | ||
assertHasPrint("[runtime] Hello from Baz"); | ||
assertHasPrint("[runtime] Hello from Foo__Bar__Bar"); | ||
assertHasPrint("[runtime] Hello from Foo__Baz__Baz"); | ||
assertHasPrint("[runtime] Hello from Foo__Main__Main"); | ||
assertHasPrint("[runtime] Hello from Main"); | ||
|
||
#if debug | ||
if (failed) messages.filter(m -> StringTools.startsWith(m, "Haxe print: ")).iter(m -> trace(m)); | ||
#end | ||
|
||
// Disabled this check because types move around a bit so we get false negatives | ||
// Kept for debugging purposes | ||
if (false && target == "js") { | ||
var content = sys.io.File.getContent(haxe.io.Path.join([testDir, "out.js"])); | ||
Assert.isTrue(content == originalContent); | ||
|
||
// Needs https://github.com/kLabz/hxdiff for displaying diff | ||
// if (content != originalContent) { | ||
// final a = new diff.FileData(haxe.io.Bytes.ofString(originalContent), "expected", Date.now()); | ||
// final b = new diff.FileData(haxe.io.Bytes.ofString(content), "actual", Date.now()); | ||
// var ctx:diff.Context = { | ||
// file1: a, | ||
// file2: b, | ||
// context: 10 | ||
// } | ||
|
||
// final script = diff.Analyze.diff2Files(ctx); | ||
// var diff = diff.Printer.printUnidiff(ctx, script); | ||
// Sys.println(diff); | ||
// } | ||
} | ||
} | ||
|
||
function assertBuilt(modules:Array<String>, ?macroInvalidated:Bool = false) { | ||
#if debug trace('Invalidated ${modules.join(",")} (macro invalidated: ${macroInvalidated ? "true" : "false"})'); #end | ||
#if debug var assertHasPrint = _assertHasPrint; #end | ||
|
||
for (m in modules) { | ||
assertHasPrint('Building $m.'); | ||
|
||
var t = 'Foo__${m}__${m}'; | ||
if (!macroInvalidated) assertHasPrint('[$m] Previously generated type for $t has been discarded.'); | ||
assertHasPrint('[$m] Generating type for $t.'); | ||
|
||
if (m == "Baz") { | ||
assertHasPrint('[$m] Reusing previously generated type for Foo__Bar__Bar.'); | ||
} | ||
} | ||
} | ||
|
||
@:variant("JsDefineModule", true, "js") | ||
@:variant("JsDefineType", false, "js") | ||
@:variant("InterpDefineModule", true, "interp") | ||
@:variant("InterpDefineType", false, "interp") | ||
function test(defineModule:Bool, target:String) { | ||
var targetArgs = switch target { | ||
case "js": ["-js", "out.js", "-lib", "hxnodejs", "-cmd", "node out.js"]; | ||
case "interp": ["--interp"]; | ||
case _: []; | ||
} | ||
|
||
var args = ["-main", "Main", "Baz"]; | ||
if (defineModule) args = args.concat(["-D", "config.defineModule"]); | ||
args = args.concat(targetArgs); | ||
|
||
runHaxe(args); | ||
if (target == "js") originalContent = sys.io.File.getContent(haxe.io.Path.join([testDir, "out.js"])); | ||
assertBuilt(["Main", "Bar", "Baz"], true); | ||
assertResult(target); | ||
|
||
#if debug trace("Rerun without invalidate"); #end | ||
runHaxe(args); | ||
assertResult(target); | ||
|
||
runHaxeJson([], ServerMethods.Invalidate, {file: new FsPath("Baz.hx")}); | ||
runHaxe(args); | ||
assertBuilt(["Baz"]); | ||
assertResult(target); | ||
|
||
runHaxeJson([], ServerMethods.Invalidate, {file: new FsPath("Main.hx")}); | ||
runHaxe(args); | ||
assertBuilt(["Main"]); | ||
assertResult(target); | ||
|
||
runHaxeJson([], ServerMethods.Invalidate, {file: new FsPath("Bar.hx")}); | ||
runHaxeJson([], ServerMethods.Invalidate, {file: new FsPath("Main.hx")}); | ||
runHaxe(args); | ||
assertBuilt(["Main", "Bar"]); | ||
assertResult(target); | ||
|
||
runHaxeJson([], ServerMethods.Invalidate, {file: new FsPath("Bar.hx")}); | ||
runHaxe(args); | ||
assertBuilt(["Main", "Bar", "Baz"]); | ||
assertResult(target); | ||
|
||
runHaxeJson([], ServerMethods.Invalidate, {file: new FsPath("Foo.hx")}); | ||
runHaxe(args); | ||
assertBuilt(["Main", "Bar", "Baz"]); | ||
assertResult(target); | ||
|
||
runHaxeJson([], ServerMethods.Invalidate, {file: new FsPath("Macro.macro.hx")}); | ||
runHaxe(args); | ||
assertBuilt(["Main", "Bar", "Baz"], true); | ||
assertResult(target); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#if !macro @:build(Macro.logBuild()) #end | ||
class Bar { | ||
static function __init__() Sys.println("[runtime] Hello from Bar"); | ||
} | ||
|
||
typedef B = Foo<Bar>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#if !macro @:build(Macro.logBuild()) #end | ||
class Baz { | ||
static function __init__() Sys.println("[runtime] Hello from Baz"); | ||
} | ||
|
||
typedef AA = Foo<Bar>; | ||
typedef BB = Foo<Baz>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#if !macro @:genericBuild(Macro.buildFoo()) #end | ||
class Foo<T> {} |
63 changes: 63 additions & 0 deletions
63
tests/server/test/templates/csSafeTypeBuilding/Macro.macro.hx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import haxe.macro.Context; | ||
import haxe.macro.Expr; | ||
import haxe.macro.Type; | ||
import haxe.macro.TypeTools; | ||
|
||
class Macro { | ||
public static function logBuild() { | ||
Sys.println('Building ${Context.getLocalClass().toString()}.'); | ||
return null; | ||
} | ||
|
||
@:persistent static var generated = new Map<String, Bool>(); | ||
|
||
static function isAlive(ct:ComplexType, pos:Position):Bool { | ||
// Null check is just there to make it a one liner | ||
// Basically returning true if no exception is caught | ||
return try Context.resolveType(ct, pos) != null catch(e) false; | ||
} | ||
|
||
public static function buildFoo() { | ||
var from = '[${Context.getLocalModule()}] '; | ||
var print = s -> Sys.println(from + s); | ||
|
||
switch (Context.getLocalType()) { | ||
case TInst(_, [target]): | ||
var pos = Context.currentPos(); | ||
var bt = TypeTools.toBaseType(target); | ||
var key = ["Foo", bt.module, bt.name].join("__"); | ||
var ct = TPath({pack: [], name: key}); | ||
|
||
if (generated.exists(key)) { | ||
if (isAlive(ct, pos)) { | ||
print('Reusing previously generated type for $key.'); | ||
return ct; | ||
} | ||
|
||
print('Previously generated type for $key has been discarded.'); | ||
} | ||
|
||
var genDef = macro class $key { | ||
static function __init__() Sys.println("[runtime] Hello from " + $v{key}); | ||
}; | ||
|
||
// Not really needed but nicer | ||
// genDef.pos = pos; | ||
|
||
// Not needed unless dce full | ||
// genDef.meta.push({name: ":keep", params: [], pos: pos}); | ||
|
||
print('Generating type for $key.'); | ||
#if config.defineModule | ||
Context.defineModule(key, [genDef]); | ||
#else | ||
Context.defineType(genDef, bt.module); | ||
#end | ||
|
||
generated.set(key, true); | ||
return ct; | ||
|
||
case _: throw ""; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Create a dependency to Bar | ||
import Bar; | ||
|
||
typedef A = Foo<Main>; | ||
|
||
#if !macro @:build(Macro.logBuild()) #end | ||
class Main { | ||
static function main() Sys.println("[runtime] Hello from Main"); | ||
} |