Skip to content

Commit d0c8e76

Browse files
committed
More tests
1 parent 34c0a15 commit d0c8e76

File tree

4 files changed

+92
-36
lines changed

4 files changed

+92
-36
lines changed

dist/Transpiler.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,12 @@ var LuaTranspiler = /** @class */ (function () {
303303
return result;
304304
};
305305
LuaTranspiler.prototype.transpileReturn = function (node) {
306-
return "return " + this.transpileExpression(node.expression);
306+
if (node.expression) {
307+
return "return " + this.transpileExpression(node.expression);
308+
}
309+
else {
310+
return "return";
311+
}
307312
};
308313
LuaTranspiler.prototype.transpileExpression = function (node, brackets) {
309314
switch (node.kind) {
@@ -379,6 +384,12 @@ var LuaTranspiler = /** @class */ (function () {
379384
case ts.SyntaxKind.MinusEqualsToken:
380385
result = lhs + "=" + lhs + "-" + rhs;
381386
break;
387+
case ts.SyntaxKind.AsteriskEqualsToken:
388+
result = lhs + "=" + lhs + "*" + rhs;
389+
break;
390+
case ts.SyntaxKind.SlashEqualsToken:
391+
result = lhs + "=" + lhs + "/" + rhs;
392+
break;
382393
case ts.SyntaxKind.AmpersandAmpersandToken:
383394
result = lhs + " and " + rhs;
384395
break;
@@ -425,7 +436,7 @@ var LuaTranspiler = /** @class */ (function () {
425436
var condition = this.transpileExpression(node.condition);
426437
var val1 = this.transpileExpression(node.whenTrue);
427438
var val2 = this.transpileExpression(node.whenFalse);
428-
return "TS_ITE(" + condition + ",function() return " + val1 + " end, function() return " + val2 + " end)";
439+
return "TS_ITE(" + condition + ",function() return " + val1 + " end,function() return " + val2 + " end)";
429440
};
430441
// Replace some missmatching operators
431442
LuaTranspiler.prototype.transpileOperator = function (operator) {
@@ -443,9 +454,9 @@ var LuaTranspiler = /** @class */ (function () {
443454
var operand = this.transpileExpression(node.operand, true);
444455
switch (node.operator) {
445456
case ts.SyntaxKind.PlusPlusToken:
446-
return operand + " = " + operand + " + 1";
457+
return operand + "=" + operand + "+1";
447458
case ts.SyntaxKind.MinusMinusToken:
448-
return operand + " = " + operand + " - 1";
459+
return operand + "=" + operand + "-1";
449460
default:
450461
throw new TranspileError("Unsupported unary postfix: " + TSHelper_1.TSHelper.enumName(node.kind, ts.SyntaxKind), node);
451462
}
@@ -454,9 +465,9 @@ var LuaTranspiler = /** @class */ (function () {
454465
var operand = this.transpileExpression(node.operand, true);
455466
switch (node.operator) {
456467
case ts.SyntaxKind.PlusPlusToken:
457-
return operand + " = " + operand + " + 1";
468+
return operand + "=" + operand + "+1";
458469
case ts.SyntaxKind.MinusMinusToken:
459-
return operand + " = " + operand + " - 1";
470+
return operand + "=" + operand + "-1";
460471
case ts.SyntaxKind.ExclamationToken:
461472
return "not " + operand;
462473
case ts.SyntaxKind.MinusToken:

src/Transpiler.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,12 @@ export class LuaTranspiler {
420420
case ts.SyntaxKind.MinusEqualsToken:
421421
result = `${lhs}=${lhs}-${rhs}`;
422422
break;
423+
case ts.SyntaxKind.AsteriskEqualsToken:
424+
result = `${lhs}=${lhs}*${rhs}`;
425+
break;
426+
case ts.SyntaxKind.SlashEqualsToken:
427+
result = `${lhs}=${lhs}/${rhs}`;
428+
break;
423429
case ts.SyntaxKind.AmpersandAmpersandToken:
424430
result = `${lhs} and ${rhs}`;
425431
break;
@@ -467,7 +473,7 @@ export class LuaTranspiler {
467473
let val1 = this.transpileExpression(node.whenTrue);
468474
let val2 = this.transpileExpression(node.whenFalse);
469475

470-
return `TS_ITE(${condition},function() return ${val1} end, function() return ${val2} end)`;
476+
return `TS_ITE(${condition},function() return ${val1} end,function() return ${val2} end)`;
471477
}
472478

473479
// Replace some missmatching operators
@@ -487,9 +493,9 @@ export class LuaTranspiler {
487493
const operand = this.transpileExpression(node.operand, true);
488494
switch (node.operator) {
489495
case ts.SyntaxKind.PlusPlusToken:
490-
return `${operand} = ${operand} + 1`;
496+
return `${operand}=${operand}+1`;
491497
case ts.SyntaxKind.MinusMinusToken:
492-
return `${operand} = ${operand} - 1`;
498+
return `${operand}=${operand}-1`;
493499
default:
494500
throw new TranspileError("Unsupported unary postfix: " + tsEx.enumName(node.kind, ts.SyntaxKind), node);
495501
}
@@ -499,9 +505,9 @@ export class LuaTranspiler {
499505
const operand = this.transpileExpression(node.operand, true);
500506
switch (node.operator) {
501507
case ts.SyntaxKind.PlusPlusToken:
502-
return `${operand} = ${operand} + 1`;
508+
return `${operand}=${operand}+1`;
503509
case ts.SyntaxKind.MinusMinusToken:
504-
return `${operand} = ${operand} - 1`;
510+
return `${operand}=${operand}-1`;
505511
case ts.SyntaxKind.ExclamationToken:
506512
return `not ${operand}`;
507513
case ts.SyntaxKind.MinusToken:

test/unit/expressions.spec.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { Expect, Test, TestCase } from "alsatian";
2+
3+
import * as ts from "typescript";
4+
import {LuaTranspiler, TranspileError} from "../../dist/Transpiler";
5+
6+
const dummyChecker = {getTypeAtLocation: function() {return {};}}
7+
function transpileString(str: string): string {
8+
const file = ts.createSourceFile("", str, ts.ScriptTarget.Latest);
9+
const result = LuaTranspiler.transpileSourceFile(file, dummyChecker, false);
10+
return result.trim();
11+
}
12+
13+
export class ExpressionTests {
14+
15+
@TestCase("i++", "i=i+1")
16+
@TestCase("++i", "i=i+1")
17+
@TestCase("i--", "i=i-1")
18+
@TestCase("--i", "i=i-1")
19+
@TestCase("!a", "not a")
20+
@TestCase("-a", "-a")
21+
@Test("Unary expressions basic")
22+
public unaryBasic(input: string, lua: string) {
23+
Expect(transpileString(input)).toBe(lua);
24+
}
25+
26+
@TestCase("1+1", "1+1")
27+
@TestCase("1-1", "1-1")
28+
@TestCase("1*1", "1*1")
29+
@TestCase("1/1", "1/1")
30+
@TestCase("1%1", "1%1")
31+
@TestCase("1==1", "1==1")
32+
@Test("Binary expressions basic")
33+
public binary(input: string, lua: string) {
34+
Expect(transpileString(input)).toBe(lua);
35+
}
36+
37+
@TestCase("a+=b", "a=a+b")
38+
@TestCase("a-=b", "a=a-b")
39+
@TestCase("a*=b", "a=a*b")
40+
@TestCase("a/=b", "a=a/b")
41+
@TestCase("a&b", "bit.band(a,b)")
42+
@TestCase("a|b", "bit.bor(a,b)")
43+
@Test("Binary expressions overridden operators")
44+
public binaryOperatorOverride(input: string, lua: string) {
45+
Expect(transpileString(input)).toBe(lua);
46+
}
47+
48+
@TestCase("1+1", "1+1")
49+
@TestCase("-1+1", "-1+1")
50+
@TestCase("1*30+4", "(1*30)+4")
51+
@TestCase("1*(3+4)", "1*(3+4)")
52+
@TestCase("1*(3+4*2)", "1*(3+(4*2))")
53+
@Test("Binary expressions ordering parentheses")
54+
public binaryParentheses(input: string, lua: string) {
55+
Expect(transpileString(input)).toBe(lua);
56+
}
57+
58+
@TestCase("1 + a ? 3*a : c", "TS_ITE(1+a,function() return 3*a end,function() return c end)")
59+
@TestCase("a ? b : c", "TS_ITE(a,function() return b end,function() return c end)")
60+
@Test("Ternary operator")
61+
public conditional(input: string, lua: string) {
62+
Expect(transpileString(input)).toBe(lua);
63+
}
64+
}

test/unit/test1.spec.ts

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)