1+ import { Expect , Test , TestCase } from "alsatian" ;
2+
3+ import * as ts from "typescript" ;
4+ import { LuaTranspiler , TranspileError } from "../../../dist/Transpiler" ;
5+
6+ const LuaVM = require ( "lua.vm.js" ) ;
7+ const fs = require ( "fs" ) ;
8+
9+ const dummyArrayType = { flags : ts . TypeFlags . Object , symbol : { escapedName : "Array" } } ;
10+ let dummyType = { } ;
11+ const dummyChecker = { getTypeAtLocation : function ( ) { return dummyType ; } }
12+ function transpileString ( str : string ) : string {
13+ const file = ts . createSourceFile ( "" , str , ts . ScriptTarget . Latest ) ;
14+ const result = LuaTranspiler . transpileSourceFile ( file , dummyChecker , false ) ;
15+ return result . trim ( ) ;
16+ }
17+ function executeLua ( lua : string ) : string {
18+ const luavm = new LuaVM . Lua . State ( ) ;
19+ return luavm . execute ( lua ) [ 0 ] ;
20+ }
21+
22+ const lualib = fs . readFileSync ( "dist/lualib/typescript.lua" ) + "\n" ;
23+
24+ const toStringDef = "function ToString(list)\n" +
25+ "local result = \"\"\n" +
26+ "for i=1,#list do result = result .. list[i]\n" +
27+ "if i < #list then result = result .. ',' end end\n" +
28+ "return result end\n" ;
29+
30+ export class LuaTests {
31+
32+ @TestCase ( [ 0 , 1 , 2 , 3 ] , 1 , 2 )
33+ @TestCase ( [ 0 , 1 , 2 , 3 ] , 1 , 1 )
34+ @TestCase ( [ 0 , 1 , 2 , 3 ] , 1 , - 1 )
35+ @TestCase ( [ 0 , 1 , 2 , 3 ] , - 3 , - 1 )
36+ @TestCase ( [ 0 , 1 , 2 , 3 , 4 , 5 ] , 1 , 3 )
37+ @TestCase ( [ 0 , 1 , 2 , 3 , 4 , 5 ] , 3 )
38+ @Test ( "array.slice" )
39+ public slice < T > ( inp : T [ ] , start : number , end ?: number ) {
40+ // Make typechecker return array type
41+ dummyType = dummyArrayType ;
42+ // Transpile
43+ let lua = transpileString ( `return ToString([${ inp . toString ( ) } ].slice(${ start } , ${ end } ))` ) ;
44+
45+ // Add library
46+ lua = toStringDef + lualib + lua ;
47+
48+ // Execute
49+ let result = executeLua ( lua ) ;
50+
51+ // Assert
52+ Expect ( result ) . toBe ( inp . slice ( start , end ) . toString ( ) ) ;
53+ }
54+ }
0 commit comments