Skip to content

Commit ddccc06

Browse files
committed
Added Tests for while loop & string functions
1 parent 793211e commit ddccc06

File tree

6 files changed

+142
-21
lines changed

6 files changed

+142
-21
lines changed

src/Transpiler.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -644,28 +644,27 @@ export class LuaTranspiler {
644644
transpileCallExpression(node: ts.CallExpression): string {
645645
// Check for calls on primitives to override
646646
if (ts.isPropertyAccessExpression(node.expression)) {
647-
const type = this.checker.getTypeAtLocation(node.expression.expression);
648-
switch (type.flags) {
649-
case ts.TypeFlags.String:
650-
case ts.TypeFlags.StringLiteral:
651-
return this.transpileStringCallExpression(node);
652-
case ts.TypeFlags.Object:
653-
if (tsEx.isArrayType(type))
654-
return this.transpileArrayCallExpression(node);
655-
}
656-
657647
const expType = this.checker.getTypeAtLocation(node.expression.expression);
658648

659649
if (expType.symbol && expType.symbol.escapedName == "Math") {
660650
const params = this.transpileArguments(node.arguments);
661651
return this.transpileMathExpression(node.expression.name) + `(${params})`;
662652
}
663653

664-
if (ts.isIdentifier(node.expression) && node.expression.escapedText == "String") {
654+
if (this.transpileExpression((node.expression as ts.PropertyAccessExpression).expression) === "String") {
665655
const params = this.transpileArguments(node.arguments);
666656
return this.transpileStringExpression(node.expression.name) + `(${params})`;
667657
}
668658

659+
switch (expType.flags) {
660+
case ts.TypeFlags.String:
661+
case ts.TypeFlags.StringLiteral:
662+
return this.transpileStringCallExpression(node);
663+
case ts.TypeFlags.Object:
664+
if (tsEx.isArrayType(expType))
665+
return this.transpileArrayCallExpression(node);
666+
}
667+
669668
// Include context parameter if present
670669
let callPath = (expType && expType.symbol) ? `${expType.symbol.name}.${node.expression.name.escapedText}` : this.transpileExpression(node.expression);
671670
let params = this.transpileArguments(node.arguments, node.expression.expression);
@@ -691,7 +690,7 @@ export class LuaTranspiler {
691690
const caller = this.transpileExpression(expression.expression);
692691
switch (expression.name.escapedText) {
693692
case "replace":
694-
return `string.sub(${caller},${params})`;
693+
return `string.gsub(${caller},${params})`;
695694
case "indexOf":
696695
if (node.arguments.length == 1) {
697696
return `(string.find(${caller},${params},1,true) or 0)-1`;
@@ -704,7 +703,7 @@ export class LuaTranspiler {
704703
} else {
705704
const arg1 = this.transpileExpression(node.arguments[0]);
706705
const arg2 = this.transpileExpression(node.arguments[1]);
707-
return `string.sub(${caller},${arg1}+1,${arg2}+1)`;
706+
return `string.sub(${caller},${arg1}+1,${arg2})`;
708707
}
709708
default:
710709
throw new TranspileError("Unsupported string function: " + expression.name.escapedText, node);

test/integration/lua/loops.spec.ts

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,31 @@ const deepEqual = require('deep-equal')
55

66
export class LuaLoopTests {
77

8+
@TestCase([0, 1, 2, 3], [1, 2, 3, 4])
9+
@Test("while")
10+
public while(inp: number[], expected: number[]) {
11+
// Transpile
12+
let lua = util.transpileString(
13+
`let arrTest = ${JSON.stringify(inp)};
14+
let i = 0;
15+
while (i < arrTest.length) {
16+
arrTest[i] = arrTest[i] + 1;
17+
i++;
18+
}
19+
return JSONStringify(arrTest);`
20+
, util.dummyTypes.Array
21+
);
22+
23+
// Execute
24+
let result = util.executeLua(lua);
25+
26+
// Assert
27+
Expect(result).toBe(JSON.stringify(expected));
28+
}
29+
830
@TestCase([0, 1, 2, 3], [1, 2, 3, 4])
931
@Test("for")
10-
public for<T>(inp: T[], expected: T[]) {
32+
public for(inp: number[], expected: number[]) {
1133
// Transpile
1234
let lua = util.transpileString(
1335
`let arrTest = ${JSON.stringify(inp)};
@@ -27,7 +49,7 @@ export class LuaLoopTests {
2749

2850
@TestCase({ ['test1']: 0, ['test2']: 1, ['test3']: 2 }, { ['test1']: 1, ['test2']: 2, ['test3']: 3 })
2951
@Test("forin[Object]")
30-
public forinObject<T>(inp: any, expected: any) {
52+
public forinObject(inp: any, expected: any) {
3153
// Transpile
3254
let lua = util.transpileString(
3355
`let objTest = ${JSON.stringify(inp)};
@@ -45,9 +67,9 @@ export class LuaLoopTests {
4567
Expect(deepEqual(JSON.parse(result), expected)).toBe(true);
4668
}
4769

48-
@TestCase([1,2,3])
70+
@TestCase([1, 2, 3])
4971
@Test("forin[Array]")
50-
public forinArray<T>(inp: T[]) {
72+
public forinArray(inp: number[]) {
5173
// Transpile & Assert
5274
Expect(() => {
5375
let lua = util.transpileString(
@@ -60,9 +82,9 @@ export class LuaLoopTests {
6082
}).toThrowError(Error, "Iterating over arrays with 'for in' is not allowed.");
6183
}
6284

63-
@TestCase([0,1,2], [1,2,3])
85+
@TestCase([0, 1, 2], [1, 2, 3])
6486
@Test("forof")
65-
public forof<T>(inp: any, expected: any) {
87+
public forof(inp: any, expected: any) {
6688
// Transpile
6789
let lua = util.transpileString(
6890
`let objTest = ${JSON.stringify(inp)};
@@ -80,4 +102,5 @@ export class LuaLoopTests {
80102
// Assert
81103
Expect(result).toBe(JSON.stringify(expected));
82104
}
105+
83106
}

test/integration/lua/lualib.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,5 +168,4 @@ export class LuaLibArrayTests {
168168
let joinedInp = inp.join(seperator);
169169
Expect(result).toBe(joinedInp);
170170
}
171-
172171
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import { Expect, Test, TestCase, IgnoreTest } from "alsatian";
2+
import * as util from "../../src/util"
3+
4+
export class StringTests {
5+
6+
@TestCase([])
7+
@TestCase([65])
8+
@TestCase([65, 66])
9+
@TestCase([65, 66, 67])
10+
@Test("String.fromCharCode")
11+
public stringFromCharcode(inp: number[], expected: string) {
12+
// Transpile
13+
let lua = util.transpileString(
14+
`return String.fromCharCode(${inp.toString()})`,
15+
util.dummyTypes.String
16+
);
17+
18+
// Execute
19+
let result = util.executeLua(lua);
20+
21+
// Assert
22+
Expect(result).toBe(String.fromCharCode(...inp));
23+
}
24+
25+
@TestCase("hello test", "", "")
26+
@TestCase("hello test", " ", "")
27+
@TestCase("hello test", "hello", "")
28+
@TestCase("hello test", "test", "")
29+
@TestCase("hello test", "test", "world")
30+
@Test("string.replace")
31+
public replace<T>(inp: string, searchValue: string, replaceValue: string) {
32+
// Transpile
33+
let lua = util.transpileString(
34+
`return "${inp}".replace("${searchValue}", "${replaceValue}")`,
35+
util.dummyTypes.String
36+
);
37+
38+
// Execute
39+
let result = util.executeLua(lua);
40+
41+
// Assert
42+
Expect(result).toBe(inp.replace(searchValue, replaceValue));
43+
}
44+
45+
@TestCase("hello test", new RegExp("123", "g"), "")
46+
@IgnoreTest()
47+
@Test("string.replace[Regex]")
48+
public replaceRegex<T>(inp: string, searchValue: string, replaceValue: string) {
49+
// Transpile
50+
let lua = util.transpileString(
51+
`return "${inp}".replace("${searchValue}", "${replaceValue}")`,
52+
util.dummyTypes.String
53+
);
54+
55+
// Execute
56+
let result = util.executeLua(lua);
57+
58+
// Assert
59+
Expect(result).toBe(inp.replace(searchValue, replaceValue));
60+
}
61+
62+
@TestCase("hello test", "")
63+
@TestCase("hello test", "t")
64+
@TestCase("hello test", "h")
65+
@Test("string.indexOf")
66+
public indexOf<T>(inp: string, searchValue: string) {
67+
// Transpile
68+
let lua = util.transpileString(
69+
`return "${inp}".indexOf("${searchValue}")`,
70+
util.dummyTypes.String
71+
);
72+
73+
// Execute
74+
let result = util.executeLua(lua);
75+
76+
// Assert
77+
Expect(result).toBe(inp.indexOf(searchValue));
78+
}
79+
80+
@TestCase("hello test", 0)
81+
@TestCase("hello test", 1)
82+
@TestCase("hello test", 1, 2)
83+
@TestCase("hello test", 1, 5)
84+
@Test("string.substring")
85+
public substring<T>(inp: string, start: number, end?: number) {
86+
// Transpile
87+
let paramStr = end ? `${start}, ${end}` : `${start}`;
88+
let lua = util.transpileString(
89+
`return "${inp}".substring(${paramStr})`,
90+
util.dummyTypes.String
91+
);
92+
93+
// Execute
94+
let result = util.executeLua(lua);
95+
96+
// Assert
97+
Expect(result).toBe(inp.substring(start, end));
98+
}
99+
}

test/src/util.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export namespace dummyTypes {
99
export const Array = { flags: ts.TypeFlags.Object, symbol: { escapedName: "Array" } };
1010
export const Object = { flags: ts.TypeFlags.Object, symbol: { escapedName: "Object" } };
1111
export const Number = { flags: ts.TypeFlags.Number, symbol: { escapedName: "Number" } };
12+
export const String = { flags: ts.TypeFlags.String, symbol: { escapedName: "String" } };
1213
}
1314

1415
export function transpileString(str: string, dummyType: any = dummyTypes.None): string {

test/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
"experimentalDecorators": true
44
},
55
"exclude": ["integration/testfiles/*"]
6-
}
6+
}

0 commit comments

Comments
 (0)