forked from TypeScriptToLua/TypeScriptToLua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdestructuring.spec.ts
More file actions
129 lines (115 loc) · 4.65 KB
/
destructuring.spec.ts
File metadata and controls
129 lines (115 loc) · 4.65 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
import * as util from "../util";
const allBindings = "x, y, z, rest";
const testCases = [
{ binding: "{ x }", value: { x: true } },
{ binding: "{ x, y }", value: { x: false, y: true } },
{ binding: "{ x: z, y }", value: { x: true, y: false } },
{ binding: "{ x: { x, y }, z }", value: { x: { x: true, y: false }, z: false } },
{ binding: "{ x, y = true }", value: { x: false, y: false } },
{ binding: "{ x = true }", value: {} },
{ binding: "{ x, y = true }", value: { x: false } },
{ binding: "{ ...rest }", value: {} },
{ binding: "{ x, ...rest }", value: { x: "x" } },
{ binding: "{ x, ...rest }", value: { x: "x", y: "y", z: "z" } },
{ binding: "[]", value: [] },
{ binding: "[x, y]", value: ["x", "y"] },
{ binding: "[x, , y]", value: ["x", "", "y"] },
{ binding: "[x = true]", value: [false] },
{ binding: "[[x, y]]", value: [["x", "y"]] },
{ binding: "[x, ...rest]", value: ["x"] },
{ binding: "[x, ...rest]", value: ["x", "y", "z"] },
{ binding: "{ y: [z = true] }", value: { y: [false] } },
{ binding: "{ x: [x, y] }", value: { x: ["x", "y"] } },
{ binding: "{ x: [{ y }] }", value: { x: [{ y: "y" }] } },
].map(({ binding, value }) => ({ binding, value: util.formatCode(value) }));
test.each([
...testCases,
{ binding: "{ x, y }, z", value: "{ x: false, y: false }, true" },
{ binding: "{ x, y }, { z }", value: "{ x: false, y: false }, { z: true }" },
])("in function parameter (%p)", ({ binding, value }) => {
util.testFunction`
let ${allBindings};
function test(${binding}) {
return { ${allBindings} };
}
return test(${value});
`.expectToMatchJsResult();
});
test.each(testCases)("in variable declaration (%p)", ({ binding, value }) => {
util.testFunction`
let ${allBindings};
{
const ${binding} = ${value};
return { ${allBindings} };
}
`.expectToMatchJsResult();
});
// TODO: https://github.com/TypeScriptToLua/TypeScriptToLua/issues/695
// TODO: https://github.com/microsoft/TypeScript/issues/32656
test.each(testCases.filter(x => x.binding !== "[x, , y]" && x.binding !== "{ x, ...rest }"))(
"in exported variable declaration (%p)",
({ binding, value }) => {
util.testModule`
export const ${binding} = ${value};
`.expectToMatchJsResult();
}
);
const assignmentTestCases = [
...testCases,
...[
{ binding: "{ x: obj.prop }", value: { x: true } },
{ binding: "{ x: obj.prop = true }", value: {} },
{ binding: "[{ x: obj.prop }]", value: [{ x: true }] },
{ binding: "{ obj: { prop: obj.prop } }", value: { obj: { prop: true } } },
{ binding: "{ x = true }", value: {} },
].map(({ binding, value }) => ({ binding, value: util.formatCode(value) })),
{ binding: "{ x: { [(3).toString()]: y } }", value: "{ x: { [(3).toString()]: true } }" },
];
test.each(assignmentTestCases)("in assignment expression (%p)", ({ binding, value }) => {
util.testFunction`
let ${allBindings};
const obj = { prop: false };
const expressionResult = (${binding} = ${value});
return { ${allBindings}, obj, expressionResult };
`.expectToMatchJsResult();
});
describe("array destructuring optimization", () => {
// TODO: Try to generalize optimization logic between declaration and assignment and make more generic tests
test("array", () => {
util.testFunction`
const array = [3, 5, 1];
const [a, b, c] = array;
return { a, b, c };
`
.tap(builder => expect(builder.getMainLuaCodeChunk()).toContain("unpack"))
.expectToMatchJsResult();
});
test("array literal", () => {
util.testFunction`
const [a, b, c] = [3, 5, 1];
return { a, b, c };
`
.tap(builder => expect(builder.getMainLuaCodeChunk()).not.toContain("unpack"))
.expectToMatchJsResult();
});
test("array literal with extra values", () => {
util.testFunction`
let called = false;
const set = () => { called = true; };
const [head] = ["foo", set()];
return { head, called };
`
.tap(builder => expect(builder.getMainLuaCodeChunk()).not.toContain("unpack"))
.expectToMatchJsResult();
});
test("array union", () => {
util.testFunction`
const array: [string] | [] = ["bar"];
let x: string;
[x] = array;
return x;
`
.tap(builder => expect(builder.getMainLuaCodeChunk()).toContain("unpack"))
.expectToMatchJsResult();
});
});