Skip to content

Commit

Permalink
update ArgParser Exception Tests
Browse files Browse the repository at this point in the history
Signed-off-by: ふぁ <yuki@yuki0311.com>
  • Loading branch information
fa0311 committed Sep 25, 2024
1 parent 7d630b5 commit 0c7568c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
4 changes: 3 additions & 1 deletion lib/src/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -315,17 +315,19 @@ class Parser {
bool _handleLongOption(String name, String? value) {
var option = _grammar.findByNameOrAlias(name);
if (option != null) {
_args.removeFirst();
if (option.isFlag) {
_validate(value == null,
'Flag option "--$name" should not be given a value.', '--$name');

_args.removeFirst();
_setFlag(_results, option, true);
} else if (value != null) {
// We have a value like --foo=bar.
_args.removeFirst();
_setOption(_results, option, value, '--$name');
} else {
// Option like --foo, so look for the value as the next arg.
_args.removeFirst();
_readNextArgAsValue(option, '--$name');
}
} else if (name.startsWith('no-')) {
Expand Down
32 changes: 22 additions & 10 deletions test/parse_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -771,18 +771,23 @@ void main() {
test('throws exception for unknown option', () {
var parser = ArgParser();
throwsArgParserException(parser, ['--verbose'],
'Could not find an option named "--verbose".', [], '--verbose');
throwsArgParserException(
parser, ['-v'], 'Could not find an option or flag "-v".', [], '-v');
'Could not find an option named "--verbose".', [], '--verbose', 0);
throwsArgParserException(parser, ['-v'],
'Could not find an option or flag "-v".', [], '-v', 0);
});

test('throws exception for flag with value', () {
var parser = ArgParser();
parser.addFlag('flag', abbr: 'f');
throwsArgParserException(parser, ['--flag=1'],
'Flag option "--flag" should not be given a value.', [], '--flag');
throwsArgParserException(
parser,
['--flag=1'],
'Flag option "--flag" should not be given a value.',
[],
'--flag',
0);
throwsArgParserException(parser, ['-f=1'],
'Option "-f" is a flag and cannot handle value "=1".', [], '-f');
'Option "-f" is a flag and cannot handle value "=1".', [], '-f', 0);
});

test('throws exception after parsing multiple options', () {
Expand All @@ -794,14 +799,20 @@ void main() {
['--first', '1', '--second', '2', '--verbose', '3'],
'Could not find an option named "--verbose".',
[],
'--verbose');
'--verbose',
4);
});

test('throws exception for option with invalid value', () {
var parser = ArgParser();
parser.addOption('first', allowed: ['a', 'b']);
throwsArgParserException(parser, ['--first', 'c'],
'"c" is not an allowed value for option "--first".', [], '--first');
throwsArgParserException(
parser,
['--first', 'c'],
'"c" is not an allowed value for option "--first".',
[],
'--first',
1);
});

test('throws exception after parsing command', () {
Expand All @@ -812,7 +823,8 @@ void main() {
['command', '--verbose'],
'Could not find an option named "--verbose".',
['command'],
'--verbose');
'--verbose',
1);
});
});
});
Expand Down
4 changes: 3 additions & 1 deletion test/test_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -349,14 +349,16 @@ void throwsFormat(ArgParser parser, List<String> args, {String? reason}) {
}

void throwsArgParserException(ArgParser parser, List<String> args,
String message, List<String> commands, String arg) {
String message, List<String> commands, String arg, int offset) {
try {
parser.parse(args);
fail('Expected an ArgParserException');
} on ArgParserException catch (e) {
expect(e.message, message);
expect(e.commands, commands);
expect(e.arg, arg);
expect(e.source, args);
expect(e.offset, offset);
} catch (e) {
fail('Expected an ArgParserException, but got $e');
}
Expand Down

0 comments on commit 0c7568c

Please sign in to comment.