forked from PaulBernier/castl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runTests.js
executable file
·216 lines (184 loc) · 6.42 KB
/
runTests.js
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
#!/usr/bin/env node
/*
Copyright (c) 2014, Paul Bernier
CASTL is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
CASTL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with CASTL. If not, see <http://www.gnu.org/licenses/>.
*/
(function () {
"use strict";
process.env['LUA_PATH'] = process.env['LUA_PATH'] + ";/usr/local/lib/node_modules/castl/lua/?.lua;/usr/lib/node_modules/castl/lua/?.lua;";
var fs = require("fs");
var path = require("path");
var execSync = require('child_process').execSync;
var castl = require("./castl.js");
/*********************************
*
* handle parameters and options
*
* *******************************/
var program = require('commander');
program
.option('--jit', 'Test using LuaJIT')
.option('--acorn', 'Test using Acorn')
.parse(process.argv);
var parserName = program.acorn ? "acorn" : "esprima";
var parser = require(parserName);
// Parser options
var parserOptions = {};
if (parserName === "esprima") {
parserOptions.loc = true;
} else if (parserName === "acorn") {
parserOptions.locations = true;
}
// Castl options
var castlOptions = {
jit: program.jit,
evalMode: false,
debug: true,
heuristic: false
};
/**************************
*
* Get list of test files
*
* ************************/
function addJsFilesFromFolder(filesList, folderPath) {
var dirfiles = fs.readdirSync(folderPath);
for (i = 0; i < dirfiles.length; ++i) {
file = dirfiles[i];
if (path.extname(file) === ".js") {
filesList.push(folderPath + file);
}
}
}
var i, file;
var files = [];
if (program.args.length > 0) {
files = program.args;
} else {
addJsFilesFromFolder(files,"./test/");
addJsFilesFromFolder(files,"./test/issues/");
}
/**************************
*
* Test each file
*
* ************************/
// Padding functions
function padRight(n, width) {
n = n + '';
return n.length >= width ? n : n + new Array(width - n.length + 1).join(" ");
}
function padLeft(str, padding) {
return new Array(padding).join(" ") + str;
}
var total = files.length;
var width = (total + '').length + 2;
var offset = 60;
var code, testHeader, failed = [];
for (i = 0; i < total; ++i) {
file = files[i];
testHeader = padRight((i + 1) + ".", width) + file;
process.stdout.write(testHeader);
// 1. Execute js file
code = execSync("nodejs " + file);
if (code > 0) {
console.log("--> Compilation of js code failed!");
failed.push({
src: file,
code: 1
});
continue;
}
// 2. Compile JS to Lua
try {
compileTest(file);
} catch (e) {
console.log(e.stack);
console.log("--> Compilation of js code failed!");
failed.push({
src: file,
code: 2
});
continue;
}
// 3. Execute lua code compiled
if (program.jit) {
code = execSync("luajit " + file + ".lua");
} else {
code = execSync("lua5.2 " + file + ".lua");
}
if (code > 0) {
console.log("--> Execution of lua code failed!");
failed.push({
src: file,
code: 3
});
continue;
}
console.log(padLeft("--> Everything Is AWESOME!!!", offset - testHeader.length));
}
function compileTest(filename) {
var data = fs.readFileSync(filename, 'utf8');
var position = data.indexOf('\n');
if (position !== -1) {
var firstLine = data.substr(0, position).trim();
if (firstLine === "var assert = require('assert');" ||
firstLine === "var assert = require(\"assert\");") {
// Skip first line
data = data.replace(/^[^\n]*\n/, "\n");
var syntax = "";
try {
syntax = parser.parse(data, parserOptions);
} catch (e) {
throw new SyntaxError("Couldn't parse JS code");
}
var compiledCode = castl.compileAST(syntax, castlOptions).compiled;
compiledCode = compiledCode.replace(/assert\(_ENV,/g, "assert(");
var finalCode = ["local assert, print = assert, print"];
// Set environment
if (program.jit) {
finalCode.push("local _ENV = require(\"castl.runtime\");");
finalCode.push("return setfenv(function(...)");
finalCode.push(compiledCode);
finalCode.push("end, _ENV)();");
} else {
finalCode.push("local _ENV = require(\"castl.runtime\");");
finalCode.push(compiledCode);
}
fs.writeFileSync(filename + ".lua", finalCode.join("\n"));
} else {
throw new Error("First line of file must be 'var assert = require('assert');' ");
}
} else {
throw new Error("No lines in the file");
}
}
/**************************
*
* Display result
*
* ************************/
console.log("----------------------------------------------");
if (program.jit) {
console.log("Report (LuaJIT):");
} else {
console.log("Report (Lua 5.2):");
}
var success = total - failed.length;
console.log("Passed: " + success + "/" + total + "\n");
if (success !== total) {
console.log("Failed: " + failed.length);
failed.forEach(function (test) {
console.log("-" + test.src + " - Code: " + test.code);
});
}
}());