Skip to content
This repository has been archived by the owner on Oct 17, 2024. It is now read-only.

Commit

Permalink
add test and cleanup test
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 7550d12 commit fd5e803
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 20 deletions.
6 changes: 3 additions & 3 deletions test/command_parse_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,15 @@ void main() {
var command = parser.addCommand('install');
command.addOption('path', abbr: 'p');

throwsArgParserException(parser, ['-p', 'foo', 'install'], '-p');
throwsFormat(parser, ['-p', 'foo', 'install']);
});

test('does not parse a command option before the command', () {
var parser = ArgParser();
var command = parser.addCommand('install');
command.addOption('path');

throwsArgParserException(parser, ['--path', 'foo', 'install'], '--path');
throwsFormat(parser, ['--path', 'foo', 'install']);
});

test('does not parse a command abbreviation before the command', () {
Expand All @@ -156,7 +156,7 @@ void main() {
command.addFlag('debug', abbr: 'd');
command.addFlag('verbose', abbr: 'v');

throwsArgParserException(parser, ['-dv', 'install'], '-d');
throwsFormat(parser, ['-dv', 'install']);
});

test('assigns collapsed options to the proper command', () {
Expand Down
74 changes: 61 additions & 13 deletions test/parse_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void main() {
var parser = ArgParser();
parser.addFlag('verbose');

throwsArgParserException(parser, ['--verbose=true'], '--verbose');
throwsFormat(parser, ['--verbose=true']);
});

test('are case-sensitive', () {
Expand Down Expand Up @@ -189,7 +189,7 @@ void main() {
var parser = ArgParser();
parser.addFlag('strum', negatable: false);

throwsArgParserException(parser, ['--no-strum'], '--no-strum');
throwsFormat(parser, ['--no-strum']);
});
});

Expand Down Expand Up @@ -399,14 +399,14 @@ void main() {

test('throw if unknown', () {
var parser = ArgParser();
throwsArgParserException(parser, ['-f'], '-f');
throwsFormat(parser, ['-f']);
});

test('throw if the value is missing', () {
var parser = ArgParser();
parser.addOption('file', abbr: 'f');

throwsArgParserException(parser, ['-f'], '-f');
throwsFormat(parser, ['-f']);
});

test('does not throw if the value looks like an option', () {
Expand All @@ -424,7 +424,7 @@ void main() {
var parser = ArgParser();
parser.addOption('mode', abbr: 'm', allowed: ['debug', 'release']);

throwsArgParserException(parser, ['-mprofile'], '-m');
throwsFormat(parser, ['-mprofile']);
});

group('throw if a comma-separated value is not allowed', () {
Expand All @@ -433,7 +433,7 @@ void main() {
parser
.addMultiOption('mode', abbr: 'm', allowed: ['debug', 'release']);

throwsArgParserException(parser, ['-mdebug,profile'], '-m');
throwsFormat(parser, ['-mdebug,profile']);
});
});

Expand All @@ -443,7 +443,7 @@ void main() {
parser.addOption('banana', abbr: 'b'); // Takes an argument.
parser.addFlag('cherry', abbr: 'c');

throwsArgParserException(parser, ['-abc'], '-b');
throwsFormat(parser, ['-abc']);
});

test('throw if it has a value but the option is a flag', () {
Expand All @@ -452,7 +452,7 @@ void main() {
parser.addFlag('banana', abbr: 'b');

// The '?!' means this can only be understood as '--apple b?!c'.
throwsArgParserException(parser, ['-ab?!c'], '-a');
throwsFormat(parser, ['-ab?!c']);
});

test('are case-sensitive', () {
Expand Down Expand Up @@ -496,15 +496,14 @@ void main() {

test('throw if unknown', () {
var parser = ArgParser();
throwsArgParserException(parser, ['--unknown'], '--unknown');
throwsArgParserException(
parser, ['--nobody'], '--nobody'); // Starts with "no".
throwsFormat(parser, ['--unknown']);
throwsFormat(parser, ['--nobody']); // Starts with "no".
});

test('throw if the arg does not include a value', () {
var parser = ArgParser();
parser.addOption('mode');
throwsArgParserException(parser, ['--mode'], '--mode');
throwsFormat(parser, ['--mode']);
});

test('do not throw if the value looks like an option', () {
Expand Down Expand Up @@ -539,7 +538,7 @@ void main() {
test('throw if the value is not in the allowed set', () {
var parser = ArgParser();
parser.addOption('mode', allowed: ['debug', 'release']);
throwsArgParserException(parser, ['--mode=profile'], '--mode');
throwsFormat(parser, ['--mode=profile']);
});

test('returns last provided value', () {
Expand Down Expand Up @@ -767,5 +766,54 @@ void main() {
expect(results.rest, equals(['stop', '--', 'arg']));
});
});

group('ArgParser Exception Tests', () {
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');
});

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, ['-f=1'],
'Option "-f" is a flag and cannot handle value "=1".', [], '-f');
});

test('throws exception after parsing multiple options', () {
var parser = ArgParser();
parser.addOption('first');
parser.addOption('second');
throwsArgParserException(
parser,
['--first', '1', '--second', '2', '--verbose', '3'],
'Could not find an option named "--verbose".',
[],
'--verbose');
});

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');
});

test('throws exception after parsing command', () {
var parser = ArgParser();
parser.addCommand('command', ArgParser());
throwsArgParserException(
parser,
['command', '--verbose'],
'Could not find an option named "--verbose".',
['command'],
'--verbose');
});
});
});
}
11 changes: 9 additions & 2 deletions test/test_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -343,12 +343,19 @@ void throwsIllegalArg(void Function() function, {String? reason}) {
expect(function, throwsArgumentError, reason: reason);
}

void throwsArgParserException(ArgParser parser, List<String> args, String? arg,
{String? reason}) {
void throwsFormat(ArgParser parser, List<String> args, {String? reason}) {
expect(() => parser.parse(args), throwsA(isA<FormatException>()),
reason: reason);
}

void throwsArgParserException(ArgParser parser, List<String> args,
String message, List<String> commands, String arg) {
try {
parser.parse(args);
fail('Expected an ArgParserException');
} on ArgParserException catch (e) {
expect(e.message, message);
expect(e.commands, commands);
expect(e.arg, arg);
} catch (e) {
fail('Expected an ArgParserException, but got $e');
Expand Down
3 changes: 1 addition & 2 deletions test/trailing_options_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ void main() {
});

void expectThrows(List<String> args, String arg) {
throwsArgParserException(parser, args, arg,
reason: 'with allowTrailingOptions: true');
throwsFormat(parser, args, reason: 'with allowTrailingOptions: true');
}

test('collects non-options in rest', () {
Expand Down

0 comments on commit fd5e803

Please sign in to comment.