@@ -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