Skip to content

Commit 125fc32

Browse files
committed
Moved Test functionality to util module & Added tests for loops
1 parent 1de303b commit 125fc32

6 files changed

Lines changed: 194 additions & 111 deletions

File tree

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"coverage": "nyc --reporter=lcov npm test && nyc report"
1010
},
1111
"devDependencies": {
12+
"deep-equal": "^1.0.1",
1213
"nyc": "^11.4.1"
1314
}
1415
}

test/integration/lua/loops.spec.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { Expect, Test, TestCase } from "alsatian";
2+
import * as util from "../../src/util"
3+
4+
const deepEqual = require('deep-equal')
5+
6+
export class LuaLoopTests {
7+
8+
@TestCase([0, 1, 2, 3], [1, 2, 3, 4])
9+
@Test("for")
10+
public for<T>(inp: T[], expected: T[]) {
11+
// Transpile
12+
let lua = util.transpileString(
13+
`let arrTest = [${inp.toString()}];
14+
for (let i = 0; i < arrTest.length; ++i) {
15+
arrTest[i] = arrTest[i] + 1;
16+
}
17+
return ArrayToString(arrTest);`
18+
, util.dummyTypes.Array
19+
);
20+
21+
// Execute
22+
let result = util.executeLua(lua);
23+
24+
// Assert
25+
Expect(result).toBe(expected.toString());
26+
}
27+
28+
@TestCase({ ['test1']: 0, ['test2']: 1, ['test3']: 2 }, { ['test1']: 1, ['test2']: 2, ['test3']: 3 })
29+
@Test("forin")
30+
public forin<T>(inp: any, expected: any) {
31+
// Transpile
32+
let lua = util.transpileString(
33+
`let objTest = ${JSON.stringify(inp)};
34+
for (let key in objTest) {
35+
objTest[key] = objTest[key] + 1;
36+
}
37+
return JSONStringify(objTest);`
38+
, util.dummyTypes.Object
39+
);
40+
41+
// Execute
42+
let result = util.executeLua(lua);
43+
44+
// Assert
45+
Expect(deepEqual(JSON.parse(result), expected)).toBe(true);
46+
}
47+
48+
@TestCase([0,1,2], [1,2,3])
49+
@Test("forof")
50+
public forof<T>(inp: any, expected: any) {
51+
// Transpile
52+
let lua = util.transpileString(
53+
`let objTest = [${inp.toString()}];
54+
let arrResultTest = {};
55+
for (let value of objTest) {
56+
arrResultTest.push(value + 1)
57+
}
58+
return ArrayToString(arrResultTest);`
59+
, util.dummyTypes.Array
60+
);
61+
62+
// Execute
63+
let result = util.executeLua(lua);
64+
65+
// Assert
66+
Expect(result).toBe(expected.toString());
67+
}
68+
}

test/integration/lua/lualib.spec.ts

Lines changed: 50 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,195 +1,135 @@
11
import { Expect, Test, TestCase } from "alsatian";
2+
import * as util from "../../src/util"
23

3-
import * as ts from "typescript";
4-
import {LuaTranspiler, TranspileError} from "../../../dist/Transpiler";
5-
6-
const LuaVM = require("lua.vm.js");
7-
const fs = require("fs");
8-
9-
const dummyArrayType = { flags: ts.TypeFlags.Object, symbol: {escapedName: "Array"}};
10-
let dummyType = {};
11-
const dummyChecker = {getTypeAtLocation: function() {return dummyType;}}
12-
function transpileString(str: string): string {
13-
const file = ts.createSourceFile("", str, ts.ScriptTarget.Latest);
14-
const result = LuaTranspiler.transpileSourceFile(file, dummyChecker, false);
15-
return result.trim();
16-
}
17-
function executeLua(lua: string): string {
18-
const luavm = new LuaVM.Lua.State();
19-
return luavm.execute(lua)[0];
20-
}
21-
22-
const lualib = fs.readFileSync("dist/lualib/typescript.lua") + "\n";
23-
24-
const toStringDef = "function ToString(list)\n"+
25-
"local result = \"\"\n" +
26-
"for i=1,#list do result = result .. list[i]\n" +
27-
"if i < #list then result = result .. ',' end end\n"+
28-
"return result end\n";
29-
30-
export class LuaTests {
4+
export class LuaLibArrayTests {
315

326
@TestCase([], "x => x")
33-
@TestCase([0,1,2,3], "x => x")
34-
@TestCase([0,1,2,3], "x => x*2")
35-
@TestCase([1,2,3,4], "x => -x")
36-
@TestCase([0,1,2,3], "x => x+2")
37-
@TestCase([0,1,2,3], "x => x%2 == 0 ? x + 1 : x - 1")
7+
@TestCase([0, 1, 2, 3], "x => x")
8+
@TestCase([0, 1, 2, 3], "x => x*2")
9+
@TestCase([1, 2, 3, 4], "x => -x")
10+
@TestCase([0, 1, 2, 3], "x => x+2")
11+
@TestCase([0, 1, 2, 3], "x => x%2 == 0 ? x + 1 : x - 1")
3812
@Test("array.map")
3913
public map<T>(inp: T[], func: string) {
40-
// Make typechecker return array type
41-
dummyType = dummyArrayType;
4214
// Transpile
43-
let lua = transpileString(`return ToString([${inp.toString()}].map(${func}))`);
44-
45-
// Add library
46-
lua = toStringDef + lualib + lua;
15+
let lua = util.transpileString(`return ArrayToString([${inp.toString()}].map(${func}))`, util.dummyTypes.Array);
4716

4817
// Execute
49-
let result = executeLua(lua);
18+
let result = util.executeLua(lua);
5019

5120
// Assert
5221
Expect(result).toBe(inp.map(eval(func)).toString());
5322
}
5423

5524
@TestCase([], "x => x > 1")
56-
@TestCase([0,1,2,3], "x => x > 1")
57-
@TestCase([0,1,2,3], "x => x < 3")
58-
@TestCase([0,1,2,3], "x => x < 0")
59-
@TestCase([0,-1,-2,-3], "x => x < 0")
60-
@TestCase([0,1,2,3], "() => true")
61-
@TestCase([0,1,2,3], "() => false")
25+
@TestCase([0, 1, 2, 3], "x => x > 1")
26+
@TestCase([0, 1, 2, 3], "x => x < 3")
27+
@TestCase([0, 1, 2, 3], "x => x < 0")
28+
@TestCase([0, -1, -2, -3], "x => x < 0")
29+
@TestCase([0, 1, 2, 3], "() => true")
30+
@TestCase([0, 1, 2, 3], "() => false")
6231
@Test("array.filter")
6332
public filter<T>(inp: T[], func: string) {
64-
// Make typechecker return array type
65-
dummyType = dummyArrayType;
6633
// Transpile
67-
let lua = transpileString(`return ToString([${inp.toString()}].filter(${func}))`);
68-
69-
// Add library
70-
lua = toStringDef + lualib + lua;
34+
let lua = util.transpileString(`return ArrayToString([${inp.toString()}].filter(${func}))`, util.dummyTypes.Array);
7135

7236
// Execute
73-
let result = executeLua(lua);
37+
let result = util.executeLua(lua);
7438

7539
// Assert
7640
Expect(result).toBe(inp.filter(eval(func)).toString());
7741
}
7842

7943
@TestCase([], "x => x > 1")
80-
@TestCase([0,1,2,3], "x => x > 1")
44+
@TestCase([0, 1, 2, 3], "x => x > 1")
8145
@TestCase([false, true, false], "x => x")
8246
@TestCase([true, true, true], "x => x")
8347
@Test("array.every")
8448
public every<T>(inp: T[], func: string) {
85-
// Make typechecker return array type
86-
dummyType = dummyArrayType;
8749
// Transpile
88-
let lua = transpileString(`return [${inp.toString()}].every(${func}))`);
89-
90-
// Add library
91-
lua = toStringDef + lualib + lua;
50+
let lua = util.transpileString(`return [${inp.toString()}].every(${func}))`, util.dummyTypes.Array);
9251

9352
// Execute
94-
let result = executeLua(lua);
53+
let result = util.executeLua(lua);
9554

9655
// Assert
9756
Expect(result.toString()).toBe(inp.every(eval(func)).toString());
9857
}
9958

10059
@TestCase([], "x => x > 1")
101-
@TestCase([0,1,2,3], "x => x > 1")
60+
@TestCase([0, 1, 2, 3], "x => x > 1")
10261
@TestCase([false, true, false], "x => x")
10362
@TestCase([true, true, true], "x => x")
10463
@Test("array.some")
10564
public some<T>(inp: T[], func: string) {
106-
// Make typechecker return array type
107-
dummyType = dummyArrayType;
10865
// Transpile
109-
let lua = transpileString(`return [${inp.toString()}].some(${func}))`);
110-
111-
// Add library
112-
lua = toStringDef + lualib + lua;
66+
let lua = util.transpileString(`return [${inp.toString()}].some(${func}))`, util.dummyTypes.Array);
11367

11468
// Execute
115-
let result = executeLua(lua);
69+
let result = util.executeLua(lua);
11670

11771
// Assert
11872
Expect(result.toString()).toBe(inp.some(eval(func)).toString());
11973
}
12074

12175
@TestCase([], 1, 2)
122-
@TestCase([0,1,2,3], 1, 2)
123-
@TestCase([0,1,2,3], 1, 1)
124-
@TestCase([0,1,2,3], 1, -1)
125-
@TestCase([0,1,2,3], -3, -1)
126-
@TestCase([0,1,2,3,4,5], 1, 3)
127-
@TestCase([0,1,2,3,4,5], 3)
76+
@TestCase([0, 1, 2, 3], 1, 2)
77+
@TestCase([0, 1, 2, 3], 1, 1)
78+
@TestCase([0, 1, 2, 3], 1, -1)
79+
@TestCase([0, 1, 2, 3], -3, -1)
80+
@TestCase([0, 1, 2, 3, 4, 5], 1, 3)
81+
@TestCase([0, 1, 2, 3, 4, 5], 3)
12882
@Test("array.slice")
12983
public slice<T>(inp: T[], start: number, end?: number) {
130-
// Make typechecker return array type
131-
dummyType = dummyArrayType;
13284
// Transpile
133-
let lua = transpileString(`return ToString([${inp.toString()}].slice(${start}, ${end}))`);
134-
135-
// Add library
136-
lua = toStringDef + lualib + lua;
85+
let lua = util.transpileString(`return ArrayToString([${inp.toString()}].slice(${start}, ${end}))`, util.dummyTypes.Array);
13786

13887
// Execute
139-
let result = executeLua(lua);
88+
let result = util.executeLua(lua);
14089

14190
// Assert
14291
Expect(result).toBe(inp.slice(start, end).toString());
14392
}
14493

14594
@TestCase([], 0, 0, 9, 10, 11)
146-
@TestCase([0,1,2,3], 1, 0, 9, 10, 11)
147-
@TestCase([0,1,2,3], 2, 2, 9, 10, 11)
148-
@TestCase([0,1,2,3], 4, 1, 8, 9)
149-
@TestCase([0,1,2,3], 4, 0, 8, 9)
150-
@TestCase([0,1,2,3,4,5], 5, 9, 10, 11)
151-
@TestCase([0,1,2,3,4,5], 3, 2, 3, 4, 5)
95+
@TestCase([0, 1, 2, 3], 1, 0, 9, 10, 11)
96+
@TestCase([0, 1, 2, 3], 2, 2, 9, 10, 11)
97+
@TestCase([0, 1, 2, 3], 4, 1, 8, 9)
98+
@TestCase([0, 1, 2, 3], 4, 0, 8, 9)
99+
@TestCase([0, 1, 2, 3, 4, 5], 5, 9, 10, 11)
100+
@TestCase([0, 1, 2, 3, 4, 5], 3, 2, 3, 4, 5)
152101
@Test("array.splice[Insert]")
153102
public spliceInsert<T>(inp: T[], start: number, deleteCount: number, ...newElements: any[]) {
154-
// Make typechecker return array type
155-
dummyType = dummyArrayType;
156103
// Transpile
157-
let lua = transpileString(
158-
`let spliceTestTable = [${inp.toString()}]
104+
let lua = util.transpileString(
105+
`let spliceTestTable = [${inp.toString()}];
159106
spliceTestTable.splice(${start}, ${deleteCount}, ${newElements});
160-
return ToString(spliceTestTable);`
107+
return ArrayToString(spliceTestTable);`,
108+
util.dummyTypes.Array
161109
);
162110

163-
// Add library
164-
lua = toStringDef + lualib + lua;
165-
166111
// Execute
167-
let result = executeLua(lua);
112+
let result = util.executeLua(lua);
168113

169114
// Assert
170115
inp.splice(start, deleteCount, ...newElements)
171116
Expect(result).toBe(inp.toString());
172117
}
173118

174119
@TestCase([], 1, 1)
175-
@TestCase([0,1,2,3], 1, 1)
176-
@TestCase([0,1,2,3], 10, 1)
177-
@TestCase([0,1,2,3], 4)
178-
@TestCase([0,1,2,3,4,5], 3)
179-
@TestCase([0,1,2,3,4,5], 2, 2)
180-
@TestCase([0,1,2,3,4,5,6,7,8], 5, 9, 10, 11)
120+
@TestCase([0, 1, 2, 3], 1, 1)
121+
@TestCase([0, 1, 2, 3], 10, 1)
122+
@TestCase([0, 1, 2, 3], 4)
123+
@TestCase([0, 1, 2, 3, 4, 5], 3)
124+
@TestCase([0, 1, 2, 3, 4, 5], 2, 2)
125+
@TestCase([0, 1, 2, 3, 4, 5, 6, 7, 8], 5, 9, 10, 11)
181126
@Test("array.splice[Remove]")
182127
public spliceRemove<T>(inp: T[], start: number, deleteCount?: number, ...newElements: any[]) {
183-
// Make typechecker return array type
184-
dummyType = dummyArrayType;
185128
// Transpile
186-
let lua = transpileString(`return ToString([${inp.toString()}].splice(${start}, ${deleteCount}, ${newElements}))`);
187-
188-
// Add library
189-
lua = toStringDef + lualib + lua;
129+
let lua = util.transpileString(`return ArrayToString([${inp.toString()}].splice(${start}, ${deleteCount}, ${newElements}))`, util.dummyTypes.Array);
190130

191131
// Execute
192-
let result = executeLua(lua);
132+
let result = util.executeLua(lua);
193133

194134
// Assert
195135
if (deleteCount) {

test/runner.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ testRunner.run(testSet);
2323
// this will be called after all tests have been run
2424
//.then((results) => done())
2525
// this will be called if there was a problem
26-
//.catch((error) => doSomethingWith(error));
26+
//.catch((error) => doSomethingWith(error));

0 commit comments

Comments
 (0)