Skip to content

Commit 40ee16c

Browse files
committed
Improved and added tests
1 parent f94d0c8 commit 40ee16c

File tree

10 files changed

+126
-42
lines changed

10 files changed

+126
-42
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Test = Test or {}
2+
Test.__index = Test
3+
function Test.new(construct, ...)
4+
local instance = setmetatable({}, Test)
5+
if construct and Test.constructor then Test.constructor(instance, ...) end
6+
return instance
7+
end
8+
function Test.constructor(self,field)
9+
self.field = field
10+
end
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
val1=0
22
val2=2
3-
val3=3
3+
val3=3
4+
local a = val1
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function myFunc()
2+
return
3+
end
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Test {
2+
constructor(private field: number) {}
3+
}

test/translation/ts/enumMembersOnly.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ enum TestEnum {
33
val1 = 0,
44
val2 = 2,
55
val3
6-
}
6+
}
7+
8+
const a = TestEnum.val1;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function myFunc() {
2+
return;
3+
}

test/unit/assignments.spec.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Expect, Test, TestCase } from "alsatian";
1+
import { Expect, Test, TestCase, FocusTest } from "alsatian";
2+
import { TranspileError } from "../../src/Transpiler";
23

34
import * as util from "../src/util";
45
const fs = require("fs");
@@ -52,4 +53,24 @@ export class AssignmentTests {
5253
const result = util.executeLua(lua);
5354
Expect(result).toBe(undefined);
5455
}
56+
57+
@TestCase(["a", "b"], ["e", "f"])
58+
@TestCase(["a", "b"], ["e", "f", "g"])
59+
@TestCase(["a", "b", "c"], ["e", "f", "g"])
60+
@Test("Binding pattern assignment")
61+
public bindingPattern(input: string[], values: string[]) {
62+
const pattern = input.join(",");
63+
const initializer = values.map(v => `"${v}"`).join(",");
64+
65+
const lua = util.transpileString(`const [${pattern}] = [${initializer}]; return [${pattern}].join("-");`);
66+
const result = util.executeLua(lua);
67+
68+
Expect(result).toBe(values.slice(0, input.length).join("-"));
69+
}
70+
71+
@Test("Ellipsis binding pattern")
72+
public ellipsisBindingPattern() {
73+
Expect(() => util.transpileString("let [a,b,...c] = [1,2,3];"))
74+
.toThrowError(Error, "Ellipsis destruction is not allowed.");
75+
}
5576
}

