-
-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathloading.spec.ts
More file actions
87 lines (77 loc) · 3.31 KB
/
loading.spec.ts
File metadata and controls
87 lines (77 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import * as tstl from "../../../src";
import { unsupportedProperty } from "../../../src/transformation/utils/diagnostics";
import * as util from "../../util";
describe("luaLibImport", () => {
test("inline", () => {
util.testExpression`[0].indexOf(1)`
.setOptions({ luaLibImport: tstl.LuaLibImportKind.Inline })
.tap(builder => expect(builder.getMainLuaCodeChunk()).not.toContain('require("lualib_bundle")'))
.expectToMatchJsResult();
});
test("require", () => {
util.testExpression`[0].indexOf(1)`
.setOptions({ luaLibImport: tstl.LuaLibImportKind.Require })
.tap(builder => expect(builder.getMainLuaCodeChunk()).toContain('require("lualib_bundle")'))
.expectToMatchJsResult();
});
function testLualibOnlyHasArrayIndexOf(builder: util.TestBuilder) {
const lualibBundle = builder.getLuaResult().transpiledFiles.find(f => f.outPath.endsWith("lualib_bundle.lua"))!;
expect(lualibBundle).toBeDefined();
expect(lualibBundle.lua).toEqual(expect.stringMatching("^local function __TS__ArrayIndexOf"));
expect(lualibBundle.lua).not.toContain("__TS__ArrayConcat");
expect(lualibBundle.lua).not.toContain("Error");
}
test("require-minimal", () => {
util.testExpression`[0].indexOf(1)`
.setOptions({ luaLibImport: tstl.LuaLibImportKind.RequireMinimal })
.tap(builder => expect(builder.getMainLuaCodeChunk()).toContain('require("lualib_bundle")'))
.tap(testLualibOnlyHasArrayIndexOf)
.expectToMatchJsResult();
});
test("require-minimal with lualib in another file", () => {
util.testModule`
import "./other";
`
.setOptions({ luaLibImport: tstl.LuaLibImportKind.RequireMinimal })
.addExtraFile(
"other.lua",
`
local ____lualib = require("lualib_bundle")
local __TS__ArrayIndexOf = ____lualib.__TS__ArrayIndexOf
__TS__ArrayIndexOf({}, 1)
`
)
// note: indent matters in above code, because searching for lualib checks for start & end of line
.tap(testLualibOnlyHasArrayIndexOf)
.expectNoExecutionError();
});
});
test.each([
tstl.LuaLibImportKind.Inline,
tstl.LuaLibImportKind.None,
tstl.LuaLibImportKind.Require,
tstl.LuaLibImportKind.RequireMinimal,
])("should not include lualib without code (%p)", luaLibImport => {
util.testModule``.setOptions({ luaLibImport }).tap(builder => expect(builder.getMainLuaCodeChunk()).toBe(""));
});
test("lualib should not include tstl header", () => {
util.testExpression`[0].indexOf(1)`.tap(builder =>
expect(builder.getMainLuaCodeChunk()).not.toContain("Generated with")
);
});
test("using lualib does not crash when coroutine is not defined", () => {
util.testModule`
declare const _G: any;
declare function require(this: void, name: string): any
_G.coroutine = undefined;
require("lualib_bundle");
export const result = 1
`.expectToEqual({ result: 1 });
});
describe("Unknown builtin property", () => {
test("access", () => {
util.testExpression`Math.unknownProperty`
.disableSemanticCheck()
.expectDiagnosticsToMatchSnapshot([unsupportedProperty.code]);
});
});