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 | 10 |
10 main() { | 11 void main() { |
11 group('ArgParser.addFlag()', () { | 12 group('ArgParser.addFlag()', () { |
12 test('throws ArgumentError if the flag already exists', () { | 13 test('throws ArgumentError if the flag already exists', () { |
13 var parser = new ArgParser(); | 14 var parser = new ArgParser(); |
14 parser.addFlag('foo'); | 15 parser.addFlag('foo'); |
15 throwsIllegalArg(() => parser.addFlag('foo')); | 16 throwsIllegalArg(() => parser.addFlag('foo')); |
16 }); | 17 }); |
17 | 18 |
18 test('throws ArgumentError if the option already exists', () { | 19 test('throws ArgumentError if the option already exists', () { |
19 var parser = new ArgParser(); | 20 var parser = new ArgParser(); |
20 parser.addOption('foo'); | 21 parser.addOption('foo'); |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 test('iterates over the options in the order they were added', () { | 176 test('iterates over the options in the order they were added', () { |
176 var parser = new ArgParser(); | 177 var parser = new ArgParser(); |
177 parser.addFlag('a'); | 178 parser.addFlag('a'); |
178 parser.addOption('d'); | 179 parser.addOption('d'); |
179 parser.addFlag('b'); | 180 parser.addFlag('b'); |
180 parser.addOption('c'); | 181 parser.addOption('c'); |
181 expect(parser.options.keys, equals(['a', 'd', 'b', 'c'])); | 182 expect(parser.options.keys, equals(['a', 'd', 'b', 'c'])); |
182 }); | 183 }); |
183 }); | 184 }); |
184 | 185 |
185 group('ArgResults.options', () { | 186 group('ArgResults', () { |
186 test('returns the provided options', () { | 187 group('options', () { |
187 var parser = new ArgParser(); | 188 test('returns the provided options', () { |
188 parser.addFlag('woof'); | 189 var parser = new ArgParser(); |
189 parser.addOption('meow'); | 190 parser.addFlag('woof'); |
190 var args = parser.parse(['--woof', '--meow', 'kitty']); | 191 parser.addOption('meow'); |
191 expect(args.options, hasLength(2)); | 192 var args = parser.parse(['--woof', '--meow', 'kitty']); |
192 expect(args.options.any((o) => o == 'woof'), isTrue); | 193 expect(args.options, hasLength(2)); |
193 expect(args.options.any((o) => o == 'meow'), isTrue); | 194 expect(args.options, contains('woof')); |
| 195 expect(args.options, contains('meow')); |
| 196 }); |
| 197 |
| 198 test('includes defaulted options', () { |
| 199 var parser = new ArgParser(); |
| 200 parser.addFlag('woof', defaultsTo: false); |
| 201 parser.addOption('meow', defaultsTo: 'kitty'); |
| 202 var args = parser.parse([]); |
| 203 expect(args.options, hasLength(2)); |
| 204 expect(args.options, contains('woof')); |
| 205 expect(args.options, contains('meow')); |
| 206 }); |
194 }); | 207 }); |
195 | 208 |
196 test('includes defaulted options', () { | 209 test('[] throws if the name is not an option', () { |
197 var parser = new ArgParser(); | |
198 parser.addFlag('woof', defaultsTo: false); | |
199 parser.addOption('meow', defaultsTo: 'kitty'); | |
200 var args = parser.parse([]); | |
201 expect(args.options, hasLength(2)); | |
202 expect(args.options.any((o) => o == 'woof'), isTrue); | |
203 expect(args.options.any((o) => o == 'meow'), isTrue); | |
204 }); | |
205 }); | |
206 | |
207 group('ArgResults[]', () { | |
208 test('throws if the name is not an option', () { | |
209 var parser = new ArgParser(); | 210 var parser = new ArgParser(); |
210 var results = parser.parse([]); | 211 var results = parser.parse([]); |
211 throwsIllegalArg(() => results['unknown']); | 212 throwsIllegalArg(() => results['unknown']); |
212 }); | 213 }); |
| 214 |
| 215 test('rest cannot be modified', () { |
| 216 var results = new ArgResults({}, '', null, []); |
| 217 expect(() => results.rest.add('oops'), throwsUnsupportedError); |
| 218 }); |
213 }); | 219 }); |
214 } | 220 } |
215 | 221 |
216 throwsIllegalArg(function, {String reason: null}) { | |
217 expect(function, throwsArgumentError, reason: reason); | |
218 } | |
219 | |
220 throwsFormat(ArgParser parser, List<String> args) { | |
221 expect(() => parser.parse(args), throwsFormatException); | |
222 } | |
223 | |
224 const _INVALID_OPTIONS = const [ | 222 const _INVALID_OPTIONS = const [ |
225 ' ', '', '-', '--', '--foo', | 223 ' ', '', '-', '--', '--foo', |
226 ' with space', | 224 ' with space', |
227 'with\ttab', | 225 'with\ttab', |
228 'with\rcarriage\rreturn', | 226 'with\rcarriage\rreturn', |
229 'with\nline\nfeed', | 227 'with\nline\nfeed', |
230 "'singlequotes'", | 228 "'singlequotes'", |
231 '"doublequotes"', | 229 '"doublequotes"', |
232 'back\\slash', | 230 'back\\slash', |
233 'forward/slash' | 231 'forward/slash' |
234 ]; | 232 ]; |
235 | 233 |
236 const _VALID_OPTIONS = const [ | 234 const _VALID_OPTIONS = const [ |
237 'a' // one char | 235 'a' // one char |
238 'contains-dash', | 236 'contains-dash', |
239 'contains_underscore', | 237 'contains_underscore', |
240 'ends-with-dash-', | 238 'ends-with-dash-', |
241 'contains--doubledash--', | 239 'contains--doubledash--', |
242 '1starts-with-number', | 240 '1starts-with-number', |
243 'contains-a-1number', | 241 'contains-a-1number', |
244 'ends-with-a-number8' | 242 'ends-with-a-number8' |
245 ]; | 243 ]; |
OLD | NEW |