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

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

Issue 260963007: Move allowTrailingOptions into ArgParser. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 7 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 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 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library parse_test; 5 library parse_test;
6 6
7 import 'package:unittest/unittest.dart'; 7 import 'package:unittest/unittest.dart';
8 import 'package:args/args.dart'; 8 import 'package:args/args.dart';
9 import 'utils.dart'; 9 import 'utils.dart';
10 10
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
46 var args = parser.parse([]); 46 var args = parser.parse([]);
47 expect(args['verbose'], isFalse); 47 expect(args['verbose'], isFalse);
48 }); 48 });
49 49
50 test('throws if given a value', () { 50 test('throws if given a value', () {
51 var parser = new ArgParser(); 51 var parser = new ArgParser();
52 parser.addFlag('verbose'); 52 parser.addFlag('verbose');
53 53
54 throwsFormat(parser, ['--verbose=true']); 54 throwsFormat(parser, ['--verbose=true']);
55 }); 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 });
56 }); 65 });
57 66
58 group('flags negated with "no-"', () { 67 group('flags negated with "no-"', () {
59 test('set the flag to false', () { 68 test('set the flag to false', () {
60 var parser = new ArgParser(); 69 var parser = new ArgParser();
61 parser.addFlag('verbose'); 70 parser.addFlag('verbose');
62 71
63 var args = parser.parse(['--no-verbose']); 72 var args = parser.parse(['--no-verbose']);
64 expect(args['verbose'], isFalse); 73 expect(args['verbose'], isFalse);
65 }); 74 });
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after
288 }); 297 });
289 298
290 test('throw if it has a value but the option is a flag', () { 299 test('throw if it has a value but the option is a flag', () {
291 var parser = new ArgParser(); 300 var parser = new ArgParser();
292 parser.addFlag('apple', abbr: 'a'); 301 parser.addFlag('apple', abbr: 'a');
293 parser.addFlag('banana', abbr: 'b'); 302 parser.addFlag('banana', abbr: 'b');
294 303
295 // The '?!' means this can only be understood as '--apple b?!c'. 304 // The '?!' means this can only be understood as '--apple b?!c'.
296 throwsFormat(parser, ['-ab?!c']); 305 throwsFormat(parser, ['-ab?!c']);
297 }); 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 });
298 }); 316 });
299 317
300 group('options', () { 318 group('options', () {
301 test('are parsed if present', () { 319 test('are parsed if present', () {
302 var parser = new ArgParser(); 320 var parser = new ArgParser();
303 parser.addOption('mode'); 321 parser.addOption('mode');
304 var args = parser.parse(['--mode=release']); 322 var args = parser.parse(['--mode=release']);
305 expect(args['mode'], equals('release')); 323 expect(args['mode'], equals('release'));
306 }); 324 });
307 325
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
377 expect(args['define'], equals(['1','2'])); 395 expect(args['define'], equals(['1','2']));
378 }); 396 });
379 397
380 test('returns the default value for multi-valued arguments ' 398 test('returns the default value for multi-valued arguments '
381 'if not explicitly set', () { 399 'if not explicitly set', () {
382 var parser = new ArgParser(); 400 var parser = new ArgParser();
383 parser.addOption('define', defaultsTo: '0', allowMultiple: true); 401 parser.addOption('define', defaultsTo: '0', allowMultiple: true);
384 var args = parser.parse(['']); 402 var args = parser.parse(['']);
385 expect(args['define'], equals(['0'])); 403 expect(args['define'], equals(['0']));
386 }); 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 });
387 }); 414 });
388 415
389 group('remaining args', () { 416 group('remaining args', () {
390 test('stops parsing args when a non-option-like arg is encountered', () { 417 test('stops parsing args when a non-option-like arg is encountered', () {
391 var parser = new ArgParser(); 418 var parser = new ArgParser();
392 parser.addFlag('woof'); 419 parser.addFlag('woof');
393 parser.addOption('meow'); 420 parser.addOption('meow');
394 parser.addOption('tweet', defaultsTo: 'bird'); 421 parser.addOption('tweet', defaultsTo: 'bird');
395 422
396 var results = parser.parse(['--woof', '--meow', 'v', 'not', 'option']); 423 var results = parser.parse(['--woof', '--meow', 'v', 'not', 'option']);
397 expect(results['woof'], isTrue); 424 expect(results['woof'], isTrue);
398 expect(results['meow'], equals('v')); 425 expect(results['meow'], equals('v'));
399 expect(results['tweet'], equals('bird')); 426 expect(results['tweet'], equals('bird'));
400 expect(results.rest, orderedEquals(['not', 'option'])); 427 expect(results.rest, equals(['not', 'option']));
401 }); 428 });
402 429
403 test('stops parsing at "--"', () { 430 test('consumes "--" and stops', () {
404 var parser = new ArgParser(); 431 var parser = new ArgParser();
405 parser.addFlag('woof', defaultsTo: false); 432 parser.addFlag('woof', defaultsTo: false);
406 parser.addOption('meow', defaultsTo: 'kitty'); 433 parser.addOption('meow', defaultsTo: 'kitty');
407 434
408 var results = parser.parse(['--woof', '--', '--meow']); 435 var results = parser.parse(['--woof', '--', '--meow']);
409 expect(results['woof'], isTrue); 436 expect(results['woof'], isTrue);
410 expect(results['meow'], equals('kitty')); 437 expect(results['meow'], equals('kitty'));
411 expect(results.rest, orderedEquals(['--meow'])); 438 expect(results.rest, equals(['--meow']));
412 }); 439 });
413 440
414 test('handles options with case-sensitivity', () { 441 test('leaves "--" if not the first non-option', () {
415 var parser = new ArgParser(); 442 var parser = new ArgParser();
416 parser.addFlag('recurse', defaultsTo: false, abbr:'R'); 443 parser.addFlag('woof');
417 var results = parser.parse(['-R']); 444
418 expect(results['recurse'], isTrue); 445 var results = parser.parse(['--woof', 'stop', '--', 'arg']);
419 expect(results.rest, [ ]); 446 expect(results['woof'], isTrue);
420 throwsFormat(parser, ['-r']); 447 expect(results.rest, equals(['stop', '--', 'arg']));
421 }); 448 });
422 }); 449 });
423 }); 450 });
424 } 451 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698