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

Unified Diff: pkg/args/test/args_test.dart

Issue 12472019: pkg/args Option should be more strict about names and abbreviations (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 9 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 side-by-side diff with in-line comments
Download patch
« pkg/args/lib/args.dart ('K') | « pkg/args/lib/args.dart ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/args/test/args_test.dart
diff --git a/pkg/args/test/args_test.dart b/pkg/args/test/args_test.dart
index d807291e04d840e5e3f36f85463462deba04e1b9..4912c5d25bd63ce1da13c3d8e58507beb8a17d89 100644
--- a/pkg/args/test/args_test.dart
+++ b/pkg/args/test/args_test.dart
@@ -32,6 +32,24 @@ main() {
var parser = new ArgParser();
throwsIllegalArg(() => parser.addFlag('flummox', abbr: 'flu'));
});
+
+ test('throws ArgumentError if a flag name is invalid', () {
+ var parser = new ArgParser();
+
+ for(final name in _invalidOptions) {
Bob Nystrom 2013/03/13 18:07:09 To be consistent, we don't use final for local var
kevmoo-old 2013/03/13 19:30:48 `final` is so awesome. I'd love someone to start a
Bob Nystrom 2013/03/13 21:29:57 If we had "val" I would be all about it. In the ab
+ final reason = '${Error.safeToString(name)} is not valid';
+ throwsIllegalArg(() => parser.addFlag(name), reason: reason);
+ }
+ });
+
+ test('accepts valid flag names', () {
+ var parser = new ArgParser();
+
+ for(final name in _validOptions) {
+ final reason = '${Error.safeToString(name)} is valid';
+ expect(() => parser.addFlag(name), returnsNormally, reason: reason);
+ }
+ });
});
group('ArgParser.addOption()', () {
@@ -58,6 +76,46 @@ main() {
var parser = new ArgParser();
throwsIllegalArg(() => parser.addOption('flummox', abbr: 'flu'));
});
+
+ test('throws ArgumentError if the abbreviation is empty', () {
+ var parser = new ArgParser();
+ throwsIllegalArg(() => parser.addOption('flummox', abbr: ''));
+ });
+
+ test('throws ArgumentError if the abbreviation is an invalid value', () {
+ var parser = new ArgParser();
+ for(final name in _invalidOptions.where((v) => v != null)) {
+ throwsIllegalArg(() => parser.addOption('flummox', abbr: name));
+ }
+ });
+
+ test('throws ArgumentError if the abbreviation is a dash', () {
+ var parser = new ArgParser();
+ throwsIllegalArg(() => parser.addOption('flummox', abbr: '-'));
+ });
+
+ test('allows explict null value for "abbr"', () {
+ var parser = new ArgParser();
+ expect(() => parser.addOption('flummox', abbr: null), returnsNormally);
+ });
+
+ test('throws ArgumentError if an option name is invalid', () {
+ var parser = new ArgParser();
+
+ for(final name in _invalidOptions) {
+ final reason = '${Error.safeToString(name)} is not valid';
+ throwsIllegalArg(() => parser.addOption(name), reason: reason);
+ }
+ });
+
+ test('accepts valid option names', () {
+ var parser = new ArgParser();
+
+ for(final name in _validOptions) {
+ final reason = '${Error.safeToString(name)} is valid';
+ expect(() => parser.addOption(name), returnsNormally, reason: reason);
+ }
+ });
});
group('ArgParser.getDefault()', () {
@@ -155,10 +213,43 @@ main() {
});
}
-throwsIllegalArg(function) {
- expect(function, throwsArgumentError);
+throwsIllegalArg(function, {String reason: null}) {
+ expect(function, throwsArgumentError, reason: reason);
}
throwsFormat(ArgParser parser, List<String> args) {
expect(() => parser.parse(args), throwsFormatException);
}
+
+Iterable<String> get _invalidOptions {
Bob Nystrom 2013/03/13 18:07:09 This may be a big gratuitous. :) How about just a
kevmoo-old 2013/03/13 19:30:48 I take testing seriously.
Bob Nystrom 2013/03/13 21:29:57 Testing is serious business. SGTM.
+ final options = [null, '', '-', '--', '--foo'];
+
+ const invalidChars = const [' ', '\t', '\r', '\n', '"', '"', r'\', r'/'];
+
+ for(final invalidChar in invalidChars) {
+ // solo
+ options.add(invalidChar);
+
+ // before
+ options.add('${invalidChar}foo');
+
+ // contains
+ options.add('foo${invalidChar}bar');
+
+ // ends with
+ options.add('foo${invalidChar}');
+ }
+
+ return options;
+}
+
+const _validOptions = const
+[
+ 'a' // one char
+ 'contains-dash',
+ 'contains_underscore',
+ 'ends-with-dash-',
+ 'contains--doubledash--',
+ '1starts-with-number',
+ 'contains-a-1number',
+ 'ends-with-a-number8'];
« pkg/args/lib/args.dart ('K') | « pkg/args/lib/args.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698