-
Notifications
You must be signed in to change notification settings - Fork 5.2k
/
parse_test.ts
executable file
·200 lines (175 loc) · 4.54 KB
/
parse_test.ts
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
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { assertEquals } from "../testing/asserts.ts";
import { parse } from "./mod.ts";
Deno.test(function parseArgs(): void {
assertEquals(parse(["--no-moo"]), { moo: false, _: [] });
assertEquals(parse(["-v", "a", "-v", "b", "-v", "c"]), {
v: ["a", "b", "c"],
_: []
});
});
Deno.test(function comprehensive(): void {
assertEquals(
parse([
"--name=meowmers",
"bare",
"-cats",
"woo",
"-h",
"awesome",
"--multi=quux",
"--key",
"value",
"-b",
"--bool",
"--no-meep",
"--multi=baz",
"--",
"--not-a-flag",
"eek"
]),
{
c: true,
a: true,
t: true,
s: "woo",
h: "awesome",
b: true,
bool: true,
key: "value",
multi: ["quux", "baz"],
meep: false,
name: "meowmers",
_: ["bare", "--not-a-flag", "eek"]
}
);
});
Deno.test(function flagBoolean(): void {
const argv = parse(["-t", "moo"], { boolean: "t" });
assertEquals(argv, { t: true, _: ["moo"] });
assertEquals(typeof argv.t, "boolean");
});
Deno.test(function flagBooleanValue(): void {
const argv = parse(["--verbose", "false", "moo", "-t", "true"], {
boolean: ["t", "verbose"],
default: { verbose: true }
});
assertEquals(argv, {
verbose: false,
t: true,
_: ["moo"]
});
assertEquals(typeof argv.verbose, "boolean");
assertEquals(typeof argv.t, "boolean");
});
Deno.test(function newlinesInParams(): void {
const args = parse(["-s", "X\nX"]);
assertEquals(args, { _: [], s: "X\nX" });
// reproduce in bash:
// VALUE="new
// line"
// deno program.js --s="$VALUE"
const args2 = parse(["--s=X\nX"]);
assertEquals(args2, { _: [], s: "X\nX" });
});
Deno.test(function strings(): void {
const s = parse(["-s", "0001234"], { string: "s" }).s;
assertEquals(s, "0001234");
assertEquals(typeof s, "string");
const x = parse(["-x", "56"], { string: "x" }).x;
assertEquals(x, "56");
assertEquals(typeof x, "string");
});
Deno.test(function stringArgs(): void {
const s = parse([" ", " "], { string: "_" })._;
assertEquals(s.length, 2);
assertEquals(typeof s[0], "string");
assertEquals(s[0], " ");
assertEquals(typeof s[1], "string");
assertEquals(s[1], " ");
});
Deno.test(function emptyStrings(): void {
const s = parse(["-s"], { string: "s" }).s;
assertEquals(s, "");
assertEquals(typeof s, "string");
const str = parse(["--str"], { string: "str" }).str;
assertEquals(str, "");
assertEquals(typeof str, "string");
const letters = parse(["-art"], {
string: ["a", "t"]
});
assertEquals(letters.a, "");
assertEquals(letters.r, true);
assertEquals(letters.t, "");
});
Deno.test(function stringAndAlias(): void {
const x = parse(["--str", "000123"], {
string: "s",
alias: { s: "str" }
});
assertEquals(x.str, "000123");
assertEquals(typeof x.str, "string");
assertEquals(x.s, "000123");
assertEquals(typeof x.s, "string");
const y = parse(["-s", "000123"], {
string: "str",
alias: { str: "s" }
});
assertEquals(y.str, "000123");
assertEquals(typeof y.str, "string");
assertEquals(y.s, "000123");
assertEquals(typeof y.s, "string");
});
Deno.test(function slashBreak(): void {
assertEquals(parse(["-I/foo/bar/baz"]), { I: "/foo/bar/baz", _: [] });
assertEquals(parse(["-xyz/foo/bar/baz"]), {
x: true,
y: true,
z: "/foo/bar/baz",
_: []
});
});
Deno.test(function alias(): void {
const argv = parse(["-f", "11", "--zoom", "55"], {
alias: { z: "zoom" }
});
assertEquals(argv.zoom, 55);
assertEquals(argv.z, argv.zoom);
assertEquals(argv.f, 11);
});
Deno.test(function multiAlias(): void {
const argv = parse(["-f", "11", "--zoom", "55"], {
alias: { z: ["zm", "zoom"] }
});
assertEquals(argv.zoom, 55);
assertEquals(argv.z, argv.zoom);
assertEquals(argv.z, argv.zm);
assertEquals(argv.f, 11);
});
Deno.test(function nestedDottedObjects(): void {
const argv = parse([
"--foo.bar",
"3",
"--foo.baz",
"4",
"--foo.quux.quibble",
"5",
"--foo.quux.oO",
"--beep.boop"
]);
assertEquals(argv.foo, {
bar: 3,
baz: 4,
quux: {
quibble: 5,
oO: true
}
});
assertEquals(argv.beep, { boop: true });
});
Deno.test(function flagBuiltinProperty(): void {
const argv = parse(["--toString", "--valueOf", "foo"]);
assertEquals(argv, { toString: true, valueOf: "foo", _: [] });
assertEquals(typeof argv.toString, "boolean");
assertEquals(typeof argv.valueOf, "string");
});