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 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 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 test('for absent, allowMultiple, options are invoked with value ' | 192 test('for absent, allowMultiple, options are invoked with value ' |
193 'as an empty list.', () { | 193 'as an empty list.', () { |
194 var a; | 194 var a; |
195 var parser = new ArgParser(); | 195 var parser = new ArgParser(); |
196 parser.addOption('a', | 196 parser.addOption('a', |
197 allowMultiple: true, callback: (value) => a = value); | 197 allowMultiple: true, callback: (value) => a = value); |
198 | 198 |
199 parser.parse([]); | 199 parser.parse([]); |
200 expect(a, isEmpty); | 200 expect(a, isEmpty); |
201 }); | 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 parses empty strings', () { |
| 214 var a; |
| 215 var parser = new ArgParser(); |
| 216 parser.addOption('a', |
| 217 allowMultiple: true, callback: (value) => a = value); |
| 218 |
| 219 parser.parse(['--a=,v', '--a=w,', '--a=,', '--a=x,,y', '--a', '']); |
| 220 expect(a, equals(['', 'v', 'w', '', '', '', 'x', '', 'y', ''])); |
| 221 }); |
| 222 |
| 223 test('allowMultiple with allowed parses comma-separated strings', () { |
| 224 var a; |
| 225 var parser = new ArgParser(); |
| 226 parser.addOption('a', |
| 227 allowMultiple: true, |
| 228 allowed: ['v', 'w', 'x'], |
| 229 callback: (value) => a = value); |
| 230 |
| 231 parser.parse(['--a=v,w', '--a=x']); |
| 232 expect(a, equals(['v', 'w', 'x'])); |
| 233 }); |
202 }); | 234 }); |
203 | 235 |
204 group('abbreviations', () { | 236 group('abbreviations', () { |
205 test('are parsed with a preceding "-"', () { | 237 test('are parsed with a preceding "-"', () { |
206 var parser = new ArgParser(); | 238 var parser = new ArgParser(); |
207 parser.addFlag('arg', abbr: 'a'); | 239 parser.addFlag('arg', abbr: 'a'); |
208 | 240 |
209 var args = parser.parse(['-a']); | 241 var args = parser.parse(['-a']); |
210 expect(args['arg'], isTrue); | 242 expect(args['arg'], isTrue); |
211 }); | 243 }); |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
280 throwsFormat(parser, ['-f', '-abbr']); | 312 throwsFormat(parser, ['-f', '-abbr']); |
281 }); | 313 }); |
282 | 314 |
283 test('throw if the value is not allowed', () { | 315 test('throw if the value is not allowed', () { |
284 var parser = new ArgParser(); | 316 var parser = new ArgParser(); |
285 parser.addOption('mode', abbr: 'm', allowed: ['debug', 'release']); | 317 parser.addOption('mode', abbr: 'm', allowed: ['debug', 'release']); |
286 | 318 |
287 throwsFormat(parser, ['-mprofile']); | 319 throwsFormat(parser, ['-mprofile']); |
288 }); | 320 }); |
289 | 321 |
| 322 test('throw if a comma-separated value is not allowed', () { |
| 323 var parser = new ArgParser(); |
| 324 parser.addOption('mode', abbr: 'm', allowMultiple: true, |
| 325 allowed: ['debug', 'release']); |
| 326 |
| 327 throwsFormat(parser, ['-mdebug,profile']); |
| 328 }); |
| 329 |
290 test('throw if any but the first is not a flag', () { | 330 test('throw if any but the first is not a flag', () { |
291 var parser = new ArgParser(); | 331 var parser = new ArgParser(); |
292 parser.addFlag('apple', abbr: 'a'); | 332 parser.addFlag('apple', abbr: 'a'); |
293 parser.addOption('banana', abbr: 'b'); // Takes an argument. | 333 parser.addOption('banana', abbr: 'b'); // Takes an argument. |
294 parser.addFlag('cherry', abbr: 'c'); | 334 parser.addFlag('cherry', abbr: 'c'); |
295 | 335 |
296 throwsFormat(parser, ['-abc']); | 336 throwsFormat(parser, ['-abc']); |
297 }); | 337 }); |
298 | 338 |
299 test('throw if it has a value but the option is a flag', () { | 339 test('throw if it has a value but the option is a flag', () { |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
442 var parser = new ArgParser(); | 482 var parser = new ArgParser(); |
443 parser.addFlag('woof'); | 483 parser.addFlag('woof'); |
444 | 484 |
445 var results = parser.parse(['--woof', 'stop', '--', 'arg']); | 485 var results = parser.parse(['--woof', 'stop', '--', 'arg']); |
446 expect(results['woof'], isTrue); | 486 expect(results['woof'], isTrue); |
447 expect(results.rest, equals(['stop', '--', 'arg'])); | 487 expect(results.rest, equals(['stop', '--', 'arg'])); |
448 }); | 488 }); |
449 }); | 489 }); |
450 }); | 490 }); |
451 } | 491 } |
OLD | NEW |