forked from TypeScriptToLua/TypeScriptToLua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspread.spec.ts
More file actions
89 lines (79 loc) · 3.16 KB
/
Copy pathspread.spec.ts
File metadata and controls
89 lines (79 loc) · 3.16 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
import * as tstl from "../../src";
import * as util from "../util";
// TODO: Make some utils for testing other targets
const expectUnpack: util.TapCallback = builder => expect(builder.getMainLuaCodeChunk()).toMatch(/[^.]unpack\(/);
const expectTableUnpack: util.TapCallback = builder => expect(builder.getMainLuaCodeChunk()).toContain("table.unpack");
describe("in function call", () => {
util.testEachVersion(
undefined,
() => util.testFunction`
function foo(a: number, b: number, ...rest: number[]) {
return { a, b, rest }
}
const array = [0, 1, 2, 3] as const;
return foo(...array);
`,
{
[tstl.LuaTarget.LuaJIT]: builder => builder.tap(expectUnpack),
[tstl.LuaTarget.Lua51]: builder => builder.tap(expectUnpack),
[tstl.LuaTarget.Lua52]: builder => builder.tap(expectTableUnpack),
[tstl.LuaTarget.Lua53]: builder => builder.tap(expectTableUnpack).expectToMatchJsResult(),
}
);
});
describe("in array literal", () => {
util.testEachVersion("of array literal", () => util.testExpression`[...[0, 1, 2]]`, {
[tstl.LuaTarget.LuaJIT]: builder => builder.tap(expectUnpack),
[tstl.LuaTarget.Lua51]: builder => builder.tap(expectUnpack),
[tstl.LuaTarget.Lua52]: builder => builder.tap(expectTableUnpack),
[tstl.LuaTarget.Lua53]: builder => builder.tap(expectTableUnpack).expectToMatchJsResult(),
});
test.each(["", "string", "string with spaces", "string 1 2 3"])("of string literal (%p)", str => {
util.testExpressionTemplate`[...${str}]`.expectToMatchJsResult();
});
test("of iterable", () => {
util.testFunction`
const it = {
i: -1,
[Symbol.iterator]() {
return this;
},
next() {
++this.i;
return {
value: 2 ** this.i,
done: this.i == 9,
}
}
};
return [...it]
`.expectToMatchJsResult();
});
});
describe("in object literal", () => {
test.each([
"{ x: false, ...{ x: true, y: true } }",
"{ ...{ x: true, y: true } }",
"{ ...{ x: true }, ...{ y: true, z: true } }",
"{ ...{ x: false }, x: true }",
"{ ...{ x: false }, x: false, ...{ x: true } }",
])("of object literal (%p)", expression => {
util.testExpression(expression).expectToMatchJsResult();
});
test("of object reference", () => {
util.testFunction`
const object = { x: 0, y: 1 };
const result = { ...object, z: 2 };
return { object, result };
`.expectToMatchJsResult();
});
test.each([
["literal", "const object = { ...[0, 1, 2] };"],
["reference", "const array = [0, 1, 2]; const object = { ...array };"],
])("of array %p", (_name, expressionToCreateObject) => {
util.testFunction`
${expressionToCreateObject}
return { "0": object[0], "1": object[1], "2": object[2] };
`.expectToMatchJsResult();
});
});