-
-
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] add tests for @:buildOrder(Late) and @:buildOrder(Early)
- Loading branch information
Showing
8 changed files
with
202 additions
and
0 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,72 @@ | ||
import haxe.macro.Context; | ||
import haxe.macro.Expr; | ||
|
||
using StringTools; | ||
|
||
class Macro2 { | ||
static var id = 0; | ||
|
||
static function registerBuild(i:String, fields:Array<Field>) { | ||
if (Context.getLocalClass().get().isInterface) return null; | ||
|
||
var hasMacros = false; | ||
for (f in fields) { | ||
if (f.name == "macros") { | ||
hasMacros = true; | ||
break; | ||
} | ||
} | ||
|
||
if (!hasMacros) | ||
fields = (macro class A { | ||
public static var macros = []; | ||
}).fields.concat(fields); | ||
|
||
var id = '_' + id++; | ||
fields.push((macro class A { | ||
static var $id = {macros.push($v{i}); 0;}; | ||
}).fields[0]); | ||
|
||
return fields; | ||
} | ||
|
||
static function isAsync(f:Field):Bool { | ||
return Lambda.exists(f.meta, m -> m.name == ":async"); | ||
} | ||
|
||
@:buildOrder(Late) | ||
public static function buildTest() { | ||
var fields = haxe.macro.Context.getBuildFields(); | ||
var asyncArg = {name: "async", type: macro :Async}; | ||
|
||
// Add `async` arg to tests with `@:async` metadata | ||
for (f in fields) { | ||
if (!f.name.startsWith("test")) continue; | ||
|
||
switch f.kind { | ||
case FFun({args: [], ret: ret, expr: expr, params: []}) if (isAsync(f)): | ||
f.kind = FFun({args: [asyncArg], ret: ret, expr: expr, params: []}); | ||
|
||
case _: | ||
} | ||
} | ||
|
||
return registerBuild("Base Test", fields); | ||
} | ||
|
||
public static function autoAsync() { | ||
var fields = haxe.macro.Context.getBuildFields(); | ||
|
||
// Add `@:async` to all tests | ||
for (f in fields) { | ||
if (!f.name.startsWith("test")) continue; | ||
|
||
switch f.kind { | ||
case FFun(_): f.meta.push({name: ":async", params: [], pos: f.pos}); | ||
case _: | ||
} | ||
} | ||
|
||
return registerBuild("Auto async", fields); | ||
} | ||
} |
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,72 @@ | ||
import haxe.macro.Context; | ||
import haxe.macro.Expr; | ||
|
||
using StringTools; | ||
|
||
class Macro3 { | ||
static var id = 0; | ||
|
||
static function registerBuild(i:String, fields:Array<Field>) { | ||
if (Context.getLocalClass().get().isInterface) return null; | ||
|
||
var hasMacros = false; | ||
for (f in fields) { | ||
if (f.name == "macros") { | ||
hasMacros = true; | ||
break; | ||
} | ||
} | ||
|
||
if (!hasMacros) | ||
fields = (macro class A { | ||
public static var macros = []; | ||
}).fields.concat(fields); | ||
|
||
var id = '_' + id++; | ||
fields.push((macro class A { | ||
static var $id = {macros.push($v{i}); 0;}; | ||
}).fields[0]); | ||
|
||
return fields; | ||
} | ||
|
||
static function isAsync(f:Field):Bool { | ||
return Lambda.exists(f.meta, m -> m.name == ":async"); | ||
} | ||
|
||
public static function buildTest() { | ||
var fields = haxe.macro.Context.getBuildFields(); | ||
var asyncArg = {name: "async", type: macro :Async}; | ||
|
||
// Add `async` arg to tests with `@:async` metadata | ||
for (f in fields) { | ||
if (!f.name.startsWith("test")) continue; | ||
|
||
switch f.kind { | ||
case FFun({args: [], ret: ret, expr: expr, params: []}) if (isAsync(f)): | ||
f.kind = FFun({args: [asyncArg], ret: ret, expr: expr, params: []}); | ||
|
||
case _: | ||
} | ||
} | ||
|
||
return registerBuild("Base Test", fields); | ||
} | ||
|
||
@:buildOrder(Early) | ||
public static function autoAsync() { | ||
var fields = haxe.macro.Context.getBuildFields(); | ||
|
||
// Add `@:async` to all tests | ||
for (f in fields) { | ||
if (!f.name.startsWith("test")) continue; | ||
|
||
switch f.kind { | ||
case FFun(_): f.meta.push({name: ":async", params: [], pos: f.pos}); | ||
case _: | ||
} | ||
} | ||
|
||
return registerBuild("Auto async", fields); | ||
} | ||
} |
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,25 @@ | ||
class Main2 { | ||
static function main() { | ||
trace("TestFoo", TestFoo.macros); | ||
trace("TestBar", TestBar.macros); | ||
} | ||
} | ||
|
||
class Async { | ||
public function done() {} | ||
} | ||
|
||
@:autoBuild(Macro2.buildTest()) | ||
class BaseTest {} | ||
|
||
class TestFoo extends BaseTest { | ||
@:async | ||
function test() async.done(); | ||
} | ||
|
||
@:autoBuild(Macro2.autoAsync()) | ||
class AsyncTest extends BaseTest {} | ||
|
||
class TestBar extends AsyncTest { | ||
function test() async.done(); | ||
} |
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,25 @@ | ||
class Main3 { | ||
static function main() { | ||
trace("TestFoo", TestFoo.macros); | ||
trace("TestBar", TestBar.macros); | ||
} | ||
} | ||
|
||
class Async { | ||
public function done() {} | ||
} | ||
|
||
@:autoBuild(Macro3.buildTest()) | ||
class BaseTest {} | ||
|
||
class TestFoo extends BaseTest { | ||
@:async | ||
function test() async.done(); | ||
} | ||
|
||
@:autoBuild(Macro3.autoAsync()) | ||
class AsyncTest extends BaseTest {} | ||
|
||
class TestBar extends AsyncTest { | ||
function test() async.done(); | ||
} |
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 @@ | ||
-main Main3 | ||
--interp |
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 @@ | ||
Main3.hx:3: TestFoo,[Base Test] | ||
Main3.hx:4: TestBar,[Auto async,Base Test] |
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 @@ | ||
-main Main2 | ||
--interp |
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 @@ | ||
Main2.hx:3: TestFoo,[Base Test] | ||
Main2.hx:4: TestBar,[Auto async,Base Test] |