OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library parse_test; | |
6 | |
7 import 'package:unittest/unittest.dart'; | |
8 import 'package:args/args.dart'; | |
9 import 'utils.dart'; | |
10 | |
11 void main() { | |
12 group('ArgParser.parse()', () { | |
13 test('does not destructively modify the argument list', () { | |
14 var parser = new ArgParser(); | |
15 parser.addFlag('verbose'); | |
16 | |
17 var args = ['--verbose']; | |
18 var results = parser.parse(args); | |
19 expect(args, equals(['--verbose'])); | |
20 expect(results['verbose'], isTrue); | |
21 }); | |
22 | |
23 group('flags', () { | |
24 test('are true if present', () { | |
25 var parser = new ArgParser(); | |
26 parser.addFlag('verbose'); | |
27 | |
28 var args = parser.parse(['--verbose']); | |
29 expect(args['verbose'], isTrue); | |
30 }); | |
31 | |
32 test('default if missing', () { | |
33 var parser = new ArgParser(); | |
34 parser.addFlag('a', defaultsTo: true); | |
35 parser.addFlag('b', defaultsTo: false); | |
36 | |
37 var args = parser.parse([]); | |
38 expect(args['a'], isTrue); | |
39 expect(args['b'], isFalse); | |
40 }); | |
41 | |
42 test('are false if missing with no default', () { | |
43 var parser = new ArgParser(); | |
44 parser.addFlag('verbose'); | |
45 | |
46 var args = parser.parse([]); | |
47 expect(args['verbose'], isFalse); | |
48 }); | |
49 | |
50 test('throws if given a value', () { | |
51 var parser = new ArgParser(); | |
52 parser.addFlag('verbose'); | |
53 | |
54 throwsFormat(parser, ['--verbose=true']); | |
55 }); | |
56 | |
57 test('are case-sensitive', () { | |
58 var parser = new ArgParser(); | |
59 parser.addFlag('verbose'); | |
60 parser.addFlag('Verbose'); | |
61 var results = parser.parse(['--verbose']); | |
62 expect(results['verbose'], isTrue); | |
63 expect(results['Verbose'], isFalse); | |
64 }); | |
65 }); | |
66 | |
67 group('flags negated with "no-"', () { | |
68 test('set the flag to false', () { | |
69 var parser = new ArgParser(); | |
70 parser.addFlag('verbose'); | |
71 | |
72 var args = parser.parse(['--no-verbose']); | |
73 expect(args['verbose'], isFalse); | |
74 }); | |
75 | |
76 test('set the flag to true if the flag actually starts with "no-"', () { | |
77 var parser = new ArgParser(); | |
78 parser.addFlag('no-body'); | |
79 | |
80 var args = parser.parse(['--no-body']); | |
81 expect(args['no-body'], isTrue); | |
82 }); | |
83 | |
84 test('are not preferred over a colliding one without', () { | |
85 var parser = new ArgParser(); | |
86 parser.addFlag('no-strum'); | |
87 parser.addFlag('strum'); | |
88 | |
89 var args = parser.parse(['--no-strum']); | |
90 expect(args['no-strum'], isTrue); | |
91 expect(args['strum'], isFalse); | |
92 }); | |
93 | |
94 test('fail for non-negatable flags', () { | |
95 var parser = new ArgParser(); | |
96 parser.addFlag('strum', negatable: false); | |
97 | |
98 throwsFormat(parser, ['--no-strum']); | |
99 }); | |
100 }); | |
101 | |
102 group('callbacks', () { | |
103 test('for present flags are invoked with the value', () { | |
104 var a; | |
105 var parser = new ArgParser(); | |
106 parser.addFlag('a', callback: (value) => a = value); | |
107 | |
108 var args = parser.parse(['--a']); | |
109 expect(a, isTrue); | |
110 }); | |
111 | |
112 test('for absent flags are invoked with the default value', () { | |
113 var a; | |
114 var parser = new ArgParser(); | |
115 parser.addFlag('a', defaultsTo: false, | |
116 callback: (value) => a = value); | |
117 | |
118 var args = parser.parse([]); | |
119 expect(a, isFalse); | |
120 }); | |
121 | |
122 test('are invoked even if the flag is not present', () { | |
123 var a = 'not called'; | |
124 var parser = new ArgParser(); | |
125 parser.addFlag('a', callback: (value) => a = value); | |
126 | |
127 var args = parser.parse([]); | |
128 expect(a, isFalse); | |
129 }); | |
130 | |
131 test('for present options are invoked with the value', () { | |
132 var a; | |
133 var parser = new ArgParser(); | |
134 parser.addOption('a', callback: (value) => a = value); | |
135 | |
136 var args = parser.parse(['--a=v']); | |
137 expect(a, equals('v')); | |
138 }); | |
139 | |
140 test('for absent options are invoked with the default value', () { | |
141 var a; | |
142 var parser = new ArgParser(); | |
143 parser.addOption('a', defaultsTo: 'v', | |
144 callback: (value) => a = value); | |
145 | |
146 var args = parser.parse([]); | |
147 expect(a, equals('v')); | |
148 }); | |
149 | |
150 test('are invoked even if the option is not present', () { | |
151 var a = 'not called'; | |
152 var parser = new ArgParser(); | |
153 parser.addOption('a', callback: (value) => a = value); | |
154 | |
155 var args = parser.parse([]); | |
156 expect(a, isNull); | |
157 }); | |
158 | |
159 test('for multiple present, allowMultiple, options are invoked with ' | |
160 'value as a list', () { | |
161 var a; | |
162 var parser = new ArgParser(); | |
163 parser.addOption('a', allowMultiple: true, | |
164 callback: (value) => a = value); | |
165 | |
166 var args = parser.parse(['--a=v', '--a=x']); | |
167 expect(a, equals(['v', 'x'])); | |
168 }); | |
169 | |
170 test('for single present, allowMultiple, options are invoked with ' | |
171 ' value as a single element list', () { | |
172 var a; | |
173 var parser = new ArgParser(); | |
174 parser.addOption('a', allowMultiple: true, | |
175 callback: (value) => a = value); | |
176 | |
177 var args = parser.parse(['--a=v']); | |
178 expect(a, equals(['v'])); | |
179 }); | |
180 | |
181 test('for absent, allowMultiple, options are invoked with default ' | |
182 'value as a list.', () { | |
183 var a; | |
184 var parser = new ArgParser(); | |
185 parser.addOption('a', allowMultiple: true, defaultsTo: 'v', | |
186 callback: (value) => a = value); | |
187 | |
188 var args = parser.parse([]); | |
189 expect(a, equals(['v'])); | |
190 }); | |
191 | |
192 test('for absent, allowMultiple, options are invoked with value ' | |
193 'as an empty list.', () { | |
194 var a; | |
195 var parser = new ArgParser(); | |
196 parser.addOption('a', allowMultiple: true, | |
197 callback: (value) => a = value); | |
198 | |
199 var args = parser.parse([]); | |
200 expect(a, isEmpty); | |
201 }); | |
202 }); | |
203 | |
204 group('abbreviations', () { | |
205 test('are parsed with a preceding "-"', () { | |
206 var parser = new ArgParser(); | |
207 parser.addFlag('arg', abbr: 'a'); | |
208 | |
209 var args = parser.parse(['-a']); | |
210 expect(args['arg'], isTrue); | |
211 }); | |
212 | |
213 test('can use multiple after a single "-"', () { | |
214 var parser = new ArgParser(); | |
215 parser.addFlag('first', abbr: 'f'); | |
216 parser.addFlag('second', abbr: 's'); | |
217 parser.addFlag('third', abbr: 't'); | |
218 | |
219 var args = parser.parse(['-tf']); | |
220 expect(args['first'], isTrue); | |
221 expect(args['second'], isFalse); | |
222 expect(args['third'], isTrue); | |
223 }); | |
224 | |
225 test('can have multiple "-" args', () { | |
226 var parser = new ArgParser(); | |
227 parser.addFlag('first', abbr: 'f'); | |
228 parser.addFlag('second', abbr: 's'); | |
229 parser.addFlag('third', abbr: 't'); | |
230 | |
231 var args = parser.parse(['-s', '-tf']); | |
232 expect(args['first'], isTrue); | |
233 expect(args['second'], isTrue); | |
234 expect(args['third'], isTrue); | |
235 }); | |
236 | |
237 test('can take arguments without a space separating', () { | |
238 var parser = new ArgParser(); | |
239 parser.addOption('file', abbr: 'f'); | |
240 | |
241 var args = parser.parse(['-flip']); | |
242 expect(args['file'], equals('lip')); | |
243 }); | |
244 | |
245 test('can take arguments with a space separating', () { | |
246 var parser = new ArgParser(); | |
247 parser.addOption('file', abbr: 'f'); | |
248 | |
249 var args = parser.parse(['-f', 'name']); | |
250 expect(args['file'], equals('name')); | |
251 }); | |
252 | |
253 test('allow non-option characters in the value', () { | |
254 var parser = new ArgParser(); | |
255 parser.addOption('apple', abbr: 'a'); | |
256 | |
257 var args = parser.parse(['-ab?!c']); | |
258 expect(args['apple'], equals('b?!c')); | |
259 }); | |
260 | |
261 test('throw if unknown', () { | |
262 var parser = new ArgParser(); | |
263 throwsFormat(parser, ['-f']); | |
264 }); | |
265 | |
266 test('throw if the value is missing', () { | |
267 var parser = new ArgParser(); | |
268 parser.addOption('file', abbr: 'f'); | |
269 | |
270 throwsFormat(parser, ['-f']); | |
271 }); | |
272 | |
273 test('throw if the value looks like an option', () { | |
274 var parser = new ArgParser(); | |
275 parser.addOption('file', abbr: 'f'); | |
276 parser.addOption('other'); | |
277 | |
278 throwsFormat(parser, ['-f', '--other']); | |
279 throwsFormat(parser, ['-f', '--unknown']); | |
280 throwsFormat(parser, ['-f', '-abbr']); | |
281 }); | |
282 | |
283 test('throw if the value is not allowed', () { | |
284 var parser = new ArgParser(); | |
285 parser.addOption('mode', abbr: 'm', allowed: ['debug', 'release']); | |
286 | |
287 throwsFormat(parser, ['-mprofile']); | |
288 }); | |
289 | |
290 test('throw if any but the first is not a flag', () { | |
291 var parser = new ArgParser(); | |
292 parser.addFlag('apple', abbr: 'a'); | |
293 parser.addOption('banana', abbr: 'b'); // Takes an argument. | |
294 parser.addFlag('cherry', abbr: 'c'); | |
295 | |
296 throwsFormat(parser, ['-abc']); | |
297 }); | |
298 | |
299 test('throw if it has a value but the option is a flag', () { | |
300 var parser = new ArgParser(); | |
301 parser.addFlag('apple', abbr: 'a'); | |
302 parser.addFlag('banana', abbr: 'b'); | |
303 | |
304 // The '?!' means this can only be understood as '--apple b?!c'. | |
305 throwsFormat(parser, ['-ab?!c']); | |
306 }); | |
307 | |
308 test('are case-sensitive', () { | |
309 var parser = new ArgParser(); | |
310 parser.addFlag('file', abbr: 'f'); | |
311 parser.addFlag('force', abbr: 'F'); | |
312 var results = parser.parse(['-f']); | |
313 expect(results['file'], isTrue); | |
314 expect(results['force'], isFalse); | |
315 }); | |
316 }); | |
317 | |
318 group('options', () { | |
319 test('are parsed if present', () { | |
320 var parser = new ArgParser(); | |
321 parser.addOption('mode'); | |
322 var args = parser.parse(['--mode=release']); | |
323 expect(args['mode'], equals('release')); | |
324 }); | |
325 | |
326 test('are null if not present', () { | |
327 var parser = new ArgParser(); | |
328 parser.addOption('mode'); | |
329 var args = parser.parse([]); | |
330 expect(args['mode'], isNull); | |
331 }); | |
332 | |
333 test('default if missing', () { | |
334 var parser = new ArgParser(); | |
335 parser.addOption('mode', defaultsTo: 'debug'); | |
336 var args = parser.parse([]); | |
337 expect(args['mode'], equals('debug')); | |
338 }); | |
339 | |
340 test('allow the value to be separated by whitespace', () { | |
341 var parser = new ArgParser(); | |
342 parser.addOption('mode'); | |
343 var args = parser.parse(['--mode', 'release']); | |
344 expect(args['mode'], equals('release')); | |
345 }); | |
346 | |
347 test('throw if unknown', () { | |
348 var parser = new ArgParser(); | |
349 throwsFormat(parser, ['--unknown']); | |
350 throwsFormat(parser, ['--nobody']); // Starts with "no". | |
351 }); | |
352 | |
353 test('throw if the arg does not include a value', () { | |
354 var parser = new ArgParser(); | |
355 parser.addOption('mode'); | |
356 throwsFormat(parser, ['--mode']); | |
357 }); | |
358 | |
359 test('throw if the value looks like an option', () { | |
360 var parser = new ArgParser(); | |
361 parser.addOption('mode'); | |
362 parser.addOption('other'); | |
363 | |
364 throwsFormat(parser, ['--mode', '--other']); | |
365 throwsFormat(parser, ['--mode', '--unknown']); | |
366 throwsFormat(parser, ['--mode', '-abbr']); | |
367 }); | |
368 | |
369 test('do not throw if the value is in the allowed set', () { | |
370 var parser = new ArgParser(); | |
371 parser.addOption('mode', allowed: ['debug', 'release']); | |
372 var args = parser.parse(['--mode=debug']); | |
373 expect(args['mode'], equals('debug')); | |
374 }); | |
375 | |
376 test('throw if the value is not in the allowed set', () { | |
377 var parser = new ArgParser(); | |
378 parser.addOption('mode', allowed: ['debug', 'release']); | |
379 throwsFormat(parser, ['--mode=profile']); | |
380 }); | |
381 | |
382 test('returns last provided value', () { | |
383 var parser = new ArgParser(); | |
384 parser.addOption('define'); | |
385 var args = parser.parse(['--define=1', '--define=2']); | |
386 expect(args['define'], equals('2')); | |
387 }); | |
388 | |
389 test('returns a List if multi-valued', () { | |
390 var parser = new ArgParser(); | |
391 parser.addOption('define', allowMultiple: true); | |
392 var args = parser.parse(['--define=1']); | |
393 expect(args['define'], equals(['1'])); | |
394 args = parser.parse(['--define=1', '--define=2']); | |
395 expect(args['define'], equals(['1','2'])); | |
396 }); | |
397 | |
398 test('returns the default value for multi-valued arguments ' | |
399 'if not explicitly set', () { | |
400 var parser = new ArgParser(); | |
401 parser.addOption('define', defaultsTo: '0', allowMultiple: true); | |
402 var args = parser.parse(['']); | |
403 expect(args['define'], equals(['0'])); | |
404 }); | |
405 | |
406 test('are case-sensitive', () { | |
407 var parser = new ArgParser(); | |
408 parser.addOption('verbose', defaultsTo: 'no'); | |
409 parser.addOption('Verbose', defaultsTo: 'no'); | |
410 var results = parser.parse(['--verbose', 'chatty']); | |
411 expect(results['verbose'], equals('chatty')); | |
412 expect(results['Verbose'], equals('no')); | |
413 }); | |
414 }); | |
415 | |
416 group('remaining args', () { | |
417 test('stops parsing args when a non-option-like arg is encountered', () { | |
418 var parser = new ArgParser(); | |
419 parser.addFlag('woof'); | |
420 parser.addOption('meow'); | |
421 parser.addOption('tweet', defaultsTo: 'bird'); | |
422 | |
423 var results = parser.parse(['--woof', '--meow', 'v', 'not', 'option']); | |
424 expect(results['woof'], isTrue); | |
425 expect(results['meow'], equals('v')); | |
426 expect(results['tweet'], equals('bird')); | |
427 expect(results.rest, equals(['not', 'option'])); | |
428 }); | |
429 | |
430 test('consumes "--" and stops', () { | |
431 var parser = new ArgParser(); | |
432 parser.addFlag('woof', defaultsTo: false); | |
433 parser.addOption('meow', defaultsTo: 'kitty'); | |
434 | |
435 var results = parser.parse(['--woof', '--', '--meow']); | |
436 expect(results['woof'], isTrue); | |
437 expect(results['meow'], equals('kitty')); | |
438 expect(results.rest, equals(['--meow'])); | |
439 }); | |
440 | |
441 test('leaves "--" if not the first non-option', () { | |
442 var parser = new ArgParser(); | |
443 parser.addFlag('woof'); | |
444 | |
445 var results = parser.parse(['--woof', 'stop', '--', 'arg']); | |
446 expect(results['woof'], isTrue); | |
447 expect(results.rest, equals(['stop', '--', 'arg'])); | |
448 }); | |
449 }); | |
450 }); | |
451 } | |
OLD | NEW |