-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtest.zig
318 lines (233 loc) · 10.2 KB
/
test.zig
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
const std = @import("std");
const lib = @import("lib.zig");
const testing = std.testing;
const allocator = testing.allocator;
const App = lib.App;
const Arg = lib.Arg;
test "positional arguments with auto index" {
var app = App.init(allocator, "app", null);
errdefer app.deinit();
try app.rootCommand().addArg(Arg.positional("ONE", null, null));
try app.rootCommand().addArg(Arg.positional("TWO", null, null));
try app.rootCommand().addArg(Arg.positional("THREE", null, null));
const matches = try app.parseFrom(&.{ "val1", "val2", "val3" });
try testing.expectEqualStrings("val1", matches.getSingleValue("ONE").?);
try testing.expectEqualStrings("val2", matches.getSingleValue("TWO").?);
try testing.expectEqualStrings("val3", matches.getSingleValue("THREE").?);
app.deinit();
}
test "positional arguments with manual index" {
var app = App.init(allocator, "app", null);
errdefer app.deinit();
try app.rootCommand().addArg(Arg.positional("ONE", null, 1));
try app.rootCommand().addArg(Arg.positional("THREE", null, 3));
try app.rootCommand().addArg(Arg.positional("TWO", null, 2));
const matches = try app.parseFrom(&.{ "val1", "val2", "val3" });
try testing.expectEqualStrings("val1", matches.getSingleValue("ONE").?);
try testing.expectEqualStrings("val2", matches.getSingleValue("TWO").?);
try testing.expectEqualStrings("val3", matches.getSingleValue("THREE").?);
app.deinit();
}
test "positional arguments with same index" {
var app = App.init(allocator, "app", null);
errdefer app.deinit();
try app.rootCommand().addArg(Arg.positional("ONE", null, 1));
try testing.expectError(
error.DuplicatePositionalArgIndex,
app.rootCommand().addArg(Arg.positional("TWO", null, 1)),
);
app.deinit();
}
test "command that takes single value" {
var app = App.init(allocator, "rm", null);
errdefer app.deinit();
try app.rootCommand().addArg(Arg.positional("PATH", null, 1));
const matches = try app.parseFrom(&.{"test.txt"});
try testing.expectEqualStrings("test.txt", matches.getSingleValue("PATH").?);
app.deinit();
}
// Positional argument can no longer take multiple values
//
// test "command that takes many values" {
// var app = App.init(allocator, "rm", null);
// errdefer app.deinit();
// var paths = Arg.init("PATHS", null);
// paths.setProperty(.takes_multiple_values);
// paths.setProperty(.takes_value);
// try app.rootCommand().addArg(paths);
// app.rootCommand().setProperty(.takes_positional_arg);
// const matches = try app.parseFrom(&.{ "a", "b", "c" });
// try testing.expectEqualSlices([]const u8, &.{ "a", "b", "c" }, args.getMultiValues("PATHS").?);
// app.deinit();
// }
// Positional argument can no longer take multiple values
//
// test "command that takes many values using delimiter" {
// var app = App.init(allocator, "rm", null);
// errdefer app.deinit();
// var paths = Arg.init("PATHS", null);
// paths.setProperty(.takes_multiple_values);
// paths.setValuesDelimiter(":");
// try app.rootCommand().addArg(paths);
// app.rootCommand().setProperty(.takes_positional_arg);
// const matches = try app.parseFrom(&.{"a:b:c"});
// // This gives weird error like:
// // index 0 incorrect. expected { 97 }, found { 97 }
// //try testing.expectEqualSlices([]const u8, &.{ "a", "b", "c" }, args.getMultiValues("PATHS").?);
// const given_paths = args.getMultiValues("PATHS");
// try testing.expectEqual(true, given_paths != null);
// try testing.expectEqual(@as(usize, 3), given_paths.?.len);
// try testing.expectEqualStrings("a", given_paths.?[0]);
// try testing.expectEqualStrings("b", given_paths.?[1]);
// try testing.expectEqualStrings("c", given_paths.?[2]);
// app.deinit();
// }
test "command that takes required positional arg" {
var app = App.init(allocator, "rm", null);
errdefer app.deinit();
try app.rootCommand().addArg(Arg.positional("PATH", null, null));
app.rootCommand().setProperty(.positional_arg_required);
try testing.expectError(error.PositionalArgumentNotProvided, app.parseFrom(&.{}));
app.deinit();
}
test "command requires subcommand" {
var app = App.init(allocator, "git", null);
errdefer app.deinit();
try app.rootCommand().addSubcommand(app.createCommand("init", null));
app.rootCommand().setProperty(.subcommand_required);
try testing.expectError(error.SubcommandNotProvided, app.parseFrom(&.{}));
app.deinit();
}
test "Option that does not takes value" {
var app = App.init(allocator, "clang", null);
errdefer app.deinit();
try app.rootCommand().addArg(Arg.booleanOption("version", 'v', null));
try testing.expectError(error.UnexpectedOptionValue, app.parseFrom(&.{"-v=13"}));
app.deinit();
}
test "Option that takes single value" {
var app = App.init(allocator, "clang", null);
errdefer app.deinit();
try app.rootCommand().addArg(Arg.singleValueOption("output", 'o', null));
try testing.expectError(error.OptionValueNotProvided, app.parseFrom(&.{"-o"}));
app.deinit();
}
test "multiValuesOption provided with single value short" {
var app = App.init(allocator, "clang", null);
defer app.deinit();
const srcs = Arg.multiValuesOption("sources", 's', null, 128);
// ex: clang sources...
try app.rootCommand().addArg(srcs);
const matches_short = try app.parseFrom(&.{ "-s", "f1" });
try testing.expectEqual(@as(usize, 1), matches_short.getMultiValues("sources").?.len);
}
test "multiValuesOption provided with single value long" {
var app = App.init(allocator, "clang", null);
defer app.deinit();
const srcs = Arg.multiValuesOption("sources", 's', null, 128);
// ex: clang sources...
try app.rootCommand().addArg(srcs);
const matches_short = try app.parseFrom(&.{ "--sources", "f1" });
try testing.expectEqual(@as(usize, 1), matches_short.getMultiValues("sources").?.len);
}
test "multiValuesOption provided with multiple values short" {
var app = App.init(allocator, "clang", null);
defer app.deinit();
const srcs = Arg.multiValuesOption("sources", 's', null, 128);
// ex: clang sources...
try app.rootCommand().addArg(srcs);
const matches = try app.parseFrom(&.{ "-s", "f1", "f2", "f3", "f4", "f5" });
try testing.expectEqual(@as(usize, 5), matches.getMultiValues("sources").?.len);
}
test "multiValuesOption provided with multiple values long" {
var app = App.init(allocator, "clang", null);
defer app.deinit();
const srcs = Arg.multiValuesOption("sources", 's', null, 128);
// ex: clang sources...
try app.rootCommand().addArg(srcs);
const matches = try app.parseFrom(&.{ "--sources", "f1", "f2", "f3", "f4", "f5" });
try testing.expectEqual(@as(usize, 5), matches.getMultiValues("sources").?.len);
}
test "Option that takes many/multiple values" {
var app = App.init(allocator, "clang", null);
errdefer app.deinit();
var srcs = Arg.init("sources", null);
srcs.setShortName('s');
srcs.setValuesDelimiter(":");
srcs.setProperty(.takes_value);
srcs.setProperty(.takes_multiple_values);
// ex: clang sources...
try app.rootCommand().addArg(srcs);
const matches = try app.parseFrom(&.{ "-s", "f1", "f2", "f3", "f4", "f5" });
try testing.expectEqual(@as(usize, 5), matches.getMultiValues("sources").?.len);
app.deinit();
}
test "Option with min values" {
var app = App.init(allocator, "clang", null);
errdefer app.deinit();
var srcs = Arg.init("sources", null);
srcs.setShortName('s');
srcs.setMinValues(2);
srcs.setValuesDelimiter(":");
srcs.setProperty(.takes_value);
try app.rootCommand().addArg(srcs);
try testing.expectError(error.TooFewOptionValue, app.parseFrom(&.{"-s=f1"}));
app.deinit();
}
test "Option with max values" {
var app = App.init(allocator, "clang", null);
errdefer app.deinit();
var srcs = Arg.multiValuesOption("sources", 's', null, 5);
srcs.setValuesDelimiter(":");
try app.rootCommand().addArg(srcs);
try testing.expectError(error.TooManyOptionValue, app.parseFrom(
&.{"-s=f1:f2:f3:f4:f5:f6"},
));
app.deinit();
}
test "Option with allowed values" {
var app = App.init(allocator, "clang", null);
errdefer app.deinit();
const stdd = Arg.singleValueOptionWithValidValues(
"std",
null,
null,
&[_][]const u8{ "c99", "c11", "c17" },
);
try app.rootCommand().addArg(stdd);
try testing.expectError(error.InvalidOptionValue, app.parseFrom(&.{"--std=c100"}));
app.deinit();
}
test "passing positional argument before options" {
var app = App.init(allocator, "ls", null);
errdefer app.deinit();
try app.rootCommand().addArg(Arg.positional("PATH", null, null));
try app.rootCommand().addArg(Arg.booleanOption("all", 'a', null));
app.rootCommand().setProperty(.positional_arg_required);
const matches = try app.parseFrom(&.{ ".", "-a" });
try testing.expectEqualStrings(".", matches.getSingleValue("PATH").?);
try testing.expectEqual(true, matches.containsArg("all"));
app.deinit();
}
test "passing positional argument after options" {
var app = App.init(allocator, "ls", null);
errdefer app.deinit();
try app.rootCommand().addArg(Arg.positional("PATH", null, null));
try app.rootCommand().addArg(Arg.booleanOption("all", 'a', null));
const matches = try app.parseFrom(&.{ "-a", "." });
try testing.expectEqualStrings(".", matches.getSingleValue("PATH").?);
try testing.expectEqual(true, matches.containsArg("all"));
app.deinit();
}
test "passing positional argument before and after options" {
var app = App.init(allocator, "ls", null);
errdefer app.deinit();
try app.rootCommand().addArg(Arg.positional("PATH", null, null));
try app.rootCommand().addArg(Arg.booleanOption("all", 'a', null));
try app.rootCommand().addArg(Arg.booleanOption("one-line", '1', null));
const matches = try app.parseFrom(&.{ "-1", ".", "-a" });
try testing.expectEqualStrings(".", matches.getSingleValue("PATH").?);
try testing.expectEqual(true, matches.containsArg("one-line"));
try testing.expectEqual(true, matches.containsArg("all"));
app.deinit();
}