X Tutup
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions test/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ import * as ts from "typescript";

import { Expect } from "alsatian";

import { transpileString } from "../../src/Compiler";
import { transpileString as _transpileString } from "../../src/Compiler";
import { CompilerOptions } from "../../src/CompilerOptions";
import { LuaTarget, LuaTranspiler } from "../../src/Transpiler";
import { createTranspiler } from "../../src/TranspilerFactory";

import {lauxlib, lua, lualib, to_jsstring, to_luastring } from "fengari";

import * as fs from "fs";

export { transpileString };
export function transpileString(str: string, options?: CompilerOptions): string {
return _transpileString("/** !NoContext */ declare function JSONStringify(t: any): string;\n" + str, options);
}

export function executeLua(luaStr: string, withLib = true): any {
if (withLib) {
Expand Down
12 changes: 0 additions & 12 deletions test/unit/loops.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export class LuaLoopTests {
arrTest[i] = arrTest[i] + 1;
i++;
}
/** !NoContext */ declare function JSONStringify(t: any): string;
return JSONStringify(arrTest);`
);

Expand Down Expand Up @@ -53,7 +52,6 @@ export class LuaLoopTests {

i++;
}
/** !NoContext */ declare function JSONStringify(t: any): string;
return JSONStringify(arrTest);`
);

Expand Down Expand Up @@ -88,7 +86,6 @@ export class LuaLoopTests {

i++;
} while (i < arrTest.length)
/** !NoContext */ declare function JSONStringify(t: any): string;
return JSONStringify(arrTest);`
);

Expand All @@ -108,7 +105,6 @@ export class LuaLoopTests {
for (let i = 0; i < arrTest.length; ++i) {
arrTest[i] = arrTest[i] + 1;
}
/** !NoContext */ declare function JSONStringify(t: any): string;
return JSONStringify(arrTest);`
);

Expand Down Expand Up @@ -137,7 +133,6 @@ export class LuaLoopTests {
arrTest[i] = j;
}
}
/** !NoContext */ declare function JSONStringify(t: any): string;
return JSONStringify(arrTest);
`
);
Expand All @@ -158,7 +153,6 @@ export class LuaLoopTests {
for (let i = 0; arrTest.length > i; i++) {
arrTest[i] = arrTest[i] + 1;
}
/** !NoContext */ declare function JSONStringify(t: any): string;
return JSONStringify(arrTest);`
);

Expand All @@ -179,7 +173,6 @@ export class LuaLoopTests {
break;
arrTest[i] = arrTest[i] + 1;
}
/** !NoContext */ declare function JSONStringify(t: any): string;
return JSONStringify(arrTest);`
);

Expand All @@ -206,7 +199,6 @@ export class LuaLoopTests {
for (${header}) {
arrTest[i] = arrTest[i] + 1;
}
/** !NoContext */ declare function JSONStringify(t: any): string;
return JSONStringify(arrTest);`
);

Expand All @@ -226,7 +218,6 @@ export class LuaLoopTests {
for (let key in objTest) {
objTest[key] = objTest[key] + 1;
}
/** !NoContext */ declare function JSONStringify(t: any): string;
return JSONStringify(objTest);`
);

Expand Down Expand Up @@ -264,7 +255,6 @@ export class LuaLoopTests {

obj[i] = 0;
}
/** !NoContext */ declare function JSONStringify(t: any): string;
return JSONStringify(obj);
`
);
Expand All @@ -286,7 +276,6 @@ export class LuaLoopTests {
for (let value of objTest) {
arrResultTest.push(value + 1)
}
/** !NoContext */ declare function JSONStringify(t: any): string;
return JSONStringify(arrResultTest);`
);

Expand Down Expand Up @@ -318,7 +307,6 @@ export class LuaLoopTests {
}
a++;
}
/** !NoContext */ declare function JSONStringify(t: any): string;
return JSONStringify(testArr);`
);

