forked from TypeScriptToLua/TypeScriptToLua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmulti.spec.ts
More file actions
305 lines (278 loc) · 8.42 KB
/
Copy pathmulti.spec.ts
File metadata and controls
305 lines (278 loc) · 8.42 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import * as path from "path";
import * as util from "../../util";
import * as tstl from "../../../src";
import {
invalidMultiFunctionUse,
invalidMultiReturnAccess,
invalidMultiFunctionReturnType,
} from "../../../src/transformation/utils/diagnostics";
const multiProjectOptions: tstl.CompilerOptions = {
types: [path.resolve(__dirname, "../../../language-extensions")],
};
test("multi example use case", () => {
util.testModule`
function multiReturn(): LuaMultiReturn<[string, number]> {
return $multi("foo", 5);
}
const [a, b] = multiReturn();
export { a, b };
`
.setOptions(multiProjectOptions)
.expectToEqual({ a: "foo", b: 5 });
});
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/995
test("Destructuring assignment of LuaMultiReturn", () => {
util.testModule`
function multiReturn(): LuaMultiReturn<[number, number, number]> {
return $multi(1, 2, 3);
}
const [a, ...b] = multiReturn();
export {a, b};
`
.setOptions(multiProjectOptions)
.expectToEqual({ a: 1, b: [2, 3] });
});
test("Destructuring assignment of LuaMultiReturn returning nil", () => {
util.testModule`
function multiReturn(): LuaMultiReturn<[number, number, number]> {
return;
}
const [a, ...b] = multiReturn();
export {a, b};
`
.setOptions(multiProjectOptions)
.expectToEqual({ a: undefined, b: [] });
});
test.each<[string, any]>([
["$multi()", undefined],
["$multi(true)", true],
["$multi(1, 2)", 1],
])("$multi call on return statement (%s)", (expression, result) => {
util.testFunction`
return ${expression};
`
.setOptions(multiProjectOptions)
.expectToEqual(result);
});
const multiFunction = `
function multi(...args) {
return $multi(...args);
}
`;
const createCasesThatCall = (name: string): Array<[string, any]> => [
[`let a; [a] = ${name}()`, undefined],
[`const [a] = ${name}()`, undefined],
[`const [a] = ${name}(1)`, 1],
[`const [a = 1] = ${name}()`, 1],
[`const [a = 1] = ${name}(2)`, 2],
[`const ar = [1]; const [a] = ${name}(...ar)`, 1],
[`const _ = null, [a] = ${name}(1)`, 1],
[`let a; for (const [a] = ${name}(1, 2); false; 1) {}`, undefined],
[`let a; for ([a] = ${name}(1, 2); false; 1) {}`, 1],
[`let a; if ([a] = ${name}(1)) { ++a; }`, 2],
];
test.each<[string, any]>(createCasesThatCall("$multi"))("invalid direct $multi function use (%s)", statement => {
util.testModule`
${multiFunction}
${statement}
export { a };
`
.setOptions(multiProjectOptions)
.setReturnExport("a")
.expectDiagnosticsToMatchSnapshot([invalidMultiFunctionUse.code]);
});
test.each<[string, any]>(createCasesThatCall("multi"))(
"valid indirect $multi function use (%s)",
(statement, result) => {
util.testModule`
${multiFunction}
${statement}
export { a };
`
.setOptions(multiProjectOptions)
.setReturnExport("a")
.expectToEqual(result);
}
);
test.each<[string, number[]]>([
["$multi", [invalidMultiFunctionUse.code]],
["$multi()", [invalidMultiFunctionUse.code]],
["({ $multi });", [invalidMultiFunctionUse.code]],
["const a = $multi();", [invalidMultiFunctionUse.code]],
["const {} = $multi();", [invalidMultiFunctionUse.code]],
["([a] = $multi(1)) => {}", [invalidMultiFunctionUse.code]],
["const [a = 0] = $multi()", [invalidMultiFunctionUse.code]],
])("invalid $multi call (%s)", (statement, diagnostics) => {
util.testModule`
${statement}
`
.setOptions(multiProjectOptions)
.expectDiagnosticsToMatchSnapshot(diagnostics);
});
test("function to spread multi type result from multi type function", () => {
util.testFunction`
${multiFunction}
function m() {
return $multi(...multi(true));
}
return m();
`
.setOptions(multiProjectOptions)
.expectToEqual(true);
});
test("$multi call with destructuring assignment side effects", () => {
util.testModule`
${multiFunction}
let a;
export { a };
[a] = multi(1);
`
.setOptions(multiProjectOptions)
.setReturnExport("a")
.expectToEqual(1);
});
test("allow $multi call in ArrowFunction body", () => {
util.testFunction`
const call = () => $multi(1);
const [result] = call();
return result;
`
.setOptions(multiProjectOptions)
.expectToEqual(1);
});
test("forward $multi call", () => {
util.testFunction`
function foo() { return $multi(1, 2); }
function call() { return foo(); }
const [resultA, resultB] = call();
return [resultA, resultB];
`
.setOptions(multiProjectOptions)
.expectToEqual([1, 2]);
});
test("forward $multi call indirect", () => {
util.testFunction`
function foo() { return $multi(1, 2); }
function call() { const m = foo(); return m; }
const [resultA, resultB] = call();
return [resultA, resultB];
`
.setOptions(multiProjectOptions)
.expectToEqual([1, 2]);
});
test("forward $multi call in ArrowFunction body", () => {
util.testFunction`
const foo = () => $multi(1, 2);
const call = () => foo();
const [resultA, resultB] = call();
return [resultA, resultB];
`
.setOptions(multiProjectOptions)
.expectToEqual([1, 2]);
});
test.each(["0", "i"])("allow LuaMultiReturn numeric access (%s)", expression => {
util.testFunction`
${multiFunction}
const i = 0;
return multi(1)[${expression}];
`
.setOptions(multiProjectOptions)
.expectToEqual(1);
});
test.each(["multi()['forEach']", "multi().forEach"])("disallow LuaMultiReturn non-numeric access", expression => {
util.testFunction`
${multiFunction}
return ${expression};
`
.setOptions(multiProjectOptions)
.expectDiagnosticsToMatchSnapshot([invalidMultiReturnAccess.code]);
});
test("invalid $multi implicit cast", () => {
util.testModule`
function badMulti(): [string, number] {
return $multi("foo", 42);
}
`
.setOptions(multiProjectOptions)
.expectDiagnosticsToMatchSnapshot([invalidMultiFunctionReturnType.code]);
});
test.each([
{ flag: true, result: 4 },
{ flag: false, result: 7 },
])("overloaded $multi/non-$multi return", ({ flag, result }) => {
util.testFunction`
function multiOverload(flag: true): LuaMultiReturn<[string, number]>;
function multiOverload(flag: false): [string, number];
function multiOverload(flag: boolean) {
if (flag) {
return $multi("foo", 4);
} else {
return ["bar", 7];
}
}
const [x, y] = multiOverload(${flag});
return y;
`
.setOptions(multiProjectOptions)
.expectToEqual(result);
});
test("return $multi from try", () => {
util.testFunction`
function multiTest() {
try {
return $multi(1, 2);
} catch {
}
}
const [_, a] = multiTest();
return a;
`
.setOptions(multiProjectOptions)
.expectToEqual(2);
});
test("return $multi from catch", () => {
util.testFunction`
function multiTest() {
try {
throw "error";
} catch {
return $multi(1, 2);
}
}
const [_, a] = multiTest();
return a;
`
.setOptions(multiProjectOptions)
.expectToEqual(2);
});
test("return LuaMultiReturn from try", () => {
util.testFunction`
${multiFunction}
function multiTest() {
try {
return multi(1, 2);
} catch {
}
}
const [_, a] = multiTest();
return a;
`
.setOptions(multiProjectOptions)
.expectToEqual(2);
});
test("return LuaMultiReturn from catch", () => {
util.testFunction`
${multiFunction}
function multiTest() {
try {
throw "error";
} catch {
return multi(1, 2);
}
}
const [_, a] = multiTest();
return a;
`
.setOptions(multiProjectOptions)
.expectToEqual(2);
});