Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(579)

Side by Side Diff: packages/args/test/parse_test.dart

Issue 1400473008: Roll Observatory packages and add a roll script (Closed) Base URL: git@github.com:dart-lang/observatory_pub_packages.git@master
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « packages/args/test/command_test.dart ('k') | packages/args/test/trailing_options_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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:test/test.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 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, callback: (value) => a = value);
116
117 parser.parse([]);
118 expect(a, isFalse);
119 });
120
121 test('are invoked even if the flag is not present', () {
122 var a = 'not called';
123 var parser = new ArgParser();
124 parser.addFlag('a', callback: (value) => a = value);
125
126 parser.parse([]);
127 expect(a, isFalse);
128 });
129
130 test('for present options are invoked with the value', () {
131 var a;
132 var parser = new ArgParser();
133 parser.addOption('a', callback: (value) => a = value);
134
135 parser.parse(['--a=v']);
136 expect(a, equals('v'));
137 });
138
139 test('for absent options are invoked with the default value', () {
140 var a;
141 var parser = new ArgParser();
142 parser.addOption('a', defaultsTo: 'v', callback: (value) => a = value);
143
144 parser.parse([]);
145 expect(a, equals('v'));
146 });
147
148 test('are invoked even if the option is not present', () {
149 var a = 'not called';
150 var parser = new ArgParser();
151 parser.addOption('a', callback: (value) => a = value);
152
153 parser.parse([]);
154 expect(a, isNull);
155 });
156
157 test('for multiple present, allowMultiple, options are invoked with '
158 'value as a list', () {
159 var a;
160 var parser = new ArgParser();
161 parser.addOption('a',
162 allowMultiple: true, callback: (value) => a = value);
163
164 parser.parse(['--a=v', '--a=x']);
165 expect(a, equals(['v', 'x']));
166 });
167
168 test('for single present, allowMultiple, options are invoked with '
169 ' value as a single element list', () {
170 var a;
171 var parser = new ArgParser();
172 parser.addOption('a',
173 allowMultiple: true, callback: (value) => a = value);
174
175 parser.parse(['--a=v']);
176 expect(a, equals(['v']));
177 });
178
179 test('for absent, allowMultiple, options are invoked with default '
180 'value as a list.', () {
181 var a;
182 var parser = new ArgParser();
183 parser.addOption('a',
184 allowMultiple: true,
185 defaultsTo: 'v',
186 callback: (value) => a = value);
187
188 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',
197 allowMultiple: true, callback: (value) => a = value);
198
199 parser.parse([]);
200 expect(a, isEmpty);
201 });
202
203 test('allowMultiple parses comma-separated strings', () {
204 var a;
205 var parser = new ArgParser();
206 parser.addOption('a',
207 allowMultiple: true, callback: (value) => a = value);
208
209 parser.parse(['--a=v,w', '--a=x']);
210 expect(a, equals(['v', 'w', 'x']));
211 });
212
213 test("allowMultiple doesn't parses comma-separated strings with "
214 "splitCommas: false", () {
215 var a;
216 var parser = new ArgParser();
217 parser.addOption('a',
218 allowMultiple: true,
219 splitCommas: false,
220 callback: (value) => a = value);
221
222 parser.parse(['--a=v,w', '--a=x']);
223 expect(a, equals(['v,w', 'x']));
224 });
225
226 test('allowMultiple parses empty strings', () {
227 var a;
228 var parser = new ArgParser();
229 parser.addOption('a',
230 allowMultiple: true, callback: (value) => a = value);
231
232 parser.parse(['--a=,v', '--a=w,', '--a=,', '--a=x,,y', '--a', '']);
233 expect(a, equals(['', 'v', 'w', '', '', '', 'x', '', 'y', '']));
234 });
235
236 test('allowMultiple with allowed parses comma-separated strings', () {
237 var a;
238 var parser = new ArgParser();
239 parser.addOption('a',
240 allowMultiple: true,
241 allowed: ['v', 'w', 'x'],
242 callback: (value) => a = value);
243
244 parser.parse(['--a=v,w', '--a=x']);
245 expect(a, equals(['v', 'w', 'x']));
246 });
247 });
248
249 group('abbreviations', () {
250 test('are parsed with a preceding "-"', () {
251 var parser = new ArgParser();
252 parser.addFlag('arg', abbr: 'a');
253
254 var args = parser.parse(['-a']);
255 expect(args['arg'], isTrue);
256 });
257
258 test('can use multiple after a single "-"', () {
259 var parser = new ArgParser();
260 parser.addFlag('first', abbr: 'f');
261 parser.addFlag('second', abbr: 's');
262 parser.addFlag('third', abbr: 't');
263
264 var args = parser.parse(['-tf']);
265 expect(args['first'], isTrue);
266 expect(args['second'], isFalse);
267 expect(args['third'], isTrue);
268 });
269
270 test('can have multiple "-" args', () {
271 var parser = new ArgParser();
272 parser.addFlag('first', abbr: 'f');
273 parser.addFlag('second', abbr: 's');
274 parser.addFlag('third', abbr: 't');
275
276 var args = parser.parse(['-s', '-tf']);
277 expect(args['first'], isTrue);
278 expect(args['second'], isTrue);
279 expect(args['third'], isTrue);
280 });
281
282 test('can take arguments without a space separating', () {
283 var parser = new ArgParser();
284 parser.addOption('file', abbr: 'f');
285
286 var args = parser.parse(['-flip']);
287 expect(args['file'], equals('lip'));
288 });
289
290 test('can take arguments with a space separating', () {
291 var parser = new ArgParser();
292 parser.addOption('file', abbr: 'f');
293
294 var args = parser.parse(['-f', 'name']);
295 expect(args['file'], equals('name'));
296 });
297
298 test('allow non-option characters in the value', () {
299 var parser = new ArgParser();
300 parser.addOption('apple', abbr: 'a');
301
302 var args = parser.parse(['-ab?!c']);
303 expect(args['apple'], equals('b?!c'));
304 });
305
306 test('throw if unknown', () {
307 var parser = new ArgParser();
308 throwsFormat(parser, ['-f']);
309 });
310
311 test('throw if the value is missing', () {
312 var parser = new ArgParser();
313 parser.addOption('file', abbr: 'f');
314
315 throwsFormat(parser, ['-f']);
316 });
317
318 test('does not throw if the value looks like an option', () {
319 var parser = new ArgParser();
320 parser.addOption('file', abbr: 'f');
321 parser.addOption('other');
322
323 expect(parser.parse(['-f', '--other'])['file'], equals('--other'));
324 expect(parser.parse(['-f', '--unknown'])['file'], equals('--unknown'));
325 expect(parser.parse(['-f', '-abbr'])['file'], equals('-abbr'));
326 expect(parser.parse(['-f', '--'])['file'], equals('--'));
327 });
328
329 test('throw if the value is not allowed', () {
330 var parser = new ArgParser();
331 parser.addOption('mode', abbr: 'm', allowed: ['debug', 'release']);
332
333 throwsFormat(parser, ['-mprofile']);
334 });
335
336 test('throw if a comma-separated value is not allowed', () {
337 var parser = new ArgParser();
338 parser.addOption('mode', abbr: 'm', allowMultiple: true,
339 allowed: ['debug', 'release']);
340
341 throwsFormat(parser, ['-mdebug,profile']);
342 });
343
344 test('throw if any but the first is not a flag', () {
345 var parser = new ArgParser();
346 parser.addFlag('apple', abbr: 'a');
347 parser.addOption('banana', abbr: 'b'); // Takes an argument.
348 parser.addFlag('cherry', abbr: 'c');
349
350 throwsFormat(parser, ['-abc']);
351 });
352
353 test('throw if it has a value but the option is a flag', () {
354 var parser = new ArgParser();
355 parser.addFlag('apple', abbr: 'a');
356 parser.addFlag('banana', abbr: 'b');
357
358 // The '?!' means this can only be understood as '--apple b?!c'.
359 throwsFormat(parser, ['-ab?!c']);
360 });
361
362 test('are case-sensitive', () {
363 var parser = new ArgParser();
364 parser.addFlag('file', abbr: 'f');
365 parser.addFlag('force', abbr: 'F');
366 var results = parser.parse(['-f']);
367 expect(results['file'], isTrue);
368 expect(results['force'], isFalse);
369 });
370 });
371
372 group('options', () {
373 test('are parsed if present', () {
374 var parser = new ArgParser();
375 parser.addOption('mode');
376 var args = parser.parse(['--mode=release']);
377 expect(args['mode'], equals('release'));
378 });
379
380 test('are null if not present', () {
381 var parser = new ArgParser();
382 parser.addOption('mode');
383 var args = parser.parse([]);
384 expect(args['mode'], isNull);
385 });
386
387 test('default if missing', () {
388 var parser = new ArgParser();
389 parser.addOption('mode', defaultsTo: 'debug');
390 var args = parser.parse([]);
391 expect(args['mode'], equals('debug'));
392 });
393
394 test('allow the value to be separated by whitespace', () {
395 var parser = new ArgParser();
396 parser.addOption('mode');
397 var args = parser.parse(['--mode', 'release']);
398 expect(args['mode'], equals('release'));
399 });
400
401 test('throw if unknown', () {
402 var parser = new ArgParser();
403 throwsFormat(parser, ['--unknown']);
404 throwsFormat(parser, ['--nobody']); // Starts with "no".
405 });
406
407 test('throw if the arg does not include a value', () {
408 var parser = new ArgParser();
409 parser.addOption('mode');
410 throwsFormat(parser, ['--mode']);
411 });
412
413 test('do not throw if the value looks like an option', () {
414 var parser = new ArgParser();
415 parser.addOption('mode');
416 parser.addOption('other');
417
418 expect(parser.parse(['--mode', '--other'])['mode'], equals('--other'));
419 expect(parser.parse(['--mode', '--unknown'])['mode'],
420 equals('--unknown'));
421 expect(parser.parse(['--mode', '-abbr'])['mode'], equals('-abbr'));
422 expect(parser.parse(['--mode', '--'])['mode'], equals('--'));
423 });
424
425 test('do not throw if the value is in the allowed set', () {
426 var parser = new ArgParser();
427 parser.addOption('mode', allowed: ['debug', 'release']);
428 var args = parser.parse(['--mode=debug']);
429 expect(args['mode'], equals('debug'));
430 });
431
432 test('throw if the value is not in the allowed set', () {
433 var parser = new ArgParser();
434 parser.addOption('mode', allowed: ['debug', 'release']);
435 throwsFormat(parser, ['--mode=profile']);
436 });
437
438 test('returns last provided value', () {
439 var parser = new ArgParser();
440 parser.addOption('define');
441 var args = parser.parse(['--define=1', '--define=2']);
442 expect(args['define'], equals('2'));
443 });
444
445 test('returns a List if multi-valued', () {
446 var parser = new ArgParser();
447 parser.addOption('define', allowMultiple: true);
448 var args = parser.parse(['--define=1']);
449 expect(args['define'], equals(['1']));
450 args = parser.parse(['--define=1', '--define=2']);
451 expect(args['define'], equals(['1', '2']));
452 });
453
454 test('returns the default value for multi-valued arguments '
455 'if not explicitly set', () {
456 var parser = new ArgParser();
457 parser.addOption('define', defaultsTo: '0', allowMultiple: true);
458 var args = parser.parse(['']);
459 expect(args['define'], equals(['0']));
460 });
461
462 test('are case-sensitive', () {
463 var parser = new ArgParser();
464 parser.addOption('verbose', defaultsTo: 'no');
465 parser.addOption('Verbose', defaultsTo: 'no');
466 var results = parser.parse(['--verbose', 'chatty']);
467 expect(results['verbose'], equals('chatty'));
468 expect(results['Verbose'], equals('no'));
469 });
470 });
471
472 group('remaining args', () {
473 test('stops parsing args when a non-option-like arg is encountered', () {
474 var parser = new ArgParser();
475 parser.addFlag('woof');
476 parser.addOption('meow');
477 parser.addOption('tweet', defaultsTo: 'bird');
478
479 var results = parser.parse(['--woof', '--meow', 'v', 'not', 'option']);
480 expect(results['woof'], isTrue);
481 expect(results['meow'], equals('v'));
482 expect(results['tweet'], equals('bird'));
483 expect(results.rest, equals(['not', 'option']));
484 });
485
486 test('consumes "--" and stops', () {
487 var parser = new ArgParser();
488 parser.addFlag('woof', defaultsTo: false);
489 parser.addOption('meow', defaultsTo: 'kitty');
490
491 var results = parser.parse(['--woof', '--', '--meow']);
492 expect(results['woof'], isTrue);
493 expect(results['meow'], equals('kitty'));
494 expect(results.rest, equals(['--meow']));
495 });
496
497 test('leaves "--" if not the first non-option', () {
498 var parser = new ArgParser();
499 parser.addFlag('woof');
500
501 var results = parser.parse(['--woof', 'stop', '--', 'arg']);
502 expect(results['woof'], isTrue);
503 expect(results.rest, equals(['stop', '--', 'arg']));
504 });
505 });
506 });
507 }
OLDNEW
« no previous file with comments | « packages/args/test/command_test.dart ('k') | packages/args/test/trailing_options_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698