Skip to content

Commit 9d20e41

Browse files
committed
Add few basic tests
1 parent d1a0291 commit 9d20e41

File tree

5 files changed

+71
-14
lines changed

5 files changed

+71
-14
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import * as path from "path";
2+
import * as util from "../../util";
3+
4+
test("printer", () => {
5+
util.testModule``
6+
.setOptions({ luaPlugins: [{ plugin: path.join(__dirname, "printer.ts") }] })
7+
.tap(builder => expect(builder.getMainLuaCodeChunk()).toMatch("Plugin"));
8+
});
9+
10+
test("visitor", () => {
11+
util.testFunction`
12+
return false;
13+
`
14+
.setOptions({ luaPlugins: [{ plugin: path.join(__dirname, "visitor.ts") }] })
15+
.expectToEqual(true);
16+
});
17+
18+
test("visitor using super", () => {
19+
util.testFunction`
20+
return "foo";
21+
`
22+
.setOptions({ luaPlugins: [{ plugin: path.join(__dirname, "visitor-super.ts") }] })
23+
.expectToEqual("bar");
24+
});

test/transpile/plugins/printer.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import * as tstl from "../../../src";
2+
3+
const plugin: tstl.Plugin = {
4+
printer: (program, emitHost, fileName, ...args) => {
5+
const result = new tstl.LuaPrinter(program.getCompilerOptions(), emitHost, fileName).print(...args);
6+
result.code = `-- Plugin\n${result.code}`;
7+
return result;
8+
},
9+
};
10+
11+
// tslint:disable-next-line: no-default-export
12+
export default plugin;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import * as ts from "typescript";
2+
import * as tstl from "../../../src";
3+
4+
const plugin: tstl.Plugin = {
5+
visitors: {
6+
[ts.SyntaxKind.StringLiteral]: (node, context) => {
7+
const result = context.superTransformExpression(node);
8+
9+
if (tstl.isStringLiteral(result)) {
10+
result.value = "bar";
11+
}
12+
13+
return result;
14+
},
15+
},
16+
};
17+
18+
// tslint:disable-next-line: no-default-export
19+
export default plugin;

test/transpile/plugins/visitor.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import * as ts from "typescript";
2+
import * as tstl from "../../../src";
3+
4+
const plugin: tstl.Plugin = {
5+
visitors: {
6+
[ts.SyntaxKind.ReturnStatement]: () => tstl.createReturnStatement([tstl.createBooleanLiteral(true)]),
7+
},
8+
};
9+
10+
// tslint:disable-next-line: no-default-export
11+
export default plugin;

test/transpile/transformers/transformers.spec.ts

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@ import * as path from "path";
22
import * as tstl from "../../../src";
33
import * as util from "../../util";
44

5-
const optionsOfTransformer = (transformer: tstl.TransformerImport): tstl.CompilerOptions => ({
6-
plugins: [transformer],
7-
});
8-
95
test("should ignore language service plugins", () => {
106
util.testFunction`
117
return;
@@ -19,7 +15,7 @@ describe("resolution", () => {
1915
util.testFunction`
2016
return;
2117
`
22-
.setOptions(optionsOfTransformer(transformer))
18+
.setOptions({ plugins: [transformer] })
2319
.expectToEqual(true);
2420
};
2521

@@ -42,25 +38,20 @@ describe("resolution", () => {
4238

4339
test("error if transformer could not be resolved", () => {
4440
util.testModule``
45-
.setOptions(optionsOfTransformer({ transform: path.join(__dirname, "error.ts") }))
41+
.setOptions({ plugins: [{ transform: path.join(__dirname, "error.ts") }] })
4642
.expectToHaveDiagnostics();
4743
});
4844
});
4945

5046
describe("factory types", () => {
5147
for (const type of ["program", "config", "checker", "raw", "compilerOptions"] as const) {
5248
test(type, () => {
53-
const options = optionsOfTransformer({
54-
transform: path.join(__dirname, "types.ts"),
55-
type,
56-
import: type,
57-
value: true,
58-
});
59-
6049
util.testFunction`
6150
return false;
6251
`
63-
.setOptions(options)
52+
.setOptions({
53+
plugins: [{ transform: path.join(__dirname, "types.ts"), type, import: type, value: true }],
54+
})
6455
.expectToEqual(true);
6556
});
6657
}

0 commit comments

Comments
 (0)