Skip to content

Commit 2ff8dd6

Browse files
committed
conditional tests
1 parent 1c3fd47 commit 2ff8dd6

File tree

8 files changed

+216
-20
lines changed

8 files changed

+216
-20
lines changed

dist/TSHelper.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ var TSHelper = /** @class */ (function () {
2828
}
2929
return "unknown";
3030
};
31+
TSHelper.containsStatement = function (statements, kind) {
32+
return statements.some(function (statement) { return statement.kind === kind; });
33+
};
3134
TSHelper.isFileModule = function (sourceFile) {
3235
if (sourceFile) {
3336
// Vanilla ts flags files as external module if they have an import or

dist/Transpiler.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,11 @@ var LuaTranspiler = /** @class */ (function () {
314314
var result = this.indent + "-------Switch statement start-------\n";
315315
// If statement to go to right entry label
316316
clauses.forEach(function (clause, index) {
317+
if (index !== clauses.length - 1
318+
&& clause.statements.length !== 0
319+
&& !TSHelper_1.TSHelper.containsStatement(clause.statements, ts.SyntaxKind.BreakStatement)) {
320+
throw new TranspileError("Missing break, fall through is not allowed.", clause);
321+
}
317322
if (ts.isCaseClause(clause)) {
318323
var keyword = index == 0 ? "if" : "elseif";
319324
var condition = _this.transpileExpression(clause.expression, true);
@@ -324,17 +329,11 @@ var LuaTranspiler = /** @class */ (function () {
324329
result += _this.indent + "else\n";
325330
}
326331
_this.pushIndent();
327-
// Labels for fallthrough
328-
result += _this.indent + ("::switchCase" + (_this.genVarCounter + index) + "::\n");
329332
_this.transpilingSwitch = true;
330333
clause.statements.forEach(function (statement) {
331334
result += _this.transpileNode(statement);
332335
});
333336
_this.transpilingSwitch = false;
334-
// If this goto is reached, fall through to the next case
335-
if (index < clauses.length - 1) {
336-
result += _this.indent + ("goto switchCase" + (_this.genVarCounter + index + 1) + "\n");
337-
}
338337
_this.popIndent();
339338
});
340339
result += this.indent + "end\n";

src/Compiler.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function compile(fileNames: string[], options: ts.CompilerOptions, projectRoot:
3434
}
3535

3636
program.getSourceFiles().forEach(sourceFile => {
37-
if (!sourceFile.isDeclarationFile) {
37+
if (!sourceFile.isDeclarationFile) {
3838
// Print AST for debugging
3939
//printAST(sourceFile, 0);
4040

@@ -43,7 +43,7 @@ function compile(fileNames: string[], options: ts.CompilerOptions, projectRoot:
4343
const addHeader = options.noHeader === true ? false : true;
4444
let lua = LuaTranspiler.transpileSourceFile(sourceFile, checker, addHeader);
4545
let outPath = sourceFile.fileName.substring(0, sourceFile.fileName.lastIndexOf(".")) + ".lua";
46-
46+
4747
if (options.outDir) {
4848
var extension = options.outDir;
4949
if (extension[extension.length - 1] != "/") extension = extension + "/";
@@ -83,7 +83,7 @@ function printAST(node: ts.Node, indent: number) {
8383
const filename = process.argv[2].split("\\").join("/");
8484
const filepath = filename.substring(0, filename.lastIndexOf("/"));
8585
let configPath = ts.findConfigFile(filepath, ts.sys.fileExists);
86-
86+
8787
if (configPath) {
8888
configPath = configPath.split("\\").join("/");
8989
const projectRoot = configPath.substring(0, configPath.lastIndexOf("/"));

src/TSHelper.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ export class TSHelper {
2929
return "unknown";
3030
}
3131

32+
static containsStatement(statements: ts.NodeArray<ts.Statement>, kind: ts.SyntaxKind): boolean {
33+
return statements.some(statement => statement.kind === kind);
34+
}
35+
3236
static isFileModule(sourceFile: ts.SourceFile) {
3337
if (sourceFile) {
3438
// Vanilla ts flags files as external module if they have an import or

src/Transpiler.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,11 @@ export class LuaTranspiler {
349349

350350
// If statement to go to right entry label
351351
clauses.forEach((clause, index) => {
352+
if (index !== clauses.length - 1
353+
&& clause.statements.length !== 0
354+
&& !tsEx.containsStatement(clause.statements, ts.SyntaxKind.BreakStatement)) {
355+
throw new TranspileError("Missing break, fall through is not allowed.", clause);
356+
}
352357
if (ts.isCaseClause(clause)) {
353358
let keyword = index == 0 ? "if" : "elseif";
354359
let condition = this.transpileExpression(clause.expression, true);
@@ -360,20 +365,12 @@ export class LuaTranspiler {
360365

361366
this.pushIndent();
362367

363-
// Labels for fallthrough
364-
result += this.indent + `::switchCase${this.genVarCounter + index}::\n`;
365-
366368
this.transpilingSwitch = true;
367369
clause.statements.forEach(statement => {
368370
result += this.transpileNode(statement);
369371
});
370372
this.transpilingSwitch = false;
371373

372-
// If this goto is reached, fall through to the next case
373-
if (index < clauses.length - 1) {
374-
result += this.indent + `goto switchCase${this.genVarCounter + index + 1}\n`;
375-
}
376-
377374
this.popIndent();
378375
});
379376
result += this.indent + "end\n";
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
import { Expect, Test, TestCase } from "alsatian";
2+
import * as util from "../../src/util"
3+
4+
export class LuaConditionalsTests {
5+
6+
@TestCase(0, 0)
7+
@TestCase(1, 1)
8+
@Test("if")
9+
public if(inp: number, expected: number) {
10+
// Transpile
11+
let lua = util.transpileString(
12+
`let input = ${inp}
13+
if (input === 0) {
14+
return 0;
15+
}
16+
return 1;`
17+
, util.dummyTypes.Number
18+
);
19+
20+
// Execute
21+
let result = util.executeLua(lua);
22+
23+
// Assert
24+
Expect(result).toBe(expected);
25+
}
26+
27+
@TestCase(0, 0)
28+
@TestCase(1, 1)
29+
@Test("ifelse")
30+
public ifelse(inp: number, expected: number) {
31+
// Transpile
32+
let lua = util.transpileString(
33+
`let input = ${inp}
34+
if (input === 0) {
35+
return 0;
36+
} else {
37+
return 1;
38+
}`
39+
, util.dummyTypes.Number
40+
);
41+
42+
// Execute
43+
let result = util.executeLua(lua);
44+
45+
// Assert
46+
Expect(result).toBe(expected);
47+
}
48+
49+
@TestCase(0, 0)
50+
@TestCase(1, 1)
51+
@TestCase(2, 2)
52+
@TestCase(3, 3)
53+
@Test("ifelseif")
54+
public ifelseif(inp: number, expected: number) {
55+
// Transpile
56+
let lua = util.transpileString(
57+
`let input = ${inp}
58+
if (input === 0) {
59+
return 0;
60+
} else if (input === 1){
61+
return 1;
62+
} else if (input === 2){
63+
return 2;
64+
}
65+
return 3;`
66+
, util.dummyTypes.Number
67+
);
68+
69+
// Execute
70+
let result = util.executeLua(lua);
71+
72+
// Assert
73+
Expect(result).toBe(expected);
74+
}
75+
76+
@TestCase(0, 0)
77+
@TestCase(1, 1)
78+
@TestCase(2, 2)
79+
@TestCase(3, 3)
80+
@Test("ifelseifelse")
81+
public ifelseifelse(inp: number, expected: number) {
82+
// Transpile
83+
let lua = util.transpileString(
84+
`let input = ${inp}
85+
if (input === 0) {
86+
return 0;
87+
} else if (input === 1){
88+
return 1;
89+
} else if (input === 2){
90+
return 2;
91+
} else {
92+
return 3;
93+
}`
94+
, util.dummyTypes.Number
95+
);
96+
97+
// Execute
98+
let result = util.executeLua(lua);
99+
100+
// Assert
101+
Expect(result).toBe(expected);
102+
}
103+
104+
@TestCase(0, 0)
105+
@TestCase(1, 1)
106+
@TestCase(2, 2)
107+
@TestCase(3, -1)
108+
@Test("switch")
109+
public switch(inp: number, expected: number) {
110+
// Transpile
111+
let lua = util.transpileString(
112+
`let result = -1;
113+
114+
switch (${inp}) {
115+
case 0:
116+
result = 0;
117+
break;
118+
case 1:
119+
result = 1;
120+
break;
121+
case 2:
122+
result = 2;
123+
break;
124+
}
125+
return result;`
126+
, util.dummyTypes.Number
127+
);
128+
129+
// Execute
130+
let result = util.executeLua(lua);
131+
132+
// Assert
133+
Expect(result).toBe(expected);
134+
}
135+
136+
@TestCase(0, 0)
137+
@TestCase(1, 1)
138+
@TestCase(2, 2)
139+
@TestCase(3, -2)
140+
@Test("switchdefault")
141+
public switchdefault(inp: number, expected: number) {
142+
// Transpile
143+
let lua = util.transpileString(
144+
`let result = -1;
145+
146+
switch (${inp}) {
147+
case 0:
148+
result = 0;
149+
break;
150+
case 1:
151+
result = 1;
152+
break;
153+
case 2:
154+
result = 2;
155+
break;
156+
default:
157+
result = -2;
158+
break;
159+
}
160+
return result;`
161+
, util.dummyTypes.Number
162+
);
163+
164+
// Execute
165+
let result = util.executeLua(lua);
166+
167+
// Assert
168+
Expect(result).toBe(expected);
169+
}
170+
171+
@TestCase(0, 0)
172+
@Test("switchfallthrough")
173+
public switchfallthrough(inp: number, expected: number) {
174+
// Transpile & Assert
175+
Expect(() => util.transpileString(
176+
`let result = -1;
177+
178+
switch (${inp}) {
179+
case 0:
180+
result = 0;
181+
case 1:
182+
result = 1;
183+
break;
184+
}
185+
return result;`
186+
, util.dummyTypes.Number
187+
)).toThrowError(Error, "Missing break, fall through is not allowed.")
188+
}
189+
190+
191+
}

test/integration/lua/modules.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export class LuaModuleTests {
132132
let lua = util.transpileString(inp, util.dummyTypes.Object);
133133

134134
// Assert
135-
// Dont test for correct indention this allows easier tes case definition
135+
// Dont test for correct indention this allows easier test case definition
136136
Expect(dedent(lua)).toBe(dedent(expected));
137137
}
138138
}

test/src/util.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ const LuaVM = require("lua.vm.js");
55
const fs = require("fs");
66

77
export namespace dummyTypes {
8+
export const None = {};
89
export const Array = { flags: ts.TypeFlags.Object, symbol: { escapedName: "Array" } };
9-
export const Object = { flags: ts.TypeFlags.Object, symbol: { escapedName: "Object" } }
10+
export const Object = { flags: ts.TypeFlags.Object, symbol: { escapedName: "Object" } };
11+
export const Number = { flags: ts.TypeFlags.Number, symbol: { escapedName: "Number" } };
1012
}
1113

1214
export function transpileString(str: string, dummyType: any): string {
@@ -16,7 +18,7 @@ export function transpileString(str: string, dummyType: any): string {
1618
return result.trim();
1719
}
1820

19-
export function executeLua(lua: string, withLib = true): string {
21+
export function executeLua(lua: string, withLib = true): any {
2022
if (withLib) {
2123
lua = minimalTestLib + lua
2224
}

0 commit comments

Comments
 (0)