Index: pkg/args/test/parse_test.dart |
diff --git a/pkg/args/test/args_test.dart b/pkg/args/test/parse_test.dart |
similarity index 59% |
copy from pkg/args/test/args_test.dart |
copy to pkg/args/test/parse_test.dart |
index 6feb1dcec267a62244ca29e1e6d7977d85909a87..ab1d1306f8db81e5749ce59be81f6d7a7b2a9ff9 100644 |
--- a/pkg/args/test/args_test.dart |
+++ b/pkg/args/test/parse_test.dart |
@@ -8,58 +8,6 @@ import 'package:unittest/unittest.dart'; |
import 'package:args/args.dart'; |
main() { |
- group('ArgParser.addFlag()', () { |
- test('throws ArgumentError if the flag already exists', () { |
- var parser = new ArgParser(); |
- parser.addFlag('foo'); |
- throwsIllegalArg(() => parser.addFlag('foo')); |
- }); |
- |
- test('throws ArgumentError if the option already exists', () { |
- var parser = new ArgParser(); |
- parser.addOption('foo'); |
- throwsIllegalArg(() => parser.addFlag('foo')); |
- }); |
- |
- test('throws ArgumentError if the abbreviation exists', () { |
- var parser = new ArgParser(); |
- parser.addFlag('foo', abbr: 'f'); |
- throwsIllegalArg(() => parser.addFlag('flummox', abbr: 'f')); |
- }); |
- |
- test('throws ArgumentError if the abbreviation is longer ' |
- 'than one character', () { |
- var parser = new ArgParser(); |
- throwsIllegalArg(() => parser.addFlag('flummox', abbr: 'flu')); |
- }); |
- }); |
- |
- group('ArgParser.addOption()', () { |
- test('throws ArgumentError if the flag already exists', () { |
- var parser = new ArgParser(); |
- parser.addFlag('foo'); |
- throwsIllegalArg(() => parser.addOption('foo')); |
- }); |
- |
- test('throws ArgumentError if the option already exists', () { |
- var parser = new ArgParser(); |
- parser.addOption('foo'); |
- throwsIllegalArg(() => parser.addOption('foo')); |
- }); |
- |
- test('throws ArgumentError if the abbreviation exists', () { |
- var parser = new ArgParser(); |
- parser.addFlag('foo', abbr: 'f'); |
- throwsIllegalArg(() => parser.addOption('flummox', abbr: 'f')); |
- }); |
- |
- test('throws ArgumentError if the abbreviation is longer ' |
- 'than one character', () { |
- var parser = new ArgParser(); |
- throwsIllegalArg(() => parser.addOption('flummox', abbr: 'flu')); |
- }); |
- }); |
- |
group('ArgParser.parse()', () { |
group('flags', () { |
test('are true if present', () { |
@@ -427,34 +375,6 @@ main() { |
}); |
}); |
- group('query default values', () { |
- test('queries the default value', () { |
- var parser = new ArgParser(); |
- parser.addOption('define', defaultsTo: '0'); |
- expect(()=>parser.getDefault('undefine'), |
- throwsArgumentError); |
- }); |
- |
- test('queries the default value for an unknown option', () { |
- var parser = new ArgParser(); |
- parser.addOption('define', defaultsTo: '0'); |
- expect(()=>parser.getDefault('undefine'), |
- throwsArgumentError); |
- }); |
- }); |
- |
- group('gets the option names from an ArgsResult', () { |
- test('queries the set options', () { |
- var parser = new ArgParser(); |
- parser.addFlag('woof', defaultsTo: false); |
- parser.addOption('meow', defaultsTo: 'kitty'); |
- var args = parser.parse([]); |
- expect(args.options, hasLength(2)); |
- expect(args.options.any((o) => o == 'woof'), isTrue); |
- expect(args.options.any((o) => o == 'meow'), isTrue); |
- }); |
- }); |
- |
group('remaining args', () { |
test('stops parsing args when a non-option-like arg is encountered', () { |
var parser = new ArgParser(); |
@@ -490,175 +410,6 @@ main() { |
}); |
}); |
}); |
- |
- group('ArgParser.getUsage()', () { |
- test('negatable flags show "no-" in title', () { |
- var parser = new ArgParser(); |
- parser.addFlag('mode', help: 'The mode'); |
- |
- validateUsage(parser, |
- ''' |
- --[no-]mode The mode |
- '''); |
- }); |
- |
- test('non-negatable flags don\'t show "no-" in title', () { |
- var parser = new ArgParser(); |
- parser.addFlag('mode', negatable: false, help: 'The mode'); |
- |
- validateUsage(parser, |
- ''' |
- --mode The mode |
- '''); |
- }); |
- |
- test('if there are no abbreviations, there is no column for them', () { |
- var parser = new ArgParser(); |
- parser.addFlag('mode', help: 'The mode'); |
- |
- validateUsage(parser, |
- ''' |
- --[no-]mode The mode |
- '''); |
- }); |
- |
- test('options are lined up past abbreviations', () { |
- var parser = new ArgParser(); |
- parser.addFlag('mode', abbr: 'm', help: 'The mode'); |
- parser.addOption('long', help: 'Lacks an abbreviation'); |
- |
- validateUsage(parser, |
- ''' |
- -m, --[no-]mode The mode |
- --long Lacks an abbreviation |
- '''); |
- }); |
- |
- test('help text is lined up past the longest option', () { |
- var parser = new ArgParser(); |
- parser.addFlag('mode', abbr: 'm', help: 'Lined up with below'); |
- parser.addOption('a-really-long-name', help: 'Its help text'); |
- |
- validateUsage(parser, |
- ''' |
- -m, --[no-]mode Lined up with below |
- --a-really-long-name Its help text |
- '''); |
- }); |
- |
- test('leading empty lines are ignored in help text', () { |
- var parser = new ArgParser(); |
- parser.addFlag('mode', help: '\n\n\n\nAfter newlines'); |
- |
- validateUsage(parser, |
- ''' |
- --[no-]mode After newlines |
- '''); |
- }); |
- |
- test('trailing empty lines are ignored in help text', () { |
- var parser = new ArgParser(); |
- parser.addFlag('mode', help: 'Before newlines\n\n\n\n'); |
- |
- validateUsage(parser, |
- ''' |
- --[no-]mode Before newlines |
- '''); |
- }); |
- |
- test('options are documented in the order they were added', () { |
- var parser = new ArgParser(); |
- parser.addFlag('zebra', help: 'First'); |
- parser.addFlag('monkey', help: 'Second'); |
- parser.addFlag('wombat', help: 'Third'); |
- |
- validateUsage(parser, |
- ''' |
- --[no-]zebra First |
- --[no-]monkey Second |
- --[no-]wombat Third |
- '''); |
- }); |
- |
- test('the default value for a flag is shown if on', () { |
- var parser = new ArgParser(); |
- parser.addFlag('affirm', help: 'Should be on', defaultsTo: true); |
- parser.addFlag('negate', help: 'Should be off', defaultsTo: false); |
- |
- validateUsage(parser, |
- ''' |
- --[no-]affirm Should be on |
- (defaults to on) |
- |
- --[no-]negate Should be off |
- '''); |
- }); |
- |
- test('the default value for an option with no allowed list is shown', () { |
- var parser = new ArgParser(); |
- parser.addOption('any', help: 'Can be anything', defaultsTo: 'whatevs'); |
- |
- validateUsage(parser, |
- ''' |
- --any Can be anything |
- (defaults to "whatevs") |
- '''); |
- }); |
- |
- test('the allowed list is shown', () { |
- var parser = new ArgParser(); |
- parser.addOption('suit', help: 'Like in cards', |
- allowed: ['spades', 'clubs', 'hearts', 'diamonds']); |
- |
- validateUsage(parser, |
- ''' |
- --suit Like in cards |
- [spades, clubs, hearts, diamonds] |
- '''); |
- }); |
- |
- test('the default is highlighted in the allowed list', () { |
- var parser = new ArgParser(); |
- parser.addOption('suit', help: 'Like in cards', defaultsTo: 'clubs', |
- allowed: ['spades', 'clubs', 'hearts', 'diamonds']); |
- |
- validateUsage(parser, |
- ''' |
- --suit Like in cards |
- [spades, clubs (default), hearts, diamonds] |
- '''); |
- }); |
- |
- test('the allowed help is shown', () { |
- var parser = new ArgParser(); |
- parser.addOption('suit', help: 'Like in cards', defaultsTo: 'clubs', |
- allowed: ['spades', 'clubs', 'diamonds', 'hearts'], |
- allowedHelp: { |
- 'spades': 'Swords of a soldier', |
- 'clubs': 'Weapons of war', |
- 'diamonds': 'Money for this art', |
- 'hearts': 'The shape of my heart' |
- }); |
- |
- validateUsage(parser, |
- ''' |
- --suit Like in cards |
- |
- [clubs] Weapons of war |
- [diamonds] Money for this art |
- [hearts] The shape of my heart |
- [spades] Swords of a soldier |
- '''); |
- }); |
- }); |
- |
- group('ArgResults[]', () { |
- test('throws if the name is not an option', () { |
- var parser = new ArgParser(); |
- var results = parser.parse([]); |
- throwsIllegalArg(() => results['unknown']); |
- }); |
- }); |
} |
throwsIllegalArg(function) { |
@@ -668,43 +419,3 @@ throwsIllegalArg(function) { |
throwsFormat(ArgParser parser, List<String> args) { |
expect(() => parser.parse(args), throwsFormatException); |
} |
- |
-validateUsage(ArgParser parser, String expected) { |
- expected = unindentString(expected); |
- expect(parser.getUsage(), equals(expected)); |
-} |
- |
-// TODO(rnystrom): Replace one in test_utils. |
-String unindentString(String text) { |
- var lines = text.split('\n'); |
- |
- // Count the indentation of the last line. |
- var whitespace = new RegExp('^ *'); |
- var indent = whitespace.firstMatch(lines[lines.length - 1])[0].length; |
- |
- // Drop the last line. It only exists for specifying indentation. |
- lines.removeLast(); |
- |
- // Strip indentation from the remaining lines. |
- for (var i = 0; i < lines.length; i++) { |
- var line = lines[i]; |
- if (line.length <= indent) { |
- // It's short, so it must be nothing but whitespace. |
- if (line.trim() != '') { |
- throw new ArgumentError( |
- 'Line "$line" does not have enough indentation.'); |
- } |
- |
- lines[i] = ''; |
- } else { |
- if (line.substring(0, indent).trim() != '') { |
- throw new ArgumentError( |
- 'Line "$line" does not have enough indentation.'); |
- } |
- |
- lines[i] = line.substring(indent); |
- } |
- } |
- |
- return Strings.join(lines, '\n'); |
-} |