Skip to content

Commit 645aa90

Browse files
authored
Merge pull request #81 from Perryvw/func-exp-and-template-string-tests
Added Tests for Function Expressions and Template Strings
2 parents c216ccc + 5b07fe5 commit 645aa90

3 files changed

Lines changed: 48 additions & 16 deletions

File tree

src/Transpiler.ts

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ export class LuaTranspiler {
600600
return this.transpileExpression((node as ts.DeleteExpression).expression) + "=nil";
601601
case ts.SyntaxKind.FunctionExpression:
602602
case ts.SyntaxKind.ArrowFunction:
603-
return this.transpileArrowFunction(node as ts.ArrowFunction);
603+
return this.transpileFunctionExpression(node as ts.ArrowFunction);
604604
case ts.SyntaxKind.NewExpression:
605605
return this.transpileNewExpression(node as ts.NewExpression);
606606
case ts.SyntaxKind.ComputedPropertyName:
@@ -1363,21 +1363,7 @@ export class LuaTranspiler {
13631363
return "{" + properties.join(",") + "}";
13641364
}
13651365

1366-
public transpileFunctionExpression(node: ts.FunctionExpression): string {
1367-
// Build parameter string
1368-
const paramNames: string[] = [];
1369-
node.parameters.forEach((param) => {
1370-
paramNames.push((param.name as ts.Identifier).escapedText as string);
1371-
});
1372-
1373-
let result = `function(${paramNames.join(",")})\n`;
1374-
this.pushIndent();
1375-
result += this.transpileBlock(node.body);
1376-
this.popIndent();
1377-
return result + this.indent + "end\n";
1378-
}
1379-
1380-
public transpileArrowFunction(node: ts.ArrowFunction): string {
1366+
public transpileFunctionExpression(node: ts.ArrowFunction): string {
13811367
// Build parameter string
13821368
const paramNames: string[] = [];
13831369
node.parameters.forEach((param) => {

test/unit/expressions.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,28 @@ export class ExpressionTests {
128128
public conditional(input: string, lua: string) {
129129
Expect(util.transpileString(input)).toBe(lua);
130130
}
131+
132+
@Test("Arrow Function Expression")
133+
public arrowFunctionExpression(input: string) {
134+
// Transpile
135+
const lua = util.transpileString(`let add = (a, b) => a+b; return add(1,2);`);
136+
137+
// Execute
138+
const result = util.executeLua(lua);
139+
140+
// Assert
141+
Expect(result).toBe(3);
142+
}
143+
144+
@Test("Function Expression")
145+
public functionExpression(input: string) {
146+
// Transpile
147+
const lua = util.transpileString(`let add = function(a, b) {return a+b}; return add(1,2);`);
148+
149+
// Execute
150+
const result = util.executeLua(lua);
151+
152+
// Assert
153+
Expect(result).toBe(3);
154+
}
131155
}

test/unit/string.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,28 @@ export class StringTests {
2121
Expect(result).toBe(String.fromCharCode(...inp));
2222
}
2323

24+
@TestCase(12, 23, 43)
25+
@TestCase("test", "hello", "bye")
26+
@TestCase("test", 42, "bye")
27+
@TestCase("test", 42, 12)
28+
@Test("Template Strings")
29+
public templateStrings(a: any, b: any, c: any) {
30+
// Transpile
31+
const a1 = typeof(a) === "string" ? "'" + a + "'" : a;
32+
const b1 = typeof(b) === "string" ? "'" + b + "'" : b;
33+
const c1 = typeof(c) === "string" ? "'" + c + "'" : c;
34+
35+
let lua = util.transpileString(
36+
"let a = " + a1 + "; let b = " + b1 + "; let c = " + c1 + "; return `${a} ${b} test ${c}`;"
37+
);
38+
39+
// Execute
40+
let result = util.executeLua(lua);
41+
42+
// Assert
43+
Expect(result).toBe(`${a} ${b} test ${c}`);
44+
}
45+
2446
@TestCase("hello test", "", "")
2547
@TestCase("hello test", " ", "")
2648
@TestCase("hello test", "hello", "")

0 commit comments

Comments
 (0)