/
Macro.hx
77 lines (72 loc) · 2.06 KB
/
Macro.hx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import haxe.macro.Context;
import haxe.macro.Expr;
import haxe.io.Path;
using sys.FileSystem;
using Lambda;
using StringTools;
class Macro {
#if macro
static function buildTestCase():Array<Field> {
var fields = Context.getBuildFields();
var c = Context.getLocalClass().get();
for (field in fields) {
if (field.doc == null) {
continue;
}
var doc = (c.pack.length > 0 ? "package " + c.pack.join(".") + ";\n" : "");
if (field.meta.exists(function(meta) return meta.name == ":funcCode")) {
doc += "class Main { static function main() { " + field.doc + "}}";
} else {
doc += field.doc;
}
doc = StringTools.replace(doc, "**\\/", "**/");
var transform = Marker.extractMarkers(doc);
var markers = transform.markers.length > 0 ? macro $a{transform.markers} : macro new Map();
var filename = Context.getPosInfos(c.pos).file;
for (meta in field.meta) {
if (meta.name == ":filename") {
switch (meta.params[0].expr) {
case EConst(CString(s)):
filename = Path.directory(filename) + "/" + s;
case _:
throw "String expected";
}
}
}
switch (field.kind) {
case FFun(f) if (f.expr != null):
f.expr = macro @:pos(f.expr.pos) {
ctx = new DisplayTestContext($v{filename}, $v{field.name}, $v{transform.source}, $markers);
${f.expr}
};
case _:
}
}
return fields;
}
#end
macro static public function getCases(pack:String) {
var cases = [];
var singleCase = haxe.macro.Context.definedValue("test");
function loop(pack:Array<String>) {
var path = Context.resolvePath(Path.join(pack));
for (file in sys.FileSystem.readDirectory(path)) {
if (singleCase != null && !file.endsWith(singleCase + ".hx")) {
continue;
}
if (file.endsWith("import.hx")) {
continue;
}
var p = new haxe.io.Path(file);
if (p.ext == "hx") {
var tp = {pack: pack, name: p.file};
cases.push(macro new $tp());
} else if (Path.join([path, file]).isDirectory()) {
loop(pack.concat([file]));
}
}
}
loop(pack.split('.'));
return macro $a{cases};
}
}