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

Side by Side Diff: test/parse_test.dart

Issue 975463004: Parse comma-separated multiple values. (Closed) Base URL: git@github.com:dart-lang/args@master
Patch Set: Code review changes Created 5 years, 9 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
« lib/src/arg_parser.dart ('K') | « test/args_test.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 181 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 doesn't parses comma-separated strings with "
214 "splitCommas: false", () {
215 var a;
216 var parser = new ArgParser();
217 parser.addOption('a',
218 allowMultiple: true,
219 splitCommas: false,
220 callback: (value) => a = value);
221
222 parser.parse(['--a=v,w', '--a=x']);
223 expect(a, equals(['v,w', 'x']));
224 });
225
226 test('allowMultiple parses empty strings', () {
227 var a;
228 var parser = new ArgParser();
229 parser.addOption('a',
230 allowMultiple: true, callback: (value) => a = value);
231
232 parser.parse(['--a=,v', '--a=w,', '--a=,', '--a=x,,y', '--a', '']);
233 expect(a, equals(['', 'v', 'w', '', '', '', 'x', '', 'y', '']));
234 });
235
236 test('allowMultiple with allowed parses comma-separated strings', () {
237 var a;
238 var parser = new ArgParser();
239 parser.addOption('a',
240 allowMultiple: true,
241 allowed: ['v', 'w', 'x'],
242 callback: (value) => a = value);
243
244 parser.parse(['--a=v,w', '--a=x']);
245 expect(a, equals(['v', 'w', 'x']));
246 });
202 }); 247 });
203 248
204 group('abbreviations', () { 249 group('abbreviations', () {
205 test('are parsed with a preceding "-"', () { 250 test('are parsed with a preceding "-"', () {
206 var parser = new ArgParser(); 251 var parser = new ArgParser();
207 parser.addFlag('arg', abbr: 'a'); 252 parser.addFlag('arg', abbr: 'a');
208 253
209 var args = parser.parse(['-a']); 254 var args = parser.parse(['-a']);
210 expect(args['arg'], isTrue); 255 expect(args['arg'], isTrue);
211 }); 256 });
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
280 throwsFormat(parser, ['-f', '-abbr']); 325 throwsFormat(parser, ['-f', '-abbr']);
281 }); 326 });
282 327
283 test('throw if the value is not allowed', () { 328 test('throw if the value is not allowed', () {
284 var parser = new ArgParser(); 329 var parser = new ArgParser();
285 parser.addOption('mode', abbr: 'm', allowed: ['debug', 'release']); 330 parser.addOption('mode', abbr: 'm', allowed: ['debug', 'release']);
286 331
287 throwsFormat(parser, ['-mprofile']); 332 throwsFormat(parser, ['-mprofile']);
288 }); 333 });
289 334
335 test('throw if a comma-separated value is not allowed', () {
336 var parser = new ArgParser();
337 parser.addOption('mode', abbr: 'm', allowMultiple: true,
338 allowed: ['debug', 'release']);
339
340 throwsFormat(parser, ['-mdebug,profile']);
341 });
342
290 test('throw if any but the first is not a flag', () { 343 test('throw if any but the first is not a flag', () {
291 var parser = new ArgParser(); 344 var parser = new ArgParser();
292 parser.addFlag('apple', abbr: 'a'); 345 parser.addFlag('apple', abbr: 'a');
293 parser.addOption('banana', abbr: 'b'); // Takes an argument. 346 parser.addOption('banana', abbr: 'b'); // Takes an argument.
294 parser.addFlag('cherry', abbr: 'c'); 347 parser.addFlag('cherry', abbr: 'c');
295 348
296 throwsFormat(parser, ['-abc']); 349 throwsFormat(parser, ['-abc']);
297 }); 350 });
298 351
299 test('throw if it has a value but the option is a flag', () { 352 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
442 var parser = new ArgParser(); 495 var parser = new ArgParser();
443 parser.addFlag('woof'); 496 parser.addFlag('woof');
444 497
445 var results = parser.parse(['--woof', 'stop', '--', 'arg']); 498 var results = parser.parse(['--woof', 'stop', '--', 'arg']);
446 expect(results['woof'], isTrue); 499 expect(results['woof'], isTrue);
447 expect(results.rest, equals(['stop', '--', 'arg'])); 500 expect(results.rest, equals(['stop', '--', 'arg']));
448 }); 501 });
449 }); 502 });
450 }); 503 });
451 } 504 }
OLDNEW
« lib/src/arg_parser.dart ('K') | « test/args_test.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698