Skip to content

Commit

Permalink
[tests] Compilation server tests (#11159): add Context.getType vs Con…
Browse files Browse the repository at this point in the history
…text.resolveType variants
  • Loading branch information
kLabz committed Jun 25, 2024
1 parent f283deb commit 02d27c1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
15 changes: 10 additions & 5 deletions tests/server/src/cases/CsSafeTypeBuilding.hx
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,15 @@ class CsSafeTypeBuilding extends TestCase {
}
}

@:variant("JsDefineModule", true, "js")
@:variant("JsDefineType", false, "js")
@:variant("InterpDefineModule", true, "interp")
@:variant("InterpDefineType", false, "interp")
function test(defineModule:Bool, target:String) {
@:variant("Js_DefineModule_GetType", true, true, "js")
@:variant("Js_DefineType_GetType", false, true, "js")
@:variant("Js_DefineModule_ResolveType", true, false, "js")
@:variant("Js_DefineType_ResolveType", false, false, "js")
@:variant("Interp_DefineModule_GetType", true, true, "interp")
@:variant("Interp_DefineType_GetType", false, true, "interp")
@:variant("Interp_DefineModule_ResolveType", true, false, "interp")
@:variant("Interp_DefineType_ResolveType", false, false, "interp")
function test(defineModule:Bool, getType:Bool, target:String) {
var targetArgs = switch target {
case "js": ["-js", "out.js", "-lib", "hxnodejs", "-cmd", "node out.js"];
case "interp": ["--interp"];
Expand All @@ -104,6 +108,7 @@ class CsSafeTypeBuilding extends TestCase {

var args = ["-main", "Main", "Baz"];
if (defineModule) args = args.concat(["-D", "config.defineModule"]);
if (getType) args = args.concat(["-D", "config.getType"]);
args = args.concat(targetArgs);

runHaxe(args);
Expand Down
16 changes: 16 additions & 0 deletions tests/server/test/templates/csSafeTypeBuilding/Macro.macro.hx
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,23 @@ class Macro {

@:persistent static var generated = new Map<String, Bool>();

#if config.getType
static function isAlive(name:String):Bool {
// Null check is just there to make it a one liner
// Basically returning true if no exception is caught
return try Context.getType(name) != null
catch(s:String) {
if (s != 'Type not found \'$name\'') throw s;
false;
};
}
#else
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;
}
#end

public static function buildFoo() {
var from = '[${Context.getLocalModule()}] ';
Expand All @@ -29,7 +41,11 @@ class Macro {
var ct = TPath({pack: [], name: key});

if (generated.exists(key)) {
#if config.getType
if (isAlive(key)) {
#else
if (isAlive(ct, pos)) {
#end
print('Reusing previously generated type for $key.');
return ct;
}
Expand Down

0 comments on commit 02d27c1

Please sign in to comment.