test/unit/error.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export class LuaErrorTests {
66
@Test("throwString")
77
public trowString() {
88
// Transpile
9-
let lua = util.transpileString(
9+
const lua = util.transpileString(
1010
`throw "Some Error"`
1111
);
1212
// Assert
@@ -17,7 +17,7 @@ export class LuaErrorTests {
1717
public throwError() {
1818
// Transpile & Asser
1919
Expect(() => {
20-
let lua = util.transpileString(
20+
const lua = util.transpileString(
2121
`throw Error("Some Error")`
2222
);
2323
}).toThrowError(Error, "Unsupported throw expression, only string literals are supported");

test/unit/expressions.spec.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Expect, Test, TestCase, FocusTest } from "alsatian";
1+
import { Expect, Test, TestCase } from "alsatian";
22
import { LuaTarget } from "../../src/Transpiler";
33

44
import * as ts from "typescript";
@@ -175,6 +175,16 @@ export class ExpressionTests {
175175
Expect(result).toBe(3);
176176
}
177177

178+
@Test("Null Expression")
179+
public nullExpression() {
180+
Expect(util.transpileString("null")).toBe("nil");
181+
}
182+
183+
@Test("Undefined Expression")
184+
public undefinedExpression() {
185+
Expect(util.transpileString("undefined")).toBe("nil");
186+
}
187+
178188
@TestCase([], 7)
179189
@TestCase([5], 9)
180190
@TestCase([1, 2], 3)
@@ -230,6 +240,7 @@ export class ExpressionTests {
230240
}
231241

232242
@TestCase("= 4", 4 + 4)
243+
@TestCase("-= 3", 4 - 3 + 4)
233244
@TestCase("+= 3", 4 + 3 + 4)
234245
@TestCase("*= 3", 4 * 3 + 4)
235246
@TestCase("/= 3", 4 / 3 + 4)

test/unit/string.spec.ts

Lines changed: 66 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Expect, Test, TestCase, IgnoreTest } from "alsatian";
2-
import * as util from "../src/util"
1+
import { Expect, Test, TestCase } from "alsatian";
2+
import * as util from "../src/util";
33

44
export class StringTests {
55

@@ -20,12 +20,12 @@ export class StringTests {
2020
@Test("String.fromCharCode")
2121
public stringFromCharcode(inp: number[], expected: string) {
2222
// Transpile
23-
let lua = util.transpileString(
24-
`return String.fromCharCode(${inp.toString()})`,
23+
const lua = util.transpileString(
24+
`return String.fromCharCode(${inp.toString()})`
2525
);
2626

2727
// Execute
28-
let result = util.executeLua(lua);
28+
const result = util.executeLua(lua);
2929

3030
// Assert
3131
Expect(result).toBe(String.fromCharCode(...inp));
@@ -42,12 +42,12 @@ export class StringTests {
4242
const b1 = typeof(b) === "string" ? "'" + b + "'" : b;
4343
const c1 = typeof(c) === "string" ? "'" + c + "'" : c;
4444

45-
let lua = util.transpileString(
45+
const lua = util.transpileString(
4646
"let a = " + a1 + "; let b = " + b1 + "; let c = " + c1 + "; return `${a} ${b} test ${c}`;"
4747
);
4848

4949
// Execute
50-
let result = util.executeLua(lua);
50+
const result = util.executeLua(lua);
5151

5252
// Assert
5353
Expect(result).toBe(`${a} ${b} test ${c}`);
@@ -61,12 +61,12 @@ export class StringTests {
6161
@Test("string.replace")
6262
public replace<T>(inp: string, searchValue: string, replaceValue: string) {
6363
// Transpile
64-
let lua = util.transpileString(
65-
`return "${inp}".replace("${searchValue}", "${replaceValue}")`,
64+
const lua = util.transpileString(
65+
`return "${inp}".replace("${searchValue}", "${replaceValue}")`
6666
);
6767

6868
// Execute
69-
let result = util.executeLua(lua);
69+
const result = util.executeLua(lua);
7070

7171
// Assert
7272
Expect(result).toBe(inp.replace(searchValue, replaceValue));
@@ -79,15 +79,15 @@ export class StringTests {
7979
@TestCase([42, "hello"], "42hello")
8080
@Test("string.concat[+]")
8181
public concat(inp: any[], expected: string) {
82-
let concatStr = inp.map(elem => typeof(elem) === "string" ? `"${elem}"` : elem).join(" + ");
82+
const concatStr = inp.map(elem => typeof(elem) === "string" ? `"${elem}"` : elem).join(" + ");
8383

8484
// Transpile
85-
let lua = util.transpileString(
86-
`return ${concatStr}`,
85+
const lua = util.transpileString(
86+
`return ${concatStr}`
8787
);
8888

8989
// Execute
90-
let result = util.executeLua(lua);
90+
const result = util.executeLua(lua);
9191

9292
// Assert
9393
Expect(result).toBe(expected);
@@ -100,31 +100,49 @@ export class StringTests {
100100
@Test("string.indexOf")
101101
public indexOf(inp: string, searchValue: string) {
102102
// Transpile
103-
let lua = util.transpileString(
104-
`return "${inp}".indexOf("${searchValue}")`,
103+
const lua = util.transpileString(
104+
`return "${inp}".indexOf("${searchValue}")`
105105
);
106106

107107
// Execute
108-
let result = util.executeLua(lua);
108+
const result = util.executeLua(lua);
109109

110110
// Assert
111111
Expect(result).toBe(inp.indexOf(searchValue));
112112
}
113113

114+
@TestCase("hello test", "t", 5)
115+
@TestCase("hello test", "t", 6)
116+
@TestCase("hello test", "t", 7)
117+
@TestCase("hello test", "h", 4)
118+
@Test("string.indexOf with offset")
119+
public indexOfOffset(inp: string, searchValue: string, offset: number) {
120+
// Transpile
121+
const lua = util.transpileString(
122+
`return "${inp}".indexOf("${searchValue}", ${offset})`
123+
);
124+
125+
// Execute
126+
const result = util.executeLua(lua);
127+
128+
// Assert
129+
Expect(result).toBe(inp.indexOf(searchValue, offset));
130+
}
131+
114132
@TestCase("hello test", 0)
115133
@TestCase("hello test", 1)
116134
@TestCase("hello test", 1, 2)
117135
@TestCase("hello test", 1, 5)
118136
@Test("string.substring")
119137
public substring(inp: string, start: number, end?: number) {
120138
// Transpile
121-
let paramStr = end ? `${start}, ${end}` : `${start}`;
122-
let lua = util.transpileString(
123-
`return "${inp}".substring(${paramStr})`,
139+
const paramStr = end ? `${start}, ${end}` : `${start}`;
140+
const lua = util.transpileString(
141+
`return "${inp}".substring(${paramStr})`
124142
);
125143

126144
// Execute
127-
let result = util.executeLua(lua);
145+
const result = util.executeLua(lua);
128146

129147
// Assert
130148
Expect(result).toBe(inp.substring(start, end));
@@ -136,12 +154,12 @@ export class StringTests {
136154
@Test("string.length")
137155
public length(inp: string, expected: number) {
138156
// Transpile
139-
let lua = util.transpileString(
140-
`return "${inp}".length`,
157+
const lua = util.transpileString(
158+
`return "${inp}".length`
141159
);
142160

143161
// Execute
144-
let result = util.executeLua(lua);
162+
const result = util.executeLua(lua);
145163

146164
// Assert
147165
Expect(result).toBe(inp.length);
@@ -151,12 +169,12 @@ export class StringTests {
151169
@Test("string.toLowerCase")
152170
public toLowerCase(inp: string) {
153171
// Transpile
154-
let lua = util.transpileString(
155-
`return "${inp}".toLowerCase()`,
172+
const lua = util.transpileString(
173+
`return "${inp}".toLowerCase()`
156174
);
157175

158176
// Execute
159-
let result = util.executeLua(lua);
177+
const result = util.executeLua(lua);
160178

161179
// Assert
162180
Expect(result).toBe(inp.toLowerCase());
@@ -166,12 +184,12 @@ export class StringTests {
166184
@Test("string.toUpperCase")
167185
public toUpperCase(inp: string) {
168186
// Transpile
169-
let lua = util.transpileString(
170-
`return "${inp}".toUpperCase()`,
187+
const lua = util.transpileString(
188+
`return "${inp}".toUpperCase()`
171189
);
172190

173191
// Execute
174-
let result = util.executeLua(lua);
192+
const result = util.executeLua(lua);
175193

176194
// Assert
177195
Expect(result).toBe(inp.toUpperCase());
@@ -187,12 +205,12 @@ export class StringTests {
187205
@Test("string.split")
188206
public split(inp: string, separator: string) {
189207
// Transpile
190-
let lua = util.transpileString(
191-
`return JSONStringify("${inp}".split("${separator}"))`,
208+
const lua = util.transpileString(
209+
`return JSONStringify("${inp}".split("${separator}"))`
192210
);
193211

194212
// Execute
195-
let result = util.executeLua(lua);
213+
const result = util.executeLua(lua);
196214

197215
// Assert
198216
Expect(result).toBe(JSON.stringify(inp.split(separator)));
@@ -205,15 +223,27 @@ export class StringTests {
205223
@Test("string.charAt")
206224
public charAt(inp: string, index: number) {
207225
// Transpile
208-
let lua = util.transpileString(
209-
`return "${inp}".charAt(${index})`,
226+
const lua = util.transpileString(
227+
`return "${inp}".charAt(${index})`
210228
);
211229

212230
// Execute
213-
let result = util.executeLua(lua);
231+
const result = util.executeLua(lua);
214232

215233
// Assert
216234
Expect(result).toBe(inp.charAt(index));
217235
}
218236

237+
@TestCase("abcd", 3)
238+
@TestCase("abcde", 3)
239+
@TestCase("abcde", 0)
240+
@TestCase("a", 0)
241+
@Test("string index")
242+
public index(input: string, index: number) {
243+
const lua = util.transpileString(`return "${input}"[${index}];`);
244+
245+
const result = util.executeLua(lua);
246+
247+
Expect(result).toBe(input[index]);
248+
}
219249
}

0 commit comments

Comments
 (0)