| 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 14 matching lines...) Expand all  Loading... | 
| 25       var parser = new ArgParser(); | 25       var parser = new ArgParser(); | 
| 26       parser.addFlag('foo', abbr: 'f'); | 26       parser.addFlag('foo', abbr: 'f'); | 
| 27       throwsIllegalArg(() => parser.addFlag('flummox', abbr: 'f')); | 27       throwsIllegalArg(() => parser.addFlag('flummox', abbr: 'f')); | 
| 28     }); | 28     }); | 
| 29 | 29 | 
| 30     test('throws ArgumentError if the abbreviation is longer ' | 30     test('throws ArgumentError if the abbreviation is longer ' | 
| 31          'than one character', () { | 31          'than one character', () { | 
| 32       var parser = new ArgParser(); | 32       var parser = new ArgParser(); | 
| 33       throwsIllegalArg(() => parser.addFlag('flummox', abbr: 'flu')); | 33       throwsIllegalArg(() => parser.addFlag('flummox', abbr: 'flu')); | 
| 34     }); | 34     }); | 
|  | 35 | 
|  | 36     test('throws ArgumentError if a flag name is invalid', () { | 
|  | 37       var parser = new ArgParser(); | 
|  | 38 | 
|  | 39       for(var name in _INVALID_OPTIONS) { | 
|  | 40         var reason = '${Error.safeToString(name)} is not valid'; | 
|  | 41         throwsIllegalArg(() => parser.addFlag(name), reason: reason); | 
|  | 42       } | 
|  | 43     }); | 
|  | 44 | 
|  | 45     test('accepts valid flag names', () { | 
|  | 46       var parser = new ArgParser(); | 
|  | 47 | 
|  | 48       for(var name in _VALID_OPTIONS) { | 
|  | 49         var reason = '${Error.safeToString(name)} is valid'; | 
|  | 50         expect(() => parser.addFlag(name), returnsNormally, reason: reason); | 
|  | 51       } | 
|  | 52     }); | 
| 35   }); | 53   }); | 
| 36 | 54 | 
| 37   group('ArgParser.addOption()', () { | 55   group('ArgParser.addOption()', () { | 
| 38     test('throws ArgumentError if the flag already exists', () { | 56     test('throws ArgumentError if the flag already exists', () { | 
| 39       var parser = new ArgParser(); | 57       var parser = new ArgParser(); | 
| 40       parser.addFlag('foo'); | 58       parser.addFlag('foo'); | 
| 41       throwsIllegalArg(() => parser.addOption('foo')); | 59       throwsIllegalArg(() => parser.addOption('foo')); | 
| 42     }); | 60     }); | 
| 43 | 61 | 
| 44     test('throws ArgumentError if the option already exists', () { | 62     test('throws ArgumentError if the option already exists', () { | 
| 45       var parser = new ArgParser(); | 63       var parser = new ArgParser(); | 
| 46       parser.addOption('foo'); | 64       parser.addOption('foo'); | 
| 47       throwsIllegalArg(() => parser.addOption('foo')); | 65       throwsIllegalArg(() => parser.addOption('foo')); | 
| 48     }); | 66     }); | 
| 49 | 67 | 
| 50     test('throws ArgumentError if the abbreviation exists', () { | 68     test('throws ArgumentError if the abbreviation exists', () { | 
| 51       var parser = new ArgParser(); | 69       var parser = new ArgParser(); | 
| 52       parser.addFlag('foo', abbr: 'f'); | 70       parser.addFlag('foo', abbr: 'f'); | 
| 53       throwsIllegalArg(() => parser.addOption('flummox', abbr: 'f')); | 71       throwsIllegalArg(() => parser.addOption('flummox', abbr: 'f')); | 
| 54     }); | 72     }); | 
| 55 | 73 | 
| 56     test('throws ArgumentError if the abbreviation is longer ' | 74     test('throws ArgumentError if the abbreviation is longer ' | 
| 57          'than one character', () { | 75          'than one character', () { | 
| 58       var parser = new ArgParser(); | 76       var parser = new ArgParser(); | 
| 59       throwsIllegalArg(() => parser.addOption('flummox', abbr: 'flu')); | 77       throwsIllegalArg(() => parser.addOption('flummox', abbr: 'flu')); | 
| 60     }); | 78     }); | 
|  | 79 | 
|  | 80     test('throws ArgumentError if the abbreviation is empty', () { | 
|  | 81       var parser = new ArgParser(); | 
|  | 82       throwsIllegalArg(() => parser.addOption('flummox', abbr: '')); | 
|  | 83     }); | 
|  | 84 | 
|  | 85     test('throws ArgumentError if the abbreviation is an invalid value', () { | 
|  | 86       var parser = new ArgParser(); | 
|  | 87       for(var name in _INVALID_OPTIONS.where((v) => v != null)) { | 
|  | 88         throwsIllegalArg(() => parser.addOption('flummox', abbr: name)); | 
|  | 89       } | 
|  | 90     }); | 
|  | 91 | 
|  | 92     test('throws ArgumentError if the abbreviation is a dash', () { | 
|  | 93       var parser = new ArgParser(); | 
|  | 94       throwsIllegalArg(() => parser.addOption('flummox', abbr: '-')); | 
|  | 95     }); | 
|  | 96 | 
|  | 97     test('allows explict null value for "abbr"', () { | 
|  | 98       var parser = new ArgParser(); | 
|  | 99       expect(() => parser.addOption('flummox', abbr: null), returnsNormally); | 
|  | 100     }); | 
|  | 101 | 
|  | 102     test('throws ArgumentError if an option name is invalid', () { | 
|  | 103       var parser = new ArgParser(); | 
|  | 104 | 
|  | 105       for(var name in _INVALID_OPTIONS) { | 
|  | 106         var reason = '${Error.safeToString(name)} is not valid'; | 
|  | 107         throwsIllegalArg(() => parser.addOption(name), reason: reason); | 
|  | 108       } | 
|  | 109     }); | 
|  | 110 | 
|  | 111     test('accepts valid option names', () { | 
|  | 112       var parser = new ArgParser(); | 
|  | 113 | 
|  | 114       for(var name in _VALID_OPTIONS) { | 
|  | 115         var reason = '${Error.safeToString(name)} is valid'; | 
|  | 116         expect(() => parser.addOption(name), returnsNormally, reason: reason); | 
|  | 117       } | 
|  | 118     }); | 
| 61   }); | 119   }); | 
| 62 | 120 | 
| 63   group('ArgParser.getDefault()', () { | 121   group('ArgParser.getDefault()', () { | 
| 64     test('returns the default value for an option', () { | 122     test('returns the default value for an option', () { | 
| 65       var parser = new ArgParser(); | 123       var parser = new ArgParser(); | 
| 66       parser.addOption('mode', defaultsTo: 'debug'); | 124       parser.addOption('mode', defaultsTo: 'debug'); | 
| 67       expect(parser.getDefault('mode'), 'debug'); | 125       expect(parser.getDefault('mode'), 'debug'); | 
| 68     }); | 126     }); | 
| 69 | 127 | 
| 70     test('throws if the option is unknown', () { | 128     test('throws if the option is unknown', () { | 
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 148 | 206 | 
| 149   group('ArgResults[]', () { | 207   group('ArgResults[]', () { | 
| 150     test('throws if the name is not an option', () { | 208     test('throws if the name is not an option', () { | 
| 151       var parser = new ArgParser(); | 209       var parser = new ArgParser(); | 
| 152       var results = parser.parse([]); | 210       var results = parser.parse([]); | 
| 153       throwsIllegalArg(() => results['unknown']); | 211       throwsIllegalArg(() => results['unknown']); | 
| 154     }); | 212     }); | 
| 155   }); | 213   }); | 
| 156 } | 214 } | 
| 157 | 215 | 
| 158 throwsIllegalArg(function) { | 216 throwsIllegalArg(function, {String reason: null}) { | 
| 159   expect(function, throwsArgumentError); | 217   expect(function, throwsArgumentError, reason: reason); | 
| 160 } | 218 } | 
| 161 | 219 | 
| 162 throwsFormat(ArgParser parser, List<String> args) { | 220 throwsFormat(ArgParser parser, List<String> args) { | 
| 163   expect(() => parser.parse(args), throwsFormatException); | 221   expect(() => parser.parse(args), throwsFormatException); | 
| 164 } | 222 } | 
|  | 223 | 
|  | 224 const _INVALID_OPTIONS = const [ | 
|  | 225  ' ', '', '-', '--', '--foo', | 
|  | 226  ' with space', | 
|  | 227  'with\ttab', | 
|  | 228  'with\rcarriage\rreturn', | 
|  | 229  'with\nline\nfeed', | 
|  | 230  "'singlequotes'", | 
|  | 231  '"doublequotes"', | 
|  | 232  'back\\slash', | 
|  | 233  'forward/slash' | 
|  | 234 ]; | 
|  | 235 | 
|  | 236 const _VALID_OPTIONS = const [ | 
|  | 237  'a' // one char | 
|  | 238  'contains-dash', | 
|  | 239  'contains_underscore', | 
|  | 240  'ends-with-dash-', | 
|  | 241  'contains--doubledash--', | 
|  | 242  '1starts-with-number', | 
|  | 243  'contains-a-1number', | 
|  | 244  'ends-with-a-number8' | 
|  | 245 ]; | 
| OLD | NEW | 
|---|