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 import 'utils.dart'; | 9 import 'utils.dart'; |
10 | 10 |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 expect(parser.options.keys, equals(['a', 'd', 'b', 'c'])); | 182 expect(parser.options.keys, equals(['a', 'd', 'b', 'c'])); |
183 }); | 183 }); |
184 }); | 184 }); |
185 | 185 |
186 group('ArgResults', () { | 186 group('ArgResults', () { |
187 group('options', () { | 187 group('options', () { |
188 test('returns the provided options', () { | 188 test('returns the provided options', () { |
189 var parser = new ArgParser(); | 189 var parser = new ArgParser(); |
190 parser.addFlag('woof'); | 190 parser.addFlag('woof'); |
191 parser.addOption('meow'); | 191 parser.addOption('meow'); |
| 192 |
| 193 parser.addOption('missing-option'); |
| 194 parser.addFlag('missing-flag', defaultsTo: null); |
| 195 |
192 var args = parser.parse(['--woof', '--meow', 'kitty']); | 196 var args = parser.parse(['--woof', '--meow', 'kitty']); |
193 expect(args.options, hasLength(2)); | 197 expect(args.options, hasLength(2)); |
194 expect(args.options, contains('woof')); | 198 expect(args.options, contains('woof')); |
195 expect(args.options, contains('meow')); | 199 expect(args.options, contains('meow')); |
196 }); | 200 }); |
197 | 201 |
198 test('includes defaulted options', () { | 202 test('includes defaulted options', () { |
199 var parser = new ArgParser(); | 203 var parser = new ArgParser(); |
200 parser.addFlag('woof', defaultsTo: false); | 204 parser.addFlag('woof', defaultsTo: false); |
201 parser.addOption('meow', defaultsTo: 'kitty'); | 205 parser.addOption('meow', defaultsTo: 'kitty'); |
| 206 |
| 207 // Flags normally have a default value. |
| 208 parser.addFlag('moo'); |
| 209 |
| 210 parser.addOption('missing-option'); |
| 211 parser.addFlag('missing-flag', defaultsTo: null); |
| 212 |
202 var args = parser.parse([]); | 213 var args = parser.parse([]); |
203 expect(args.options, hasLength(2)); | 214 expect(args.options, hasLength(3)); |
204 expect(args.options, contains('woof')); | 215 expect(args.options, contains('woof')); |
205 expect(args.options, contains('meow')); | 216 expect(args.options, contains('meow')); |
| 217 expect(args.options, contains('moo')); |
206 }); | 218 }); |
207 }); | 219 }); |
208 | 220 |
209 test('[] throws if the name is not an option', () { | 221 test('[] throws if the name is not an option', () { |
210 var parser = new ArgParser(); | 222 var results = new ArgParser().parse([]); |
211 var results = parser.parse([]); | |
212 throwsIllegalArg(() => results['unknown']); | 223 throwsIllegalArg(() => results['unknown']); |
213 }); | 224 }); |
214 | 225 |
215 test('rest cannot be modified', () { | 226 test('rest cannot be modified', () { |
216 var results = new ArgResults({}, '', null, []); | 227 var results = new ArgParser().parse([]); |
217 expect(() => results.rest.add('oops'), throwsUnsupportedError); | 228 expect(() => results.rest.add('oops'), throwsUnsupportedError); |
218 }); | 229 }); |
| 230 |
| 231 group('.wasProvided()', () { |
| 232 test('throws if the name is not an option', () { |
| 233 var results = new ArgParser().parse([]); |
| 234 throwsIllegalArg(() => results.wasParsed('unknown')); |
| 235 }); |
| 236 |
| 237 test('returns true for parsed options', () { |
| 238 var parser = new ArgParser(); |
| 239 parser.addFlag('fast'); |
| 240 parser.addFlag('verbose'); |
| 241 parser.addOption('mode'); |
| 242 parser.addOption('output'); |
| 243 |
| 244 var results = parser.parse(['--fast', '--mode=debug']); |
| 245 |
| 246 expect(results.wasParsed('fast'), isTrue); |
| 247 expect(results.wasParsed('verbose'), isFalse); |
| 248 expect(results.wasParsed('mode'), isTrue); |
| 249 expect(results.wasParsed('output'), isFalse); |
| 250 }); |
| 251 }); |
| 252 }); |
| 253 |
| 254 group('Option', () { |
| 255 test('.getOrDefault() returns a type-specific default value', () { |
| 256 var parser = new ArgParser(); |
| 257 parser.addFlag('flag-no', defaultsTo: null); |
| 258 parser.addFlag('flag-def', defaultsTo: true); |
| 259 parser.addOption('single-no'); |
| 260 parser.addOption('single-def', defaultsTo: 'def'); |
| 261 parser.addOption('multi-no', allowMultiple: true); |
| 262 parser.addOption('multi-def', allowMultiple: true, defaultsTo: 'def'); |
| 263 |
| 264 expect(parser.options['flag-no'].getOrDefault(null), equals(null)); |
| 265 expect(parser.options['flag-no'].getOrDefault(false), equals(false)); |
| 266 expect(parser.options['flag-def'].getOrDefault(null), equals(true)); |
| 267 expect(parser.options['flag-def'].getOrDefault(false), equals(false)); |
| 268 expect(parser.options['single-no'].getOrDefault(null), equals(null)); |
| 269 expect(parser.options['single-no'].getOrDefault('v'), equals('v')); |
| 270 expect(parser.options['single-def'].getOrDefault(null), equals('def')); |
| 271 expect(parser.options['single-def'].getOrDefault('v'), equals('v')); |
| 272 expect(parser.options['multi-no'].getOrDefault(null), equals([])); |
| 273 expect(parser.options['multi-no'].getOrDefault(['v']), equals(['v'])); |
| 274 expect(parser.options['multi-def'].getOrDefault(null), equals(['def'])); |
| 275 expect(parser.options['multi-def'].getOrDefault(['v']), equals(['v'])); |
| 276 }); |
219 }); | 277 }); |
220 } | 278 } |
221 | 279 |
222 const _INVALID_OPTIONS = const [ | 280 const _INVALID_OPTIONS = const [ |
223 ' ', '', '-', '--', '--foo', | 281 ' ', '', '-', '--', '--foo', |
224 ' with space', | 282 ' with space', |
225 'with\ttab', | 283 'with\ttab', |
226 'with\rcarriage\rreturn', | 284 'with\rcarriage\rreturn', |
227 'with\nline\nfeed', | 285 'with\nline\nfeed', |
228 "'singlequotes'", | 286 "'singlequotes'", |
229 '"doublequotes"', | 287 '"doublequotes"', |
230 'back\\slash', | 288 'back\\slash', |
231 'forward/slash' | 289 'forward/slash' |
232 ]; | 290 ]; |
233 | 291 |
234 const _VALID_OPTIONS = const [ | 292 const _VALID_OPTIONS = const [ |
235 'a' // one char | 293 'a' // One character. |
236 'contains-dash', | 294 'contains-dash', |
237 'contains_underscore', | 295 'contains_underscore', |
238 'ends-with-dash-', | 296 'ends-with-dash-', |
239 'contains--doubledash--', | 297 'contains--doubledash--', |
240 '1starts-with-number', | 298 '1starts-with-number', |
241 'contains-a-1number', | 299 'contains-a-1number', |
242 'ends-with-a-number8' | 300 'ends-with-a-number8' |
243 ]; | 301 ]; |
OLD | NEW |