Expand Down
25 changes: 5 additions & 20 deletions test/unit/lualib/lualib.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export class LuaLibArrayTests {
arrTest.forEach((elem, index) => {
arrTest[index] = arrTest[index] + 1;
})
/** !NoContext */ declare function JSONStringify(t: any): string;
return JSONStringify(arrTest);`
);

Expand All @@ -32,8 +31,7 @@ export class LuaLibArrayTests {
@Test("array.map")
public map<T>(inp: T[], func: string) {
// Transpile
const lua = util.transpileString(`/** !NoContext */ declare function JSONStringify(t: any): string;
return JSONStringify([${inp.toString()}].map(${func}))`);
const lua = util.transpileString(`return JSONStringify([${inp.toString()}].map(${func}))`);

// Execute
const result = util.executeLua(lua);
Expand All @@ -52,8 +50,7 @@ export class LuaLibArrayTests {
@Test("array.filter")
public filter<T>(inp: T[], func: string) {
// Transpile
const lua = util.transpileString(`/** !NoContext */ declare function JSONStringify(t: any): string;
return JSONStringify([${inp.toString()}].filter(${func}))`);
const lua = util.transpileString(`return JSONStringify([${inp.toString()}].filter(${func}))`);

// Execute
const result = util.executeLua(lua);
Expand All @@ -69,8 +66,7 @@ export class LuaLibArrayTests {
@Test("array.every")
public every<T>(inp: T[], func: string) {
// Transpile
const lua = util.transpileString(`/** !NoContext */ declare function JSONStringify(t: any): string;
return JSONStringify([${inp.toString()}].every(${func})))`);
const lua = util.transpileString(`return JSONStringify([${inp.toString()}].every(${func})))`);

// Execute
const result = util.executeLua(lua);
Expand All @@ -86,8 +82,7 @@ export class LuaLibArrayTests {
@Test("array.some")
public some<T>(inp: T[], func: string) {
// Transpile
const lua = util.transpileString(`/** !NoContext */ declare function JSONStringify(t: any): string;
return JSONStringify([${inp.toString()}].some(${func})))`);
const lua = util.transpileString(`return JSONStringify([${inp.toString()}].some(${func})))`);

// Execute
const result = util.executeLua(lua);
Expand All @@ -106,8 +101,7 @@ export class LuaLibArrayTests {
@Test("array.slice")
public slice<T>(inp: T[], start: number, end?: number) {
// Transpile
const lua = util.transpileString(`/** !NoContext */ declare function JSONStringify(t: any): string;
return JSONStringify([${inp.toString()}].slice(${start}, ${end}))`);
const lua = util.transpileString(`return JSONStringify([${inp.toString()}].slice(${start}, ${end}))`);

// Execute
const result = util.executeLua(lua);
Expand All @@ -129,7 +123,6 @@ export class LuaLibArrayTests {
const lua = util.transpileString(
`let spliceTestTable = [${inp.toString()}];
spliceTestTable.splice(${start}, ${deleteCount}, ${newElements});
/** !NoContext */ declare function JSONStringify(t: any): string;
return JSONStringify(spliceTestTable);`
);

Expand All @@ -156,14 +149,12 @@ export class LuaLibArrayTests {
lua = util.transpileString(
`let spliceTestTable = [${inp.toString()}];
spliceTestTable.splice(${start}, ${deleteCount}, ${newElements});
/** !NoContext */ declare function JSONStringify(t: any): string;
return JSONStringify(spliceTestTable);`
);
} else {
lua = util.transpileString(
`let spliceTestTable = [${inp.toString()}];
spliceTestTable.splice(${start});
/** !NoContext */ declare function JSONStringify(t: any): string;
return JSONStringify(spliceTestTable);`
);
}
Expand Down Expand Up @@ -197,7 +188,6 @@ export class LuaLibArrayTests {
// Transpile
const lua = util.transpileString(
`let concatTestTable = ${JSON.stringify(arr)};
/** !NoContext */ declare function JSONStringify(t: any): string;
return JSONStringify(concatTestTable.concat(${argStr}));`
);

Expand Down Expand Up @@ -288,7 +278,6 @@ export class LuaLibArrayTests {
const lua = util.transpileString(
`let testArray = [0];
testArray.push(${inp.join(", ")});
/** !NoContext */ declare function JSONStringify(t: any): string;
return JSONStringify(testArray);
`
);
Expand Down Expand Up @@ -343,7 +332,6 @@ export class LuaLibArrayTests {
const lua = util.transpileString(
`let testArray = ${array};
let val = testArray.reverse();
/** !NoContext */ declare function JSONStringify(t: any): string;
return JSONStringify(testArray)`);

// Execute
Expand All @@ -364,7 +352,6 @@ export class LuaLibArrayTests {
const lua = util.transpileString(
`let testArray = ${array};
let val = testArray.shift();
/** !NoContext */ declare function JSONStringify(t: any): string;
return JSONStringify(testArray)`);

// Execute
Expand Down Expand Up @@ -398,7 +385,6 @@ export class LuaLibArrayTests {
const lua = util.transpileString(
`let testArray = ${array};
testArray.unshift(${toUnshift});
/** !NoContext */ declare function JSONStringify(t: any): string;
return JSONStringify(testArray)`);
// Execute
const result = util.executeLua(lua);
Expand All @@ -418,7 +404,6 @@ export class LuaLibArrayTests {
const lua = util.transpileString(
`let testArray = ${array};
testArray.sort();
/** !NoContext */ declare function JSONStringify(t: any): string;
return JSONStringify(testArray)`);

// Execute
Expand Down
3 changes: 1 addition & 2 deletions test/unit/spreadElement.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ export class SpreadElementTest {
@TestCase([1, "test", 3])
@Test("Spread Element Push")
public spreadElementPush(inp: any[]) {
const lua = util.transpileString(`/** !NoContext */ declare function JSONStringify(t: any): string;
return JSONStringify([].push(...${JSON.stringify(inp)}));`);
const lua = util.transpileString(`return JSONStringify([].push(...${JSON.stringify(inp)}));`);
const result = util.executeLua(lua);
Expect(result).toBe([].push(...inp));
}
Expand Down
3 changes: 1 addition & 2 deletions test/unit/string.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,7 @@ export class StringTests {
public split(inp: string, separator: string): void {
// Transpile
const lua = util.transpileString(
`/** !NoContext */ declare function JSONStringify(t: any): string;
return JSONStringify("${inp}".split("${separator}"))`
`return JSONStringify("${inp}".split("${separator}"))`
);

// Execute
Expand Down
X Tutup