Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 args_test; | 5 library args_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 | 9 |
| 10 main() { | 10 main() { |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 29 }); | 29 }); |
| 30 | 30 |
| 31 test('are false if missing with no default', () { | 31 test('are false if missing with no default', () { |
| 32 var parser = new ArgParser(); | 32 var parser = new ArgParser(); |
| 33 parser.addFlag('verbose'); | 33 parser.addFlag('verbose'); |
| 34 | 34 |
| 35 var args = parser.parse([]); | 35 var args = parser.parse([]); |
| 36 expect(args['verbose'], isFalse); | 36 expect(args['verbose'], isFalse); |
| 37 }); | 37 }); |
| 38 | 38 |
| 39 test('throws if given a value', () { | 39 test('throw if given a value', () { |
| 40 var parser = new ArgParser(); | 40 var parser = new ArgParser(); |
| 41 parser.addFlag('verbose'); | 41 parser.addFlag('verbose'); |
| 42 | 42 |
| 43 throwsFormat(parser, ['--verbose=true']); | 43 throwsFormat(parser, ['--verbose=true']); |
| 44 }); | 44 }); |
| 45 }); | 45 }); |
| 46 | 46 |
| 47 group('flags negated with "no-"', () { | 47 group('flags negated with "no-"', () { |
| 48 test('set the flag to false', () { | 48 test('set the flag to false', () { |
| 49 var parser = new ArgParser(); | 49 var parser = new ArgParser(); |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 231 }); | 231 }); |
| 232 | 232 |
| 233 test('allow non-option characters in the value', () { | 233 test('allow non-option characters in the value', () { |
| 234 var parser = new ArgParser(); | 234 var parser = new ArgParser(); |
| 235 parser.addOption('apple', abbr: 'a'); | 235 parser.addOption('apple', abbr: 'a'); |
| 236 | 236 |
| 237 var args = parser.parse(['-ab?!c']); | 237 var args = parser.parse(['-ab?!c']); |
| 238 expect(args['apple'], equals('b?!c')); | 238 expect(args['apple'], equals('b?!c')); |
| 239 }); | 239 }); |
| 240 | 240 |
| 241 test('are case-sensitive', () { | |
| 242 var parser = new ArgParser(); | |
| 243 parser.addFlag('recurse', defaultsTo: false, abbr:'R'); | |
| 244 | |
| 245 var results = parser.parse(['-R']); | |
| 246 expect(results['recurse'], isTrue); | |
| 247 expect(results.rest, []); | |
| 248 | |
| 249 throwsFormat(parser, ['-r']); | |
| 250 }); | |
| 251 | |
| 241 test('throw if unknown', () { | 252 test('throw if unknown', () { |
| 242 var parser = new ArgParser(); | 253 var parser = new ArgParser(); |
| 243 throwsFormat(parser, ['-f']); | 254 throwsFormat(parser, ['-f']); |
| 244 }); | 255 }); |
| 245 | 256 |
| 246 test('throw if the value is missing', () { | 257 test('throw if the value is missing', () { |
| 247 var parser = new ArgParser(); | 258 var parser = new ArgParser(); |
| 248 parser.addOption('file', abbr: 'f'); | 259 parser.addOption('file', abbr: 'f'); |
| 249 | 260 |
| 250 throwsFormat(parser, ['-f']); | 261 throwsFormat(parser, ['-f']); |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 343 var args = parser.parse(['--mode=debug']); | 354 var args = parser.parse(['--mode=debug']); |
| 344 expect(args['mode'], equals('debug')); | 355 expect(args['mode'], equals('debug')); |
| 345 }); | 356 }); |
| 346 | 357 |
| 347 test('throw if the value is not in the allowed set', () { | 358 test('throw if the value is not in the allowed set', () { |
| 348 var parser = new ArgParser(); | 359 var parser = new ArgParser(); |
| 349 parser.addOption('mode', allowed: ['debug', 'release']); | 360 parser.addOption('mode', allowed: ['debug', 'release']); |
| 350 throwsFormat(parser, ['--mode=profile']); | 361 throwsFormat(parser, ['--mode=profile']); |
| 351 }); | 362 }); |
| 352 | 363 |
| 353 test('returns last provided value', () { | 364 test('return last provided value', () { |
| 354 var parser = new ArgParser(); | 365 var parser = new ArgParser(); |
| 355 parser.addOption('define'); | 366 parser.addOption('define'); |
| 356 var args = parser.parse(['--define=1', '--define=2']); | 367 var args = parser.parse(['--define=1', '--define=2']); |
| 357 expect(args['define'], equals('2')); | 368 expect(args['define'], equals('2')); |
| 358 }); | 369 }); |
| 359 | 370 |
| 360 test('returns a List if multi-valued', () { | 371 test('return a list if multi-valued', () { |
| 361 var parser = new ArgParser(); | 372 var parser = new ArgParser(); |
| 362 parser.addOption('define', allowMultiple: true); | 373 parser.addOption('define', allowMultiple: true); |
| 363 var args = parser.parse(['--define=1']); | 374 var args = parser.parse(['--define=1']); |
| 364 expect(args['define'], equals(['1'])); | 375 expect(args['define'], equals(['1'])); |
| 365 args = parser.parse(['--define=1', '--define=2']); | 376 args = parser.parse(['--define=1', '--define=2']); |
| 366 expect(args['define'], equals(['1','2'])); | 377 expect(args['define'], equals(['1','2'])); |
| 367 }); | 378 }); |
| 368 | 379 |
| 369 test('returns the default value for multi-valued arguments ' | 380 test('return the default value for multi-valued arguments if not ' |
| 370 'if not explicitly set', () { | 381 'explicitly set', () { |
| 371 var parser = new ArgParser(); | 382 var parser = new ArgParser(); |
| 372 parser.addOption('define', defaultsTo: '0', allowMultiple: true); | 383 parser.addOption('define', defaultsTo: '0', allowMultiple: true); |
| 373 var args = parser.parse(['']); | 384 var args = parser.parse(['']); |
| 374 expect(args['define'], equals(['0'])); | 385 expect(args['define'], equals(['0'])); |
| 375 }); | 386 }); |
| 376 }); | 387 }); |
| 377 | 388 |
| 378 group('remaining args', () { | 389 group('remaining args', () { |
| 379 test('stops parsing args when a non-option-like arg is encountered', () { | 390 test('accumulates unhandled arguments', () { |
| 380 var parser = new ArgParser(); | 391 var parser = new ArgParser(); |
| 381 parser.addFlag('woof'); | 392 parser.addFlag('woof'); |
| 382 parser.addOption('meow'); | 393 parser.addOption('meow'); |
| 383 parser.addOption('tweet', defaultsTo: 'bird'); | 394 var results = parser.parse(['a', '--woof', 'b', '--meow', 'c', 'd']); |
| 384 | |
| 385 var results = parser.parse(['--woof', '--meow', 'v', 'not', 'option']); | |
| 386 expect(results['woof'], isTrue); | 395 expect(results['woof'], isTrue); |
| 387 expect(results['meow'], equals('v')); | 396 expect(results['meow'], equals('c')); |
| 388 expect(results['tweet'], equals('bird')); | 397 expect(results.rest, orderedEquals(['a', 'b', 'd'])); |
|
ahe
2013/01/10 19:51:40
Could you add a test that you reject unknown argum
Bob Nystrom
2013/01/10 23:16:40
The tests on 252 and 329 should cover that, I thin
ahe
2013/01/11 05:37:28
Yes. I guess I was skipping exceptions when I scan
| |
| 389 expect(results.rest, orderedEquals(['not', 'option'])); | |
| 390 }); | 398 }); |
| 391 | 399 |
| 392 test('stops parsing at "--"', () { | 400 test('stops parsing at "--"', () { |
| 393 var parser = new ArgParser(); | 401 var parser = new ArgParser(); |
| 394 parser.addFlag('woof', defaultsTo: false); | 402 parser.addFlag('woof', defaultsTo: false); |
| 395 parser.addOption('meow', defaultsTo: 'kitty'); | 403 parser.addOption('meow', defaultsTo: 'kitty'); |
| 396 | 404 |
| 397 var results = parser.parse(['--woof', '--', '--meow']); | 405 var results = parser.parse(['--woof', '--', '--meow']); |
| 398 expect(results['woof'], isTrue); | 406 expect(results['woof'], isTrue); |
| 399 expect(results['meow'], equals('kitty')); | 407 expect(results['meow'], equals('kitty')); |
| 400 expect(results.rest, orderedEquals(['--meow'])); | 408 expect(results.rest, orderedEquals(['--meow'])); |
| 401 }); | 409 }); |
| 402 | |
| 403 test('handles options with case-sensitivity', () { | |
| 404 var parser = new ArgParser(); | |
| 405 parser.addFlag('recurse', defaultsTo: false, abbr:'R'); | |
| 406 var results = parser.parse(['-R']); | |
| 407 expect(results['recurse'], isTrue); | |
| 408 expect(results.rest, [ ]); | |
| 409 throwsFormat(parser, ['-r']); | |
| 410 }); | |
| 411 }); | 410 }); |
| 412 }); | 411 }); |
| 413 } | 412 } |
| 414 | 413 |
| 415 throwsIllegalArg(function) { | 414 throwsIllegalArg(function) { |
| 416 expect(function, throwsArgumentError); | 415 expect(function, throwsArgumentError); |
| 417 } | 416 } |
| 418 | 417 |
| 419 throwsFormat(ArgParser parser, List<String> args) { | 418 throwsFormat(ArgParser parser, List<String> args) { |
| 420 expect(() => parser.parse(args), throwsFormatException); | 419 expect(() => parser.parse(args), throwsFormatException); |
| 421 } | 420 } |
| OLD | NEW |