forked from TypeScriptToLua/TypeScriptToLua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodules.spec.ts
More file actions
211 lines (187 loc) · 5.99 KB
/
modules.spec.ts
File metadata and controls
211 lines (187 loc) · 5.99 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import * as ts from "typescript";
import * as util from "../../util";
describe("module import/export elision", () => {
const moduleDeclaration = `
declare module "module" {
export type Type = string;
export declare const value: string;
}
`;
const expectToElideImport: util.TapCallback = builder => {
builder.addExtraFile("module.d.ts", moduleDeclaration).setOptions({ module: ts.ModuleKind.CommonJS });
expect(builder.getLuaExecutionResult()).not.toBeInstanceOf(util.ExecutionError);
};
test("should elide named type imports", () => {
util.testModule`
import { Type } from "module";
const foo: Type = "bar";
`.tap(expectToElideImport);
});
test("should elide named value imports used only as a type", () => {
util.testModule`
import { value } from "module";
const foo: typeof value = "bar";
`.tap(expectToElideImport);
});
test("should elide namespace imports with unused values", () => {
util.testModule`
import * as module from "module";
const foo: module.Type = "bar";
`.tap(expectToElideImport);
});
test("should elide `import =` declarations", () => {
util.testModule`
import module = require("module");
const foo: module.Type = "bar";
`.tap(expectToElideImport);
});
test("should elide type exports", () => {
util.testModule`
(globalThis as any).foo = true;
type foo = boolean;
export { foo };
`.expectToEqual([]);
});
});
test.each(["ke-bab", "dollar$", "singlequote'", "hash#", "s p a c e", "ɥɣɎɌͼƛಠ", "_̀ः٠‿"])(
"Import module names with invalid lua identifier characters (%p)",
name => {
util.testModule`
import { foo } from "./${name}";
export { foo };
`
.disableSemanticCheck()
.setLuaHeader(`setmetatable(package.loaded, { __index = function() return { foo = "bar" } end })`)
.setReturnExport("foo")
.expectToEqual("bar");
}
);
test.each(["export default value;", "export { value as default };"])("Export Default From (%p)", exportStatement => {
const [result] = util.transpileAndExecuteProjectReturningMainExport(
{
"main.ts": `
export { default } from "./module";
`,
"module.ts": `
export const value = true;
${exportStatement};
`,
},
"default"
);
expect(result).toBe(true);
});
test("Default Import and Export Expression", () => {
const [result] = util.transpileAndExecuteProjectReturningMainExport(
{
"main.ts": `
import defaultExport from "./module";
export const value = defaultExport;
`,
"module.ts": `
export default 1 + 2 + 3;
`,
},
"value"
);
expect(result).toBe(6);
});
test("Import and Export Assignment", () => {
const [result] = util.transpileAndExecuteProjectReturningMainExport(
{
"main.ts": `
import * as m from "./module";
export const value = m;
`,
"module.ts": `
export = true;
`,
},
"value"
);
expect(result).toBe(true);
});
test("Mixed Exports, Default and Named Imports", () => {
const [result] = util.transpileAndExecuteProjectReturningMainExport(
{
"main.ts": `
import defaultExport, { a, b, c } from "./module";
export const value = defaultExport + b + c;
`,
"module.ts": `
export const a = 1;
export const b = 2;
export const c = 3;
export default a;
`,
},
"value"
);
expect(result).toBe(6);
});
test("Mixed Exports, Default and Namespace Import", () => {
const [result] = util.transpileAndExecuteProjectReturningMainExport(
{
"main.ts": `
import defaultExport, * as ns from "./module";
export const value = defaultExport + ns.b + ns.c;
`,
"module.ts": `
export const a = 1;
export const b = 2;
export const c = 3;
export default a;
`,
},
"value"
);
expect(result).toBe(6);
});
test("Export Default Function", () => {
const [result] = util.transpileAndExecuteProjectReturningMainExport(
{
"main.ts": `
import defaultExport from "./module";
export const value = defaultExport();
`,
"module.ts": `
export default function() {
return true;
}
`,
},
"value"
);
expect(result).toBe(true);
});
test.each([
["Test", "export default class Test { static method() { return true; } }"],
["default", "export default class { static method() { return true; } }"],
])("Export Default Class Name (%p)", (expectedClassName, classDeclarationStatement) => {
const [result] = util.transpileAndExecuteProjectReturningMainExport(
{
"main.ts": `
import defaultExport from "./module";
export const value = defaultExport.name;
`,
"module.ts": classDeclarationStatement,
},
"value"
);
expect(result).toBe(expectedClassName);
});
test("Export Equals", () => {
const [result] = util.transpileAndExecuteProjectReturningMainExport(
{
"main.ts": `
import * as module from "./module";
export const value = module;
`,
"module.ts": `
export = true;
`,
},
"value"
);
expect(result).toBe(true);
});