-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.hx
238 lines (182 loc) · 5.85 KB
/
test.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#! /usr/local/bin/hxp
import hxp.*;
/*
* HXP script (https://github.com/openfl/hxp) for easy crossplatform testing and benchmarks
* by Sylvio Sell - Rostock 2020
*
* install with:
*
* haxelib install hxp
* haxelib run hxp --install-hxp-alias
*
*/
class Test extends hxp.Script {
public function new() {
super();
var targets:Array<String>;
if (commandArgs.length == 0) targets = ['all'];
else targets = commandArgs.map(function(s) return s.toLowerCase());
Log.info(targets[0]);
// replace 'all' with all available targets
if (targets.indexOf('all') != -1) {
targets[targets.indexOf('all')] = 'neko';
targets = targets.concat(['hl','js','cpp']);
}
// remove double targets
targets = removeDoubles(targets);
switch (command) {
case "h"|"help":
Log.info("\nunit tests:");
Log.info(" 'hxp test <targets>'\n");
Log.info("performance benchmark:");
Log.info(" 'hxp bench <targets>' or 'hxp benchmark <targets>'\n");
Log.info("<targets> can be one or combination of: 'neko hl js cpp'");
Log.info(" or simple leave it empty for 'all' targets\n");
case "test":
test(targets);
case "bench" | "benchmark":
// fetch all benchmarks classnames from folder
var benchmarks = new Array<String>();
var benchFiles = System.readDirectory("benchmarks");
if (benchFiles != null) {
for (b in benchFiles) {
var r = ~/([^\/\\]+).hx$/;
if (r.match(b)) benchmarks.push(r.matched(1));
}
if (benchmarks.length != 0) benchmark(targets, benchmarks);
else Log.error("No .hx files into 'benchmarks' folder");
}
else Log.error("No 'benchmarks' directory found");
default:
Log.error("Expected \"hxp <test|(bench|benchmark)> <all|neko|hl|js|cpp>\"");
}
}
private function test (targets:Array<String>) {
Log.info("unit tests for targets: " + targets.join(", ") + "\n");
var base = new HXML ({
cp: [ "src", "unit-tests" ],
#if (haxe_ver >= "4.0.0")
libs: [ "hx3compat" ], // need for old unit-tests
#end
main: "Test",
dce: FULL,
//noInline: true,
debug: false
});
#if (haxe_ver >= "4.0.0")
base.define("no-deprecation-warnings"); // need for old unit-tests
#end
for (target in targets) {
Log.info("build " + target + " target...");
build(target, base);
}
for (target in targets) {
Log.info("\n------------ "+target.toUpperCase()+" ------------");
run(target, base);
}
}
private function benchmark (targets:Array<String>, benchmarks:Array<String>) {
Log.info("perform benchmark tests for targets: " + targets.join(", ") + "\n");
var base = new HXML ({
cp: [ "src", "benchmarks" ],
dce: FULL,
debug: false
});
for (benchmark in benchmarks)
{
Log.info("--------------------> " + benchmark + ":");
for (target in targets)
{
Log.info("build " + target + " target...");
base.main = benchmark;
if (benchmark == "FormulaVersusHscript") base.lib("hscript");
build(target, base);
}
for (target in targets)
{
Log.info("\n---" + target.toUpperCase()+" ---");
base.main = benchmark;
run(target, base);
}
Log.info("\n");
}
}
private function build (target:String, base:HXML) {
switch (target) {
case "neko":
var neko = base.clone();
neko.neko = 'bin/neko/${base.main.toLowerCase()}.n';
neko.build();
case "hl":
var hl = base.clone();
hl.hl = 'bin/hl/${base.main.toLowerCase()}.hl';
hl.build();
case "node"|"js":
var node = base.clone();
node.js = 'bin/node/${base.main.toLowerCase()}.js';
node.build();
case "cpp":
var cpp = base.clone();
cpp.cpp = "bin/cpp";
cpp.build();
default: Log.error ("Unknown target \"" + target + "\"");
}
}
private function run (target:String, base:HXML) {
switch (target) {
case "neko":
System.runCommand ("bin/neko", "neko", [ '${base.main.toLowerCase()}.n' ]);
case "hl":
var hlPath = Sys.getEnv ("HLPATH");
// try to use hl from haxelib lime if not hashlink installpath found
if (hlPath != null)
System.runCommand ("bin/hl", Path.combine (hlPath, "hl"), [ '${base.main.toLowerCase()}.hl' ]);
else
{
hlPath = Haxelib.getPath(new Haxelib("lime"));
if (hlPath != null)
{
hlPath = Path.combine (hlPath, "templates/bin/hl");
switch (System.hostPlatform) {
case LINUX: hlPath = Path.combine (hlPath, "Linux");
case WINDOWS: hlPath = Path.combine (hlPath, "Windows");
case MAC: hlPath = Path.combine (hlPath, "Mac");
}
if (System.hostArchitecture != X86) hlPath += "64";
//Log.info("Can't get path to hashlink binary ('HLPATH' env) so copying hashlink from Lime library: " + hlPath);
System.recursiveCopy(hlPath, "bin/hl");
if (System.hostPlatform == WINDOWS)
System.runCommand ("bin/hl", "hl.exe", [ '${base.main.toLowerCase()}.hl' ]);
else {
System.runCommand ("bin/hl", "chmod", [ 'u+x', 'hl' ]);
System.runCommand ("bin/hl", "./hl", [ '${base.main.toLowerCase()}.hl' ]);
}
}
else {
Log.warn("Can't find path to hashlink binary. Check environment variable 'HLPATH'\nor simple install Lime to use hashlink from!");
}
}
case "node"|"js":
System.runCommand ("bin/node", "node", [ '${base.main.toLowerCase()}.js' ]);
case "cpp":
if (System.hostPlatform == WINDOWS) {
System.runCommand ("bin/cpp", '${base.main}-debug.exe', []);
}
else System.runCommand ("bin/cpp", './${base.main}-debug', []);
default: Log.error ("Unknown target \"" + target + "\"");
}
}
private function removeDoubles(a:Array<String>) {
var i = a.length;
var j:Int;
while (i-- > 0) {
j = 0;
while (j < i)
if (a[j++] == a[i]) {
a.splice(i,1);
break;
}
}
return a;
}
}