Skip to content

Commit 8dffc3e

Browse files
committed
Added Array.concat test
1 parent 2bb7c1a commit 8dffc3e

2 files changed

Lines changed: 49 additions & 6 deletions

File tree

src/lualib/ArrayConcat.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
1-
function __TS__ArrayConcat<T>(arr1: T[], arr2: T[]): T[] {
2-
const out: T[] = [];
1+
/* tslint:disable */
2+
declare function pcall(func: Function): any;
3+
declare function type(val: any): string;
4+
5+
function __TS__ArrayConcat(arr1: any[], ...args: any[]): any[] {
6+
const out: any[] = [];
37
for (let i = 0; i < arr1.length; i++) {
4-
out[i] = arr1[i];
8+
out[out.length] = arr1[i];
59
}
6-
for (let i = 0; i < arr2.length; i++) {
7-
out[i] = arr2[i];
10+
for (let i = 0; i < args.length; i++) {
11+
const arg = args[i];
12+
// Hack because we don't have an isArray function
13+
if (pcall(() => (arg as any[]).length) && type(arg) !== "string") {
14+
const argAsArray = (arg as any[]);
15+
for (let j = 0; j < argAsArray.length; j++) {
16+
out[out.length] = argAsArray[j];
17+
}
18+
} else {
19+
out[out.length] = arg;
20+
}
821
}
22+
923
return out;
1024
}

test/unit/lualib/lualib.spec.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,35 @@ export class LuaLibArrayTests {
172172
}
173173
}
174174

175+
@TestCase([], [])
176+
@TestCase([1, 2, 3], [])
177+
@TestCase([1, 2, 3], [4])
178+
@TestCase([1, 2, 3], [4, 5])
179+
@TestCase([1, 2, 3], [4, 5])
180+
@TestCase([1, 2, 3], 4, [5])
181+
@TestCase([1, 2, 3], 4, [5, 6])
182+
@TestCase([1, 2, 3], 4, [5, 6], 7)
183+
@TestCase([1, 2, 3], "test", [5, 6], 7, ["test1", "test2"])
184+
@TestCase([1, 2, "test"], "test", ["test1", "test2"])
185+
@Test("array.concat")
186+
public concat<T>(arr: T[], ...args: T[]) {
187+
const argStr = args.map(arg => JSON.stringify(arg)).join(",");
188+
console.log(argStr);
189+
console.log("\n");
190+
// Transpile
191+
const lua = util.transpileString(
192+
`let concatTestTable = ${JSON.stringify(arr)};
193+
return JSONStringify(concatTestTable.concat(${argStr}));`
194+
);
195+
196+
// Execute
197+
const result = util.executeLua(lua);
198+
199+
// Assert
200+
const concatArr = arr.concat(...args);
201+
Expect(result).toBe(JSON.stringify(concatArr));
202+
}
203+
175204
@TestCase([], "")
176205
@TestCase(["test1"], "test1")
177206
@TestCase(["test1", "test2"], "test1,test2")
@@ -308,7 +337,7 @@ export class LuaLibArrayTests {
308337
// Assert
309338
Expect(result).toBe(expected);
310339
}
311-
340+
312341
@TestCase("true", 11)
313342
@TestCase("false", 13)
314343
@TestCase("a < 4", 13)

0 commit comments

Comments
 (0)