|
1 | 1 | import { Expect, Test, TestCase } from "alsatian"; |
| 2 | +import * as util from "../../src/util" |
2 | 3 |
|
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 { |
31 | 5 |
|
32 | 6 | @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") |
38 | 12 | @Test("array.map") |
39 | 13 | public map<T>(inp: T[], func: string) { |
40 | | - // Make typechecker return array type |
41 | | - dummyType = dummyArrayType; |
42 | 14 | // 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); |
47 | 16 |
|
48 | 17 | // Execute |
49 | | - let result = executeLua(lua); |
| 18 | + let result = util.executeLua(lua); |
50 | 19 |
|
51 | 20 | // Assert |
52 | 21 | Expect(result).toBe(inp.map(eval(func)).toString()); |
53 | 22 | } |
54 | 23 |
|
55 | 24 | @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") |
62 | 31 | @Test("array.filter") |
63 | 32 | public filter<T>(inp: T[], func: string) { |
64 | | - // Make typechecker return array type |
65 | | - dummyType = dummyArrayType; |
66 | 33 | // 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); |
71 | 35 |
|
72 | 36 | // Execute |
73 | | - let result = executeLua(lua); |
| 37 | + let result = util.executeLua(lua); |
74 | 38 |
|
75 | 39 | // Assert |
76 | 40 | Expect(result).toBe(inp.filter(eval(func)).toString()); |
77 | 41 | } |
78 | 42 |
|
79 | 43 | @TestCase([], "x => x > 1") |
80 | | - @TestCase([0,1,2,3], "x => x > 1") |
| 44 | + @TestCase([0, 1, 2, 3], "x => x > 1") |
81 | 45 | @TestCase([false, true, false], "x => x") |
82 | 46 | @TestCase([true, true, true], "x => x") |
83 | 47 | @Test("array.every") |
84 | 48 | public every<T>(inp: T[], func: string) { |
85 | | - // Make typechecker return array type |
86 | | - dummyType = dummyArrayType; |
87 | 49 | // 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); |
92 | 51 |
|
93 | 52 | // Execute |
94 | | - let result = executeLua(lua); |
| 53 | + let result = util.executeLua(lua); |
95 | 54 |
|
96 | 55 | // Assert |
97 | 56 | Expect(result.toString()).toBe(inp.every(eval(func)).toString()); |
98 | 57 | } |
99 | 58 |
|
100 | 59 | @TestCase([], "x => x > 1") |
101 | | - @TestCase([0,1,2,3], "x => x > 1") |
| 60 | + @TestCase([0, 1, 2, 3], "x => x > 1") |
102 | 61 | @TestCase([false, true, false], "x => x") |
103 | 62 | @TestCase([true, true, true], "x => x") |
104 | 63 | @Test("array.some") |
105 | 64 | public some<T>(inp: T[], func: string) { |
106 | | - // Make typechecker return array type |
107 | | - dummyType = dummyArrayType; |
108 | 65 | // 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); |
113 | 67 |
|
114 | 68 | // Execute |
115 | | - let result = executeLua(lua); |
| 69 | + let result = util.executeLua(lua); |
116 | 70 |
|
117 | 71 | // Assert |
118 | 72 | Expect(result.toString()).toBe(inp.some(eval(func)).toString()); |
119 | 73 | } |
120 | 74 |
|
121 | 75 | @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) |
128 | 82 | @Test("array.slice") |
129 | 83 | public slice<T>(inp: T[], start: number, end?: number) { |
130 | | - // Make typechecker return array type |
131 | | - dummyType = dummyArrayType; |
132 | 84 | // 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); |
137 | 86 |
|
138 | 87 | // Execute |
139 | | - let result = executeLua(lua); |
| 88 | + let result = util.executeLua(lua); |
140 | 89 |
|
141 | 90 | // Assert |
142 | 91 | Expect(result).toBe(inp.slice(start, end).toString()); |
143 | 92 | } |
144 | 93 |
|
145 | 94 | @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) |
152 | 101 | @Test("array.splice[Insert]") |
153 | 102 | public spliceInsert<T>(inp: T[], start: number, deleteCount: number, ...newElements: any[]) { |
154 | | - // Make typechecker return array type |
155 | | - dummyType = dummyArrayType; |
156 | 103 | // Transpile |
157 | | - let lua = transpileString( |
158 | | - `let spliceTestTable = [${inp.toString()}] |
| 104 | + let lua = util.transpileString( |
| 105 | + `let spliceTestTable = [${inp.toString()}]; |
159 | 106 | spliceTestTable.splice(${start}, ${deleteCount}, ${newElements}); |
160 | | - return ToString(spliceTestTable);` |
| 107 | + return ArrayToString(spliceTestTable);`, |
| 108 | + util.dummyTypes.Array |
161 | 109 | ); |
162 | 110 |
|
163 | | - // Add library |
164 | | - lua = toStringDef + lualib + lua; |
165 | | - |
166 | 111 | // Execute |
167 | | - let result = executeLua(lua); |
| 112 | + let result = util.executeLua(lua); |
168 | 113 |
|
169 | 114 | // Assert |
170 | 115 | inp.splice(start, deleteCount, ...newElements) |
171 | 116 | Expect(result).toBe(inp.toString()); |
172 | 117 | } |
173 | 118 |
|
174 | 119 | @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) |
181 | 126 | @Test("array.splice[Remove]") |
182 | 127 | public spliceRemove<T>(inp: T[], start: number, deleteCount?: number, ...newElements: any[]) { |
183 | | - // Make typechecker return array type |
184 | | - dummyType = dummyArrayType; |
185 | 128 | // 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); |
190 | 130 |
|
191 | 131 | // Execute |
192 | | - let result = executeLua(lua); |
| 132 | + let result = util.executeLua(lua); |
193 | 133 |
|
194 | 134 | // Assert |
195 | 135 | if (deleteCount) { |
|
0 commit comments