forked from TypeScriptToLua/TypeScriptToLua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.spec.ts
More file actions
263 lines (232 loc) · 7.38 KB
/
array.spec.ts
File metadata and controls
263 lines (232 loc) · 7.38 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
import * as util from "../util";
test("Array access", () => {
const result = util.transpileAndExecute(
`const arr: Array<number> = [3,5,1];
return arr[1];`
);
expect(result).toBe(5);
});
test("ReadonlyArray access", () => {
const result = util.transpileAndExecute(
`const arr: ReadonlyArray<number> = [3,5,1];
return arr[1];`
);
expect(result).toBe(5);
});
test("Array literal access", () => {
const result = util.transpileAndExecute(
`const arr: number[] = [3,5,1];
return arr[1];`
);
expect(result).toBe(5);
});
test("Readonly array literal access", () => {
const result = util.transpileAndExecute(
`const arr: readonly number[] = [3,5,1];
return arr[1];`
);
expect(result).toBe(5);
});
test("Array union access", () => {
const result = util.transpileAndExecute(
`function makeArray(): number[] | string[] { return [3,5,1]; }
const arr = makeArray();
return arr[1];`
);
expect(result).toBe(5);
});
test("Array union access with empty tuple", () => {
const result = util.transpileAndExecute(
`function makeArray(): number[] | [] { return [3,5,1]; }
const arr = makeArray();
return arr[1];`
);
expect(result).toBe(5);
});
test("Array union length", () => {
const result = util.transpileAndExecute(
`function makeArray(): number[] | string[] { return [3,5,1]; }
const arr = makeArray();
return arr.length;`
);
expect(result).toBe(3);
});
test("Array intersection access", () => {
const result = util.transpileAndExecute(
`type I = number[] & {foo: string};
function makeArray(): I {
let t = [3,5,1];
(t as I).foo = "bar";
return (t as I);
}
const arr = makeArray();
return arr[1];`
);
expect(result).toBe(5);
});
test("Array intersection length", () => {
const result = util.transpileAndExecute(
`type I = number[] & {foo: string};
function makeArray(): I {
let t = [3,5,1];
(t as I).foo = "bar";
return (t as I);
}
const arr = makeArray();
return arr.length;`
);
expect(result).toBe(3);
});
test.each([
{ member: "firstElement()", expected: 3 },
{ member: "name", expected: "array" },
{ member: "length", expected: 1 },
])("Derived array access (%p)", ({ member, expected }) => {
const luaHeader = `local arr = {name="array", firstElement=function(self) return self[1]; end};`;
const typeScriptHeader = `
interface CustomArray<T> extends Array<T>{
name:string,
firstElement():number;
};
declare const arr: CustomArray<number>;
`;
const result = util.transpileAndExecute(
`
arr[0] = 3;
return arr.${member};`,
undefined,
luaHeader,
typeScriptHeader
);
expect(result).toBe(expected);
});
test("Array delete", () => {
const result = util.transpileAndExecute(
`const myarray = [1,2,3,4];
delete myarray[2];
return \`\${myarray[0]},\${myarray[1]},\${myarray[2]},\${myarray[3]}\`;`
);
expect(result).toBe("1,2,nil,4");
});
test("Array delete return true", () => {
const result = util.transpileAndExecute(
`const myarray = [1,2,3,4];
const exists = delete myarray[2];
return \`\${exists}:\${myarray[0]},\${myarray[1]},\${myarray[2]},\${myarray[3]}\`;`
);
expect(result).toBe("true:1,2,nil,4");
});
test("Array delete return false", () => {
const result = util.transpileAndExecute(
`const myarray = [1,2,3,4];
const exists = delete myarray[4];
return \`\${exists}:\${myarray[0]},\${myarray[1]},\${myarray[2]},\${myarray[3]}\`;`
);
expect(result).toBe("true:1,2,3,4");
});
test("Array property access", () => {
const code = `
type A = number[] & {foo?: string};
const a: A = [1,2,3];
a.foo = "bar";
return \`\${a.foo}\${a[0]}\${a[1]}\${a[2]}\`;
`;
expect(util.transpileAndExecute(code)).toBe("bar123");
});
test.each([{ length: 0, result: 0 }, { length: 1, result: 1 }, { length: 7, result: 3 }])(
"Array length set",
({ length, result }) => {
const code = `
const arr = [1, 2, 3];
arr.length = ${length};
return arr.length;
`;
expect(util.transpileAndExecute(code)).toBe(result);
}
);
test.each([{ length: 0, result: "0/0" }, { length: 1, result: "1/1" }, { length: 7, result: "7/3" }])(
"Array length set as expression",
({ length, result }) => {
const code = `
const arr = [1, 2, 3];
const l = arr.length = ${length};
return \`\${l}/\${arr.length}\`;
`;
expect(util.transpileAndExecute(code)).toBe(result);
}
);
test.each([
{ length: -1, result: -1 },
{ length: -7, result: -7 },
{ length: 0.1, result: 0.1 },
{ length: "0 / 0", result: "NaN" },
{ length: "1 / 0", result: "Infinity" },
{ length: "-1 / 0", result: "-Infinity" },
])("Invalid array length set", ({ length, result }) => {
const code = `
const arr = [1, 2, 3];
arr.length = ${length};
`;
expect(() => util.transpileAndExecute(code)).toThrowError(`invalid array length: ${result}`);
});
test.each([0, 1, 2])("Array with OmittedExpression", index => {
const result = util.transpileAndExecute(
`const myarray = [1, , 2];
return myarray[${index}];`
);
expect(result).toBe([1, , 2][index]);
});
test("OmittedExpression in Array Binding Assignment Statement", () => {
const result = util.transpileAndExecute(
`let a, c;
[a, , c] = [1, 2, 3];
return a + c;`
);
expect(result).toBe(4);
});
test("array access call", () => {
const code = `
const arr = [() => "foo", () => "bar"];
return arr[1]();`;
expect(util.transpileAndExecute(code)).toBe("bar");
});
test.each([`["foo", "bar"].length`, `["foo", "bar"][0]`, `[() => "foo", () => "bar"][0]()`])(
"array literal property access (%p)",
expression => {
const code = `return ${expression}`;
const expectResult = eval(expression);
expect(util.transpileAndExecute(code)).toBe(expectResult);
}
);
const genericChecks = [
"function generic<T extends number[]>(array: T)",
"function generic<T extends [...number[]]>(array: T)",
"function generic<T extends any>(array: T[])",
"type ArrayType = number[]; function generic<T extends ArrayType>(array: T)",
"function generic<T extends number[]>(array: T & {})",
"function generic<T extends number[] & {}>(array: T)",
];
test.each(genericChecks)("array constrained generic foreach (%p)", signature => {
const code = `
${signature}: number {
let sum = 0;
array.forEach(item => {
if (typeof item === "number") {
sum += item;
}
});
return sum;
}
return generic([1, 2, 3]);
`;
expect(util.transpileAndExecute(code)).toBe(6);
});
test.each(genericChecks)("array constrained generic length (%p)", signature => {
const code = `
${signature}: number {
return array.length;
}
return generic([1, 2, 3]);
`;
expect(util.transpileAndExecute(code)).toBe(3);
});