forked from TypeScriptToLua/TypeScriptToLua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommandLineParser.spec.ts
More file actions
204 lines (158 loc) · 7.94 KB
/
commandLineParser.spec.ts
File metadata and controls
204 lines (158 loc) · 7.94 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
import * as ts from "typescript";
import * as tstl from "../../src";
describe("command line", () => {
test("should support aliases", () => {
const full = tstl.parseCommandLine(["--luaTarget", "5.1"]);
const alias = tstl.parseCommandLine(["-lt", "5.1"]);
expect(full).toEqual(alias);
});
test("should support standard typescript options", () => {
const commandLine = "--project tsconfig.json --noHeader -t es3 -lt 5.3";
const result = tstl.parseCommandLine(commandLine.split(" "));
expect(result.errors).not.toHaveDiagnostics();
expect(result.options).toEqual({
project: "tsconfig.json",
noHeader: true,
target: ts.ScriptTarget.ES3,
luaTarget: tstl.LuaTarget.Lua53,
});
});
test("should error on unknown options", () => {
const result = tstl.parseCommandLine(["--unknownOption"]);
expect(result.errors).toHaveDiagnostics();
});
test("should parse options case-insensitively", () => {
const result = tstl.parseCommandLine(["--NoHeader"]);
expect(result.errors).not.toHaveDiagnostics();
expect(result.options.noHeader).toBe(true);
});
describe("enum options", () => {
test("should parse enums", () => {
const result = tstl.parseCommandLine(["--luaTarget", "5.1"]);
expect(result.errors).not.toHaveDiagnostics();
expect(result.options.luaTarget).toBe(tstl.LuaTarget.Lua51);
});
test("should be case-insensitive", () => {
for (const value of ["jit", "JiT", "JIT"]) {
const result = tstl.parseCommandLine(["--luaTarget", value]);
expect(result.errors).not.toHaveDiagnostics();
expect(result.options.luaTarget).toBe(tstl.LuaTarget.LuaJIT);
}
});
test("should error on invalid value", () => {
const result = tstl.parseCommandLine(["--luaTarget", "invalid"]);
expect(result.errors).toHaveDiagnostics();
});
});
describe("boolean options", () => {
test.each([true, false])("should parse booleans (%p)", value => {
const result = tstl.parseCommandLine(["--noHeader", value.toString()]);
expect(result.errors).not.toHaveDiagnostics();
expect(result.options.noHeader).toBe(value);
});
test("should be case-sensitive", () => {
const result = tstl.parseCommandLine(["--noHeader", "FALSE"]);
expect(result.errors).not.toHaveDiagnostics();
expect(result.options.noHeader).toBe(true);
expect(result.fileNames).toEqual(["FALSE"]);
});
test("should be parsed without a value", () => {
const result = tstl.parseCommandLine(["--noHeader"]);
expect(result.errors).not.toHaveDiagnostics();
expect(result.options.noHeader).toBe(true);
});
test("shouldn't parse following arguments as values", () => {
const result = tstl.parseCommandLine(["--noHeader", "--noHoisting"]);
expect(result.errors).not.toHaveDiagnostics();
expect(result.options.noHeader).toBe(true);
expect(result.options.noHoisting).toBe(true);
});
test("shouldn't parse following files as values", () => {
const result = tstl.parseCommandLine(["--noHeader", "file.ts"]);
expect(result.errors).not.toHaveDiagnostics();
expect(result.options.noHeader).toBe(true);
});
});
describe("integration", () => {
test.each<[string, string, tstl.CompilerOptions]>([
["noHeader", "false", { noHeader: false }],
["noHeader", "true", { noHeader: true }],
["noHoisting", "false", { noHoisting: false }],
["noHoisting", "true", { noHoisting: true }],
["sourceMapTraceback", "false", { sourceMapTraceback: false }],
["sourceMapTraceback", "true", { sourceMapTraceback: true }],
["luaLibImport", "none", { luaLibImport: tstl.LuaLibImportKind.None }],
["luaLibImport", "always", { luaLibImport: tstl.LuaLibImportKind.Always }],
["luaLibImport", "inline", { luaLibImport: tstl.LuaLibImportKind.Inline }],
["luaLibImport", "require", { luaLibImport: tstl.LuaLibImportKind.Require }],
["luaTarget", "5.1", { luaTarget: tstl.LuaTarget.Lua51 }],
["luaTarget", "5.2", { luaTarget: tstl.LuaTarget.Lua52 }],
["luaTarget", "5.3", { luaTarget: tstl.LuaTarget.Lua53 }],
["luaTarget", "jit", { luaTarget: tstl.LuaTarget.LuaJIT }],
])("--%s %s", (optionName, value, expected) => {
const result = tstl.parseCommandLine([`--${optionName}`, value]);
expect(result.errors).not.toHaveDiagnostics();
expect(result.options).toEqual(expected);
});
});
});
describe("tsconfig", () => {
const parseConfigFileContent = (config: any) => {
// Specifying `files` option disables automatic file searching, that includes all files in
// the project, making these tests slow. Empty file list is considered as an error.
config.files = ["src/index.ts"];
return tstl.updateParsedConfigFile(ts.parseJsonConfigFileContent(config, ts.sys, ""));
};
test("should support deprecated root-level options", () => {
const rootLevel = parseConfigFileContent({ noHeader: true });
const namespaced = parseConfigFileContent({ tstl: { noHeader: true } });
expect(rootLevel.errors).toEqual([expect.objectContaining({ category: ts.DiagnosticCategory.Warning })]);
expect(rootLevel.options).toEqual(namespaced.options);
});
test("should allow unknown root-level options", () => {
const result = parseConfigFileContent({ unknownOption: true });
expect(result.errors).not.toHaveDiagnostics();
expect(result.options.unknownOption).toBeUndefined();
});
test("should error on unknown namespaced options", () => {
const result = parseConfigFileContent({ tstl: { unknownOption: true } });
expect(result.errors).toHaveDiagnostics();
expect(result.options.unknownOption).toBeUndefined();
});
test("should parse options case-sensitively", () => {
const result = parseConfigFileContent({ tstl: { NoHeader: true } });
expect(result.errors).toHaveDiagnostics();
expect(result.options.NoHeader).toBeUndefined();
expect(result.options.noHeader).toBeUndefined();
});
describe("enum options", () => {
test("should parse enums", () => {
const result = parseConfigFileContent({ tstl: { luaTarget: "5.1" } });
expect(result.errors).not.toHaveDiagnostics();
expect(result.options.luaTarget).toBe(tstl.LuaTarget.Lua51);
});
test("should be case-insensitive", () => {
for (const value of ["jit", "JiT", "JIT"]) {
const result = parseConfigFileContent({ tstl: { luaTarget: value } });
expect(result.errors).not.toHaveDiagnostics();
expect(result.options.luaTarget).toBe(tstl.LuaTarget.LuaJIT);
}
});
test("should error on invalid value", () => {
const result = parseConfigFileContent({ tstl: { luaTarget: "invalid" } });
expect(result.errors).toHaveDiagnostics();
});
});
describe("boolean options", () => {
test.each([true, false])("should parse booleans (%p)", value => {
const result = parseConfigFileContent({ tstl: { noHeader: value } });
expect(result.errors).not.toHaveDiagnostics();
expect(result.options.noHeader).toBe(value);
});
test("shouldn't parse strings", () => {
const result = parseConfigFileContent({ tstl: { noHeader: "true" } });
expect(result.errors).toHaveDiagnostics();
expect(result.options.noHeader).toBeUndefined();
});
});
});