Skip to content

Commit 8d55c98

Browse files
committed
Cover lualib with tests
1 parent e07aa52 commit 8d55c98

File tree

2 files changed

+92
-6
lines changed

2 files changed

+92
-6
lines changed

dist/lualib/typescript.lua

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,6 @@ function TS_ITE(condition, v1f, v2f)
77
end
88
end
99

10-
function TS_forEach(list, func)
11-
for k, v in ipairs(list) do
12-
func(v, k, list)
13-
end
14-
end
15-
1610
function TS_map(list, func)
1711
local out = {}
1812
for _, v in ipairs(list) do

test/integration/lua/lualib.spec.ts

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,96 @@ const toStringDef = "function ToString(list)\n"+
2929

3030
export class LuaTests {
3131

32+
@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")
38+
@Test("array.map")
39+
public map<T>(inp: T[], func: string) {
40+
// Make typechecker return array type
41+
dummyType = dummyArrayType;
42+
// Transpile
43+
let lua = transpileString(`return ToString([${inp.toString()}].map(${func}))`);
44+
45+
// Add library
46+
lua = toStringDef + lualib + lua;
47+
48+
// Execute
49+
let result = executeLua(lua);
50+
51+
// Assert
52+
Expect(result).toBe(inp.map(eval(func)).toString());
53+
}
54+
55+
@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")
62+
@Test("array.filter")
63+
public filter<T>(inp: T[], func: string) {
64+
// Make typechecker return array type
65+
dummyType = dummyArrayType;
66+
// Transpile
67+
let lua = transpileString(`return ToString([${inp.toString()}].filter(${func}))`);
68+
69+
// Add library
70+
lua = toStringDef + lualib + lua;
71+
72+
// Execute
73+
let result = executeLua(lua);
74+
75+
// Assert
76+
Expect(result).toBe(inp.filter(eval(func)).toString());
77+
}
78+
79+
@TestCase([], "x => x > 1")
80+
@TestCase([0,1,2,3], "x => x > 1")
81+
@TestCase([false, true, false], "x => x")
82+
@TestCase([true, true, true], "x => x")
83+
@Test("array.every")
84+
public every<T>(inp: T[], func: string) {
85+
// Make typechecker return array type
86+
dummyType = dummyArrayType;
87+
// Transpile
88+
let lua = transpileString(`return [${inp.toString()}].every(${func}))`);
89+
90+
// Add library
91+
lua = toStringDef + lualib + lua;
92+
93+
// Execute
94+
let result = executeLua(lua);
95+
96+
// Assert
97+
Expect(result.toString()).toBe(inp.every(eval(func)).toString());
98+
}
99+
100+
@TestCase([], "x => x > 1")
101+
@TestCase([0,1,2,3], "x => x > 1")
102+
@TestCase([false, true, false], "x => x")
103+
@TestCase([true, true, true], "x => x")
104+
@Test("array.some")
105+
public some<T>(inp: T[], func: string) {
106+
// Make typechecker return array type
107+
dummyType = dummyArrayType;
108+
// Transpile
109+
let lua = transpileString(`return [${inp.toString()}].some(${func}))`);
110+
111+
// Add library
112+
lua = toStringDef + lualib + lua;
113+
114+
// Execute
115+
let result = executeLua(lua);
116+
117+
// Assert
118+
Expect(result.toString()).toBe(inp.some(eval(func)).toString());
119+
}
120+
121+
@TestCase([], 1, 2)
32122
@TestCase([0,1,2,3], 1, 2)
33123
@TestCase([0,1,2,3], 1, 1)
34124
@TestCase([0,1,2,3], 1, -1)
@@ -52,6 +142,7 @@ export class LuaTests {
52142
Expect(result).toBe(inp.slice(start, end).toString());
53143
}
54144

145+
@TestCase([], 0, 0, 9, 10, 11)
55146
@TestCase([0,1,2,3], 1, 0, 9, 10, 11)
56147
@TestCase([0,1,2,3], 2, 2, 9, 10, 11)
57148
@TestCase([0,1,2,3], 4, 1, 8, 9)
@@ -80,6 +171,7 @@ export class LuaTests {
80171
Expect(result).toBe(inp.toString());
81172
}
82173

174+
@TestCase([], 1, 1)
83175
@TestCase([0,1,2,3], 1, 1)
84176
@TestCase([0,1,2,3], 10, 1)
85177
@TestCase([0,1,2,3], 4)

0 commit comments

Comments
 (0)