-
Notifications
You must be signed in to change notification settings - Fork 12
/
test_spec.js
206 lines (182 loc) · 5.31 KB
/
test_spec.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
var peg = require("pegjs");
var gen = require("escodegen");
var fs = require("fs");
var vm = require("vm");
var helpers = fs.readFileSync("helpers.js").toString();
var lang = fs.readFileSync("lua.pegjs").toString();
var parser = peg.generate(helpers + lang);
var exec = require('child_process').exec;
function leval(src) {
var AST;
try {
AST = parser.parse(src, {forceVar: true, decorateLuaObjects: true, luaCalls: true, luaOperators: true, encloseWithFunctions: false });
} catch ( e ) {
return JSON.stringify(e);
}
return function(code) {
//console.log(code);
return function() { return "(function() " + code + ")()"; }
}(gen.generate(AST));
}
tests = [
[" return 10" , 10],
["return 10" , 10],
["return 20 ", 20],
[
['if true then',
' return "yum"',
'else ',
' return "ouch"',
'end'], "yum"
],
['if 0 then return "yum" else return "ouch" end', "ouch" ],
['add = function(a,b) return a+b end return add(2,5)', 7],
['function add(a,b) return a+b end return add(2,5)', 7],
[
[
'function fastfib(n)',
'fibs = {1,1}',
'local i = 2',
'while i < n do',
' i = i + 1',
' fibs[i] = fibs[i-1] + fibs[i-2]',
'end',
'return fibs[n]',
'end',
'return fastfib(10)'
], 55
],
[
[
'local total = 0',
'for i=1,20,3 do total = total + i end',
'return total'
], 70
],
[
[
'eval("var a = [1,2,3]")',
'return a[1]'
],1
],
[
[
'eval("var a = [1,2,3]")',
'a[2] = 7',
'return eval("a.toString()")'
],"1,7,3"
],
[
[
'eval("var ct = function(w) { return \'!\' + w + \'!\' + (typeof w); }")',
'function add(a,b) return 7 + a + b, b - a end',
'local sum = add(2,3)',
'return ct(sum)',
],"!12!number"
],
[
[
'function add(a,b) return a*a+b*b,0 end',
'local o = add(3,4)',
'return ("a" .. o .. add(5,12))'
], 'a25169'
],
[
[
'eval("var json = function(w) { return JSON.stringify(w); }")',
'function g() return {x=1, y=20} end',
'o = g()',
'return json(o) .. " " .. eval("(function(x) { return x.__luaType; })(o)")',
],'{"x":1,"y":20} table'
],
[
[
'eval("var array = [1,1,2,5,7];")',
'return array[4]'
],5
],
[
[
'eval("var array = [1,1,2,5,7];")',
'out = "pairs"',
'for k,v in pairs(array) do out = out .. " " .. k .. "," .. v end',
'return out'
],'pairs 1,1 2,1 3,2 4,5 5,7'
],
[
[
'eval("var array = [1,1,2,5,7];")',
'out = "ipairs"',
'for k,v in ipairs(array) do out = out .. " " .. k .. "," .. v end',
'return out'
],'ipairs 1,1 2,1 3,2 4,5 5,7'
],
[
['while false do end'],
void 0
]
];
function makenv() {
var goodies = require("./public/stdlib.js");
return (function(stdout) {
var env = {};
for ( var i in goodies ) env[i] = goodies[i];
env.getStdOut = function() { return stdout; }
env.io = {
write: function() {
Array.prototype.forEach.call(arguments, function(s,i) {
stdout = stdout + s;
});
}
};
env.print = function() {
var s = Array.prototype.slice.call(arguments)
.map(function(x) { return env.tostring(x); })
.join("\t");
env.io.write(s + "\n");
//console.log(s);
};
env.__lua.mark(env);
return env;
})("");
}
describe("Simple Tests", function() {
var idx = 0;
tests.forEach(function(nfo) {
var code = nfo[0];
var result = nfo[1]
it("testSimple" + idx++, function(done) {
if ( typeof(code) != "string" ) code = code.join("\n");
var v = leval(code);
expect(typeof(v)).toEqual("function");
var env = makenv();
var a = vm.runInNewContext(v(), makenv(), "vm");
expect(a).toEqual(result);
done();
});
});
});
function testDirectory(dir) {
return function() {
fs.readdirSync(dir).forEach(function(f) {
it(dir + '/' + f, function(done) {
var code = fs.readFileSync(dir + '/' + f).toString();
//console.log("Start " + f);
var v = leval(code);
if ( typeof(v) != "function" ) throw v;
(function(env) {
var a = vm.runInNewContext(v(), env, "vm");
exec('lua ' + dir + '/' + f, function(err, stdout, stderr) {
if ( stderr !== "" ) expect(false).toBe(true);
expect(env.getStdOut()).toEqual(stdout.replace(/\r\n/g,"\n"));
done();
});
})(makenv());
//console.log("End " + f);
});
});
};
}
describe("Lua Tests", testDirectory('./lua-tests'));
describe("Lua More Tests", testDirectory('./lua-testmore'));
describe("Lua Lang ToolKit", testDirectory('./lua-lltk'));