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

Side by Side Diff: pkg/args/test/args_test.dart

Issue 12472019: pkg/args Option should be more strict about names and abbreviations (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 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 | Annotate | Revision Log
« no previous file with comments | « pkg/args/lib/args.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 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
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 _invalidOptions) {
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 _validOptions) {
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 _invalidOptions.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 _invalidOptions) {
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 _validOptions) {
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
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 _invalidOptions = const
225 [
Bob Nystrom 2013/03/14 17:37:11 Put the "[" on the preceding line, and the "]" on
226 ' ', '', '-', '--', '--foo',
227 ' with space',
228 'with\ttab',
229 'with\rcarriage\rreturn',
230 'with\nline\nfeed',
231 "'singlequotes'",
232 '"doublequotes"',
233 'back\\slash',
234 'forward/slash'];
235
236 const _validOptions = const
237 [
238 'a' // one char
239 'contains-dash',
240 'contains_underscore',
241 'ends-with-dash-',
242 'contains--doubledash--',
243 '1starts-with-number',
244 'contains-a-1number',
245 'ends-with-a-number8'];
OLDNEW
« no previous file with comments | « pkg/args/lib/args.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698