forked from TypeScriptToLua/TypeScriptToLua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbindingpatterns.spec.ts
More file actions
84 lines (77 loc) · 3.62 KB
/
Copy pathbindingpatterns.spec.ts
File metadata and controls
84 lines (77 loc) · 3.62 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
import * as util from "../util";
const testCases = [
{ bindingString: "{x}", objectString: "{x: true}", returnVariable: "x" },
{ bindingString: "[x, y]", objectString: "[false, true]", returnVariable: "y" },
{ bindingString: "{x: [y, z]}", objectString: "{x: [false, true]}", returnVariable: "z" },
{ bindingString: "{x: [, z]}", objectString: "{x: [false, true]}", returnVariable: "z" },
{ bindingString: "{x: [{y}]}", objectString: "{x: [{y: true}]}", returnVariable: "y" },
{ bindingString: "[[y, z]]", objectString: "[[false, true]]", returnVariable: "z" },
{ bindingString: "{x, y}", objectString: "{x: false, y: true}", returnVariable: "y" },
{ bindingString: "{x: foo, y}", objectString: "{x: true, y: false}", returnVariable: "foo" },
{ bindingString: "{x: foo, y: bar}", objectString: "{x: false, y: true}", returnVariable: "bar" },
{ bindingString: "{x: {x, y}, z}", objectString: "{x: {x: true, y: false}, z: false}", returnVariable: "x" },
{ bindingString: "{x: {x, y}, z}", objectString: "{x: {x: false, y: true}, z: false}", returnVariable: "y" },
{ bindingString: "{x: {x, y}, z}", objectString: "{x: {x: false, y: false}, z: true}", returnVariable: "z" },
];
const testCasesDefault = [
{ bindingString: "{x = true}", objectString: "{}", returnVariable: "x" },
{ bindingString: "{x, y = true}", objectString: "{x: false}", returnVariable: "y" },
];
test.each([
{ bindingString: "{x, y}, z", objectString: "{x: false, y: false}, true", returnVariable: "z" },
{ bindingString: "{x, y}, {z}", objectString: "{x: false, y: false}, {z: true}", returnVariable: "z" },
...testCases,
...testCasesDefault,
])("Object bindings in functions (%p)", ({ bindingString, objectString, returnVariable }) => {
const result = util.transpileAndExecute(`
function test(${bindingString}) {
return ${returnVariable};
}
return test(${objectString});
`);
expect(result).toBe(true);
});
test.each([...testCases, ...testCasesDefault])(
"testBindingPatternDeclarations (%p)",
({ bindingString, objectString, returnVariable }) => {
const result = util.transpileAndExecute(`
let ${bindingString} = ${objectString};
return ${returnVariable};
`);
expect(result).toBe(true);
}
);
test.each([...testCases, ...testCasesDefault])(
"testBindingPatternExportDeclarations (%p)",
({ bindingString, objectString, returnVariable }) => {
const result = util.transpileExecuteAndReturnExport(
`export const ${bindingString} = ${objectString};`,
returnVariable
);
expect(result).toBe(true);
}
);
test.each(testCases)(
"Object bindings with call expressions (%p)",
({ bindingString, objectString, returnVariable }) => {
const result = util.transpileAndExecute(`
function call() {
return ${objectString};
}
let ${bindingString} = call();
return ${returnVariable};
`);
expect(result).toBe(true);
}
);
test.each([
{ bindingString: "{x, y = true}", objectString: "{x: false, y: false}", returnVariable: "y" },
{ bindingString: "{x, y: [z = true]}", objectString: "{x: false, y: [false]}", returnVariable: "z" },
{ bindingString: "[x = true]", objectString: "[false]", returnVariable: "x" },
])("Binding patterns handle false correctly (%p)", ({ bindingString, objectString, returnVariable }) => {
const result = util.transpileExecuteAndReturnExport(
`export const ${bindingString} = ${objectString};`,
returnVariable
);
expect(result).toBe(false);
});