Skip to content

Commit fff847b

Browse files
committed
lua tests
1 parent 4f1fd41 commit fff847b

File tree

5 files changed

+65
-4
lines changed

5 files changed

+65
-4
lines changed

dist/lualib/typescript.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ function TS_filter(list, func)
3232
end
3333

3434
function TS_slice(list, startI, endI)
35-
endI = endI or (#list-1)
35+
endI = endI or #list
36+
if startI < 0 then startI = #list + startI end
37+
if endI < 0 then endI = #list + endI end
3638
local out = {}
37-
for i = startI + 1, endI + 1 do
39+
for i = startI + 1, endI do
3840
table.insert(out, list[i])
3941
end
4042
return out

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"dependencies": {
33
"alsatian": "^2.1.0",
4+
"lua.vm.js": "0.0.1",
45
"typescript": "^2.7.1"
56
}
67
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
}

test/tsconfig.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"compilerOptions": {
33
"experimentalDecorators": true
4-
},
5-
"exclude": ["integration/**/*"]
4+
}
65
}

0 commit comments

Comments
 (0)