|
1 | 1 | import * as util from "../util"; |
2 | 2 | import * as ts from "typescript"; |
| 3 | +import { CompilerOptions } from "../../src/CompilerOptions"; |
3 | 4 |
|
4 | | -const exportValueSource = ` |
5 | | - export const value = true; |
6 | | -`; |
| 5 | +const exportValueSource = "export const value = true;"; |
| 6 | +const reexportValueSource = 'export { value } from "./export";'; |
| 7 | +const validateValueSource = 'if (value !== true) { throw "Failed to import value" }'; |
7 | 8 |
|
8 | | -const reexportValueSource = ` |
9 | | - export { value } from "./export"; |
10 | | -`; |
| 9 | +function outFileOptionsWithEntry(entry: string): CompilerOptions { |
| 10 | + return { outFile: "main.lua", noHoisting: true, module: ts.ModuleKind.AMD, luaEntry: [entry] }; |
| 11 | +} |
11 | 12 |
|
12 | | -test.each<[string, Record<string, string>]>([ |
13 | | - [ |
14 | | - "Import module -> main", |
15 | | - { |
16 | | - "main.ts": ` |
17 | | - import { value } from "./module"; |
18 | | - if (value !== true) { |
19 | | - throw "Failed to import x"; |
20 | | - } |
21 | | - `, |
22 | | - "module.ts": exportValueSource, |
23 | | - }, |
24 | | - ], |
25 | | - [ |
26 | | - "Import chain export -> reexport -> main", |
27 | | - { |
28 | | - "main.ts": ` |
29 | | - import { value } from "./reexport"; |
30 | | - if (value !== true) { |
31 | | - throw "Failed to import value"; |
32 | | - } |
33 | | - `, |
34 | | - "reexport.ts": reexportValueSource, |
35 | | - "export.ts": exportValueSource, |
36 | | - }, |
37 | | - ], |
38 | | - [ |
39 | | - "Import chain with a different order", |
40 | | - { |
41 | | - "main.ts": ` |
42 | | - import { value } from "./reexport"; |
43 | | - if (value !== true) { |
44 | | - throw "Failed to import value"; |
45 | | - } |
46 | | - `, |
47 | | - "export.ts": exportValueSource, |
48 | | - "reexport.ts": reexportValueSource, |
49 | | - }, |
50 | | - ], |
51 | | - [ |
52 | | - "Import diamond export -> reexport1 & reexport2 -> main", |
53 | | - { |
54 | | - "main.ts": ` |
55 | | - import { value as a } from "./reexport1"; |
56 | | - import { value as b } from "./reexport2"; |
57 | | - if (a !== true || b !== true) { |
58 | | - throw "Failed to import a or b"; |
59 | | - } |
60 | | - `, |
61 | | - "export.ts": exportValueSource, |
62 | | - "reexport1.ts": reexportValueSource, |
63 | | - "reexport2.ts": reexportValueSource, |
64 | | - }, |
65 | | - ], |
66 | | - [ |
67 | | - "Import diamond different order", |
68 | | - { |
69 | | - "reexport1.ts": reexportValueSource, |
70 | | - "reexport2.ts": reexportValueSource, |
71 | | - "export.ts": exportValueSource, |
72 | | - "main.ts": ` |
73 | | - import { value as a } from "./reexport1"; |
74 | | - import { value as b } from "./reexport2"; |
75 | | - if (a !== true || b !== true) { |
76 | | - throw "Failed to import a or b"; |
77 | | - } |
78 | | - `, |
79 | | - }, |
80 | | - ], |
81 | | - [ |
82 | | - "Modules in directories", |
83 | | - { |
84 | | - "main.ts": ` |
85 | | - import { value } from "./module/module"; |
86 | | - if (value !== true) { |
87 | | - throw "Failed to import value"; |
88 | | - } |
89 | | - `, |
90 | | - "module/module.ts": ` |
91 | | - export const value = true; |
92 | | - `, |
93 | | - }, |
94 | | - ], |
95 | | - [ |
96 | | - "Modules aren't ordered by name", |
97 | | - { |
98 | | - "main.ts": ` |
99 | | - import { value } from "./a"; |
100 | | - if (value !== true) { |
101 | | - throw "Failed to import value"; |
102 | | - } |
103 | | - `, |
104 | | - "a.ts": ` |
105 | | - export const value = true; |
106 | | - `, |
107 | | - }, |
108 | | - ], |
109 | | - [ |
110 | | - "Modules in directories", |
111 | | - { |
112 | | - "main/main.ts": ` |
113 | | - import { value } from "../module"; |
114 | | - if (value !== true) { |
115 | | - throw "Failed to import value"; |
116 | | - } |
117 | | - `, |
118 | | - "module.ts": ` |
119 | | - export const value = true; |
120 | | - `, |
121 | | - }, |
122 | | - ], |
123 | | - [ |
124 | | - "LuaLibs are usable", |
125 | | - { |
126 | | - "module.ts": ` |
127 | | - export const array = [1, 2]; |
128 | | - array.push(3); |
129 | | - `, |
130 | | - "main.ts": ` |
131 | | - import { array } from "./module"; |
132 | | - if (array[2] !== 3) { |
133 | | - throw "Array's third item is not three"; |
134 | | - } |
135 | | - `, |
136 | | - }, |
137 | | - ], |
138 | | -])("outFile tests (%s)", (_, files) => { |
139 | | - const testBuilder = util.testBundle` |
140 | | - ${files["main.ts"]} |
141 | | - `.setOptions({ outFile: "main.lua", module: ts.ModuleKind.AMD, luaEntry: ["main.ts"] }); |
| 13 | +describe("outFile", () => { |
| 14 | + test("import module -> main", () => { |
| 15 | + util.testBundle` |
| 16 | + import { value } from "./module"; |
| 17 | + ${validateValueSource} |
| 18 | + ` |
| 19 | + .addExtraFile("module.ts", exportValueSource) |
| 20 | + .setOptions(outFileOptionsWithEntry("main.ts")) |
| 21 | + .expectNoExecutionError(); |
| 22 | + }); |
142 | 23 |
|
143 | | - const extraFiles = Object.keys(files) |
144 | | - .map(file => ({ fileName: file, code: files[file] })) |
145 | | - .filter(file => file.fileName !== "main.ts"); |
| 24 | + test("import chain export -> reexport -> main", () => { |
| 25 | + util.testBundle` |
| 26 | + import { value } from "./reexport"; |
| 27 | + ${validateValueSource} |
| 28 | + ` |
| 29 | + .addExtraFile("reexport.ts", reexportValueSource) |
| 30 | + .addExtraFile("export.ts", exportValueSource) |
| 31 | + .setOptions(outFileOptionsWithEntry("main.ts")) |
| 32 | + .expectNoExecutionError(); |
| 33 | + }); |
146 | 34 |
|
147 | | - extraFiles.forEach(extraFile => { |
148 | | - testBuilder.addExtraFile(extraFile.fileName, extraFile.code); |
| 35 | + test("diamond imports/exports -> reexport1 & reexport2 -> main", () => { |
| 36 | + util.testBundle` |
| 37 | + import { value as a } from "./reexport1"; |
| 38 | + import { value as b } from "./reexport2"; |
| 39 | + if (a !== true || b !== true) { |
| 40 | + throw "Failed to import a or b"; |
| 41 | + } |
| 42 | + ` |
| 43 | + .addExtraFile("reexport1.ts", reexportValueSource) |
| 44 | + .addExtraFile("reexport2.ts", reexportValueSource) |
| 45 | + .addExtraFile("export.ts", exportValueSource) |
| 46 | + .setOptions(outFileOptionsWithEntry("main.ts")) |
| 47 | + .expectNoExecutionError(); |
149 | 48 | }); |
150 | 49 |
|
151 | | - testBuilder.expectNoExecutionError(); |
152 | | -}); |
| 50 | + test("module in directory", () => { |
| 51 | + util.testBundle` |
| 52 | + import { value } from "./module/module"; |
| 53 | + ${validateValueSource} |
| 54 | + ` |
| 55 | + .addExtraFile("module/module.ts", exportValueSource) |
| 56 | + .setOptions(outFileOptionsWithEntry("main.ts")) |
| 57 | + .expectNoExecutionError(); |
| 58 | + }); |
| 59 | + |
| 60 | + test("modules aren't ordered by name", () => { |
| 61 | + util.testBundle` |
| 62 | + import { value } from "./a"; |
| 63 | + ${validateValueSource} |
| 64 | + ` |
| 65 | + .addExtraFile("a.ts", exportValueSource) |
| 66 | + .setOptions(outFileOptionsWithEntry("main.ts")) |
| 67 | + .expectNoExecutionError(); |
| 68 | + }); |
153 | 69 |
|
154 | | -test("outFile cyclic imports", () => { |
155 | | - util.testBundle` |
156 | | - export const a = true; |
157 | | - import { b } from "./b"; |
158 | | - if (b !== true) { |
159 | | - throw "Did not receive true from module b"; |
160 | | - } |
161 | | - ` |
162 | | - .addExtraFile( |
163 | | - "b.ts", |
164 | | - ` |
165 | | - import { a } from "./main"; |
166 | | - export const b = a; |
167 | | - ` |
168 | | - ) |
169 | | - .setOptions({ outFile: "main.lua", noHoisting: true, module: ts.ModuleKind.AMD, luaEntry: ["main.ts"] }) |
170 | | - .expectNoExecutionError(); |
| 70 | + test("entry point in directory", () => { |
| 71 | + util.testBundle`` |
| 72 | + .addExtraFile( |
| 73 | + "main/main.ts", |
| 74 | + ` |
| 75 | + import { value } from "../module"; |
| 76 | + ${validateValueSource} |
| 77 | + ` |
| 78 | + ) |
| 79 | + .addExtraFile("module.ts", exportValueSource) |
| 80 | + .setOptions(outFileOptionsWithEntry("main/main.ts")) |
| 81 | + .expectNoExecutionError(); |
| 82 | + }); |
| 83 | + |
| 84 | + test("LuaLibs", () => { |
| 85 | + util.testBundle` |
| 86 | + import { array } from "./module"; |
| 87 | + if (array[2] !== 3) { |
| 88 | + throw "Array's third item is not three"; |
| 89 | + } |
| 90 | + ` |
| 91 | + .addExtraFile( |
| 92 | + "module.ts", |
| 93 | + ` |
| 94 | + export const array = [1, 2]; |
| 95 | + array.push(3); |
| 96 | + ` |
| 97 | + ) |
| 98 | + .setOptions(outFileOptionsWithEntry("main.ts")) |
| 99 | + .expectNoExecutionError(); |
| 100 | + }); |
| 101 | + |
| 102 | + test("cyclic imports", () => { |
| 103 | + util.testBundle` |
| 104 | + export const a = true; |
| 105 | + import { value } from "./b"; |
| 106 | + ${validateValueSource} |
| 107 | + ` |
| 108 | + .addExtraFile( |
| 109 | + "b.ts", |
| 110 | + ` |
| 111 | + import { a } from "./main"; |
| 112 | + export const value = a; |
| 113 | + ` |
| 114 | + ) |
| 115 | + .setOptions(outFileOptionsWithEntry("main.ts")) |
| 116 | + .expectNoExecutionError(); |
| 117 | + }); |
171 | 118 | }); |
0 commit comments