Skip to content

Commit dccc1a1

Browse files
committed
Tests for splice
1 parent 5d7278c commit dccc1a1

File tree

2 files changed

+64
-7
lines changed

2 files changed

+64
-7
lines changed

dist/lualib/typescript.lua

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,21 @@ function TS_splice(list, startI, deleteCount, ...)
4949
startI = startI + 1
5050
local newElements = {...}
5151
local out = {}
52-
local k = #newElements
52+
local outPtr = deleteCount
53+
local newElementsCount = #newElements
5354
for i = startI + deleteCount - 1, startI, -1 do
54-
table.insert(out, list[i])
55+
out[outPtr] = list[i]
56+
outPtr = outPtr -1
5557
if newElements[k] then
5658
list[i] = newElements[k]
57-
k = k - 1
59+
newElementsCount = newElementsCount - 1
5860
else
5961
table.remove(list, i)
6062
end
6163
end
62-
while newElements[k] do
63-
table.insert(list, startI, newElements[k])
64-
k = k - 1
64+
while newElements[newElementsCount] do
65+
table.insert(list, startI, newElements[newElementsCount])
66+
newElementsCount = newElementsCount - 1
6567
end
6668
return out
6769
end

test/integration/lua/lualib.spec.ts

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,59 @@ export class LuaTests {
5151
// Assert
5252
Expect(result).toBe(inp.slice(start, end).toString());
5353
}
54-
}
54+
55+
@TestCase([0,1,2,3], 1, 0, 9, 10, 11)
56+
@TestCase([0,1,2,3], 2, 2, 9, 10, 11)
57+
@TestCase([0,1,2,3], 4, 1, 8, 9)
58+
@TestCase([0,1,2,3], 4, 0, 8, 9)
59+
@TestCase([0,1,2,3,4,5], 5, 9, 10, 11)
60+
@TestCase([0,1,2,3,4,5], 3, 2, 3, 4, 5)
61+
@Test("array.splice[Insert]")
62+
public spliceInsert<T>(inp: T[], start: number, deleteCount: number, ...newElements: any[]) {
63+
// Make typechecker return array type
64+
dummyType = dummyArrayType;
65+
// Transpile
66+
let lua = transpileString(
67+
`let spliceTestTable = [${inp.toString()}]
68+
spliceTestTable.splice(${start}, ${deleteCount}, ${newElements});
69+
return ToString(spliceTestTable);`
70+
);
71+
72+
// Add library
73+
lua = toStringDef + lualib + lua;
74+
75+
// Execute
76+
let result = executeLua(lua);
77+
78+
// Assert
79+
inp.splice(start, deleteCount, ...newElements)
80+
Expect(result).toBe(inp.toString());
81+
}
82+
83+
@TestCase([0,1,2,3], 1, 1)
84+
@TestCase([0,1,2,3], 10, 1)
85+
@TestCase([0,1,2,3], 4)
86+
@TestCase([0,1,2,3,4,5], 3)
87+
@TestCase([0,1,2,3,4,5], 2, 2)
88+
@TestCase([0,1,2,3,4,5,6,7,8], 5, 9, 10, 11)
89+
@Test("array.splice[Remove]")
90+
public spliceRemove<T>(inp: T[], start: number, deleteCount?: number, ...newElements: any[]) {
91+
// Make typechecker return array type
92+
dummyType = dummyArrayType;
93+
// Transpile
94+
let lua = transpileString(`return ToString([${inp.toString()}].splice(${start}, ${deleteCount}, ${newElements}))`);
95+
96+
// Add library
97+
lua = toStringDef + lualib + lua;
98+
99+
// Execute
100+
let result = executeLua(lua);
101+
102+
// Assert
103+
if (deleteCount) {
104+
Expect(result).toBe(inp.splice(start, deleteCount, ...newElements).toString());
105+
} else {
106+
Expect(result).toBe(inp.splice(start).toString());
107+
}
108+
}
109+
}

0 commit comments

Comments
 (0)