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

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

Issue 1400473008: Roll Observatory packages and add a roll script (Closed) Base URL: git@github.com:dart-lang/observatory_pub_packages.git@master
Patch Set: Created 5 years, 2 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
« no previous file with comments | « packages/args/pubspec.yaml ('k') | packages/args/test/command_parse_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 library args_test;
6
7 import 'package:test/test.dart';
8 import 'package:args/args.dart';
9 import 'utils.dart';
10
11 void main() {
12 group('ArgParser.addFlag()', () {
13 test('throws ArgumentError if the flag already exists', () {
14 var parser = new ArgParser();
15 parser.addFlag('foo');
16 throwsIllegalArg(() => parser.addFlag('foo'));
17 });
18
19 test('throws ArgumentError if the option already exists', () {
20 var parser = new ArgParser();
21 parser.addOption('foo');
22 throwsIllegalArg(() => parser.addFlag('foo'));
23 });
24
25 test('throws ArgumentError if the abbreviation exists', () {
26 var parser = new ArgParser();
27 parser.addFlag('foo', abbr: 'f');
28 throwsIllegalArg(() => parser.addFlag('flummox', abbr: 'f'));
29 });
30
31 test('throws ArgumentError if the abbreviation is longer '
32 'than one character', () {
33 var parser = new ArgParser();
34 throwsIllegalArg(() => parser.addFlag('flummox', abbr: 'flu'));
35 });
36
37 test('throws ArgumentError if a flag name is invalid', () {
38 var parser = new ArgParser();
39
40 for (var name in _INVALID_OPTIONS) {
41 var reason = '${Error.safeToString(name)} is not valid';
42 throwsIllegalArg(() => parser.addFlag(name), reason: reason);
43 }
44 });
45
46 test('accepts valid flag names', () {
47 var parser = new ArgParser();
48
49 for (var name in _VALID_OPTIONS) {
50 var reason = '${Error.safeToString(name)} is valid';
51 expect(() => parser.addFlag(name), returnsNormally, reason: reason);
52 }
53 });
54 });
55
56 group('ArgParser.addOption()', () {
57 test('throws ArgumentError if the flag already exists', () {
58 var parser = new ArgParser();
59 parser.addFlag('foo');
60 throwsIllegalArg(() => parser.addOption('foo'));
61 });
62
63 test('throws ArgumentError if the option already exists', () {
64 var parser = new ArgParser();
65 parser.addOption('foo');
66 throwsIllegalArg(() => parser.addOption('foo'));
67 });
68
69 test('throws ArgumentError if the abbreviation exists', () {
70 var parser = new ArgParser();
71 parser.addFlag('foo', abbr: 'f');
72 throwsIllegalArg(() => parser.addOption('flummox', abbr: 'f'));
73 });
74
75 test('throws ArgumentError if the abbreviation is longer '
76 'than one character', () {
77 var parser = new ArgParser();
78 throwsIllegalArg(() => parser.addOption('flummox', abbr: 'flu'));
79 });
80
81 test('throws ArgumentError if the abbreviation is empty', () {
82 var parser = new ArgParser();
83 throwsIllegalArg(() => parser.addOption('flummox', abbr: ''));
84 });
85
86 test('throws ArgumentError if the abbreviation is an invalid value', () {
87 var parser = new ArgParser();
88 for (var name in _INVALID_OPTIONS.where((v) => v != null)) {
89 throwsIllegalArg(() => parser.addOption('flummox', abbr: name));
90 }
91 });
92
93 test('throws ArgumentError if the abbreviation is a dash', () {
94 var parser = new ArgParser();
95 throwsIllegalArg(() => parser.addOption('flummox', abbr: '-'));
96 });
97
98 test('allows explict null value for "abbr"', () {
99 var parser = new ArgParser();
100 expect(() => parser.addOption('flummox', abbr: null), returnsNormally);
101 });
102
103 test('throws ArgumentError if an option name is invalid', () {
104 var parser = new ArgParser();
105
106 for (var name in _INVALID_OPTIONS) {
107 var reason = '${Error.safeToString(name)} is not valid';
108 throwsIllegalArg(() => parser.addOption(name), reason: reason);
109 }
110 });
111
112 test('throws ArgumentError if splitCommas is passed with allowMultiple: '
113 'false', () {
114 var parser = new ArgParser();
115 throwsIllegalArg(() => parser.addOption('flummox', splitCommas: true));
116 throwsIllegalArg(() => parser.addOption('flummox', splitCommas: false));
117 });
118
119 test('accepts valid option names', () {
120 var parser = new ArgParser();
121
122 for (var name in _VALID_OPTIONS) {
123 var reason = '${Error.safeToString(name)} is valid';
124 expect(() => parser.addOption(name), returnsNormally, reason: reason);
125 }
126 });
127 });
128
129 group('ArgParser.getDefault()', () {
130 test('returns the default value for an option', () {
131 var parser = new ArgParser();
132 parser.addOption('mode', defaultsTo: 'debug');
133 expect(parser.getDefault('mode'), 'debug');
134 });
135
136 test('throws if the option is unknown', () {
137 var parser = new ArgParser();
138 parser.addOption('mode', defaultsTo: 'debug');
139 throwsIllegalArg(() => parser.getDefault('undefined'));
140 });
141 });
142
143 group('ArgParser.commands', () {
144 test('returns an empty map if there are no commands', () {
145 var parser = new ArgParser();
146 expect(parser.commands, isEmpty);
147 });
148
149 test('returns the commands that were added', () {
150 var parser = new ArgParser();
151 parser.addCommand('hide');
152 parser.addCommand('seek');
153 expect(parser.commands, hasLength(2));
154 expect(parser.commands['hide'], isNotNull);
155 expect(parser.commands['seek'], isNotNull);
156 });
157
158 test('iterates over the commands in the order they were added', () {
159 var parser = new ArgParser();
160 parser.addCommand('a');
161 parser.addCommand('d');
162 parser.addCommand('b');
163 parser.addCommand('c');
164 expect(parser.commands.keys, equals(['a', 'd', 'b', 'c']));
165 });
166 });
167
168 group('ArgParser.options', () {
169 test('returns an empty map if there are no options', () {
170 var parser = new ArgParser();
171 expect(parser.options, isEmpty);
172 });
173
174 test('returns the options that were added', () {
175 var parser = new ArgParser();
176 parser.addFlag('hide');
177 parser.addOption('seek');
178 expect(parser.options, hasLength(2));
179 expect(parser.options['hide'], isNotNull);
180 expect(parser.options['seek'], isNotNull);
181 });
182
183 test('iterates over the options in the order they were added', () {
184 var parser = new ArgParser();
185 parser.addFlag('a');
186 parser.addOption('d');
187 parser.addFlag('b');
188 parser.addOption('c');
189 expect(parser.options.keys, equals(['a', 'd', 'b', 'c']));
190 });
191 });
192
193 group('ArgResults', () {
194 group('options', () {
195 test('returns the provided options', () {
196 var parser = new ArgParser();
197 parser.addFlag('woof');
198 parser.addOption('meow');
199
200 parser.addOption('missing-option');
201 parser.addFlag('missing-flag', defaultsTo: null);
202
203 var args = parser.parse(['--woof', '--meow', 'kitty']);
204 expect(args.options, hasLength(2));
205 expect(args.options, contains('woof'));
206 expect(args.options, contains('meow'));
207 });
208
209 test('includes defaulted options', () {
210 var parser = new ArgParser();
211 parser.addFlag('woof', defaultsTo: false);
212 parser.addOption('meow', defaultsTo: 'kitty');
213
214 // Flags normally have a default value.
215 parser.addFlag('moo');
216
217 parser.addOption('missing-option');
218 parser.addFlag('missing-flag', defaultsTo: null);
219
220 var args = parser.parse([]);
221 expect(args.options, hasLength(3));
222 expect(args.options, contains('woof'));
223 expect(args.options, contains('meow'));
224 expect(args.options, contains('moo'));
225 });
226 });
227
228 test('[] throws if the name is not an option', () {
229 var results = new ArgParser().parse([]);
230 throwsIllegalArg(() => results['unknown']);
231 });
232
233 test('rest cannot be modified', () {
234 var results = new ArgParser().parse([]);
235 expect(() => results.rest.add('oops'), throwsUnsupportedError);
236 });
237
238 test('.arguments returns the original argument list', () {
239 var parser = new ArgParser();
240 parser.addFlag('foo');
241
242 var results = parser.parse(['--foo']);
243 expect(results.arguments, equals(['--foo']));
244 });
245
246 group('.wasParsed()', () {
247 test('throws if the name is not an option', () {
248 var results = new ArgParser().parse([]);
249 throwsIllegalArg(() => results.wasParsed('unknown'));
250 });
251
252 test('returns true for parsed options', () {
253 var parser = new ArgParser();
254 parser.addFlag('fast');
255 parser.addFlag('verbose');
256 parser.addOption('mode');
257 parser.addOption('output');
258
259 var results = parser.parse(['--fast', '--mode=debug']);
260
261 expect(results.wasParsed('fast'), isTrue);
262 expect(results.wasParsed('verbose'), isFalse);
263 expect(results.wasParsed('mode'), isTrue);
264 expect(results.wasParsed('output'), isFalse);
265 });
266 });
267 });
268
269 group('Option', () {
270 test('.getOrDefault() returns a type-specific default value', () {
271 var parser = new ArgParser();
272 parser.addFlag('flag-no', defaultsTo: null);
273 parser.addFlag('flag-def', defaultsTo: true);
274 parser.addOption('single-no');
275 parser.addOption('single-def', defaultsTo: 'def');
276 parser.addOption('multi-no', allowMultiple: true);
277 parser.addOption('multi-def', allowMultiple: true, defaultsTo: 'def');
278
279 expect(parser.options['flag-no'].getOrDefault(null), equals(null));
280 expect(parser.options['flag-no'].getOrDefault(false), equals(false));
281 expect(parser.options['flag-def'].getOrDefault(null), equals(true));
282 expect(parser.options['flag-def'].getOrDefault(false), equals(false));
283 expect(parser.options['single-no'].getOrDefault(null), equals(null));
284 expect(parser.options['single-no'].getOrDefault('v'), equals('v'));
285 expect(parser.options['single-def'].getOrDefault(null), equals('def'));
286 expect(parser.options['single-def'].getOrDefault('v'), equals('v'));
287 expect(parser.options['multi-no'].getOrDefault(null), equals([]));
288 expect(parser.options['multi-no'].getOrDefault(['v']), equals(['v']));
289 expect(parser.options['multi-def'].getOrDefault(null), equals(['def']));
290 expect(parser.options['multi-def'].getOrDefault(['v']), equals(['v']));
291 });
292 });
293 }
294
295 const _INVALID_OPTIONS = const [
296 ' ',
297 '',
298 '-',
299 '--',
300 '--foo',
301 ' with space',
302 'with\ttab',
303 'with\rcarriage\rreturn',
304 'with\nline\nfeed',
305 "'singlequotes'",
306 '"doublequotes"',
307 'back\\slash',
308 'forward/slash'
309 ];
310
311 const _VALID_OPTIONS = const [
312 'a', // One character.
313 'contains-dash',
314 'contains_underscore',
315 'ends-with-dash-',
316 'contains--doubledash--',
317 '1starts-with-number',
318 'contains-a-1number',
319 'ends-with-a-number8'
320 ];
OLDNEW
« no previous file with comments | « packages/args/pubspec.yaml ('k') | packages/args/test/command_parse_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698