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

Side by Side Diff: pkg/args/test/command_runner_test.dart

Issue 797473002: Add a CommandRunner class for dispatching commands to args. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Code review changes Created 6 years 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
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.
4
5 library command_runner_test;
6
7 import 'package:args/args.dart';
8 import 'package:args/command_runner.dart';
9 import 'package:unittest/unittest.dart';
10
11 import 'utils.dart';
12
13 const _DEFAULT_USAGE = """
14 Usage: test [command] <arguments>
15
16 Global options:
17 -h, --help Print this usage information.
18
19 Available commands:
20 help Display help information for test.
21
22 Run "test help [command]" for more information about a command.""";
23
24 void main() {
25 var runner;
26 setUp(() {
27 runner = new CommandRunner("test", "A test command runner.");
28 });
29
30 test(".usageTemplate has a sane default", () {
31 expect(runner.usageTemplate,
32 equals("test [command] <arguments>"));
33 });
34
35 group(".usage", () {
36 test("returns the usage string", () {
37 expect(runner.usage, equals("""
38 A test command runner.
39
40 $_DEFAULT_USAGE"""));
41 });
42
43 test("contains custom commands", () {
44 runner.addCommand(new FooCommand());
45
46 expect(runner.usage, equals("""
47 A test command runner.
48
49 Usage: test [command] <arguments>
50
51 Global options:
52 -h, --help Print this usage information.
53
54 Available commands:
55 foo Set a value.
56 help Display help information for test.
57
58 Run "test help [command]" for more information about a command."""));
59 });
60
61 test("contains custom options", () {
62 runner.argParser.addFlag("foo", help: "Do something.");
63
64 expect(runner.usage, equals("""
65 A test command runner.
66
67 Usage: test [command] <arguments>
68
69 Global options:
70 -h, --help Print this usage information.
71 --[no-]foo Do something.
72
73 Available commands:
74 help Display help information for test.
75
76 Run "test help [command]" for more information about a command."""));
77 });
78
79 test("doesn't print hidden commands", () {
80 runner.addCommand(new HiddenCommand());
81
82 expect(runner.usage, equals("""
83 A test command runner.
84
85 $_DEFAULT_USAGE"""));
86 });
87
88 test("doesn't print aliases", () {
89 runner.addCommand(new AliasedCommand());
90
91 expect(runner.usage, equals("""
92 A test command runner.
93
94 Usage: test [command] <arguments>
95
96 Global options:
97 -h, --help Print this usage information.
98
99 Available commands:
100 aliased Set a value.
101 help Display help information for test.
102
103 Run "test help [command]" for more information about a command."""));
104 });
105 });
106
107 test("usageError splits up the message and usage", () {
108 expect(() => runner.usageError("message"),
109 throwsUsageError("message", _DEFAULT_USAGE));
110 });
111
112 group("run()", () {
113 test("runs a command", () {
114 var command = new FooCommand();
115 runner.addCommand(command);
116
117 expect(runner.run(["foo"]).then((_) {
118 expect(command.hasRun, isTrue);
119 }), completes);
120 });
121
122 test("runs an asynchronous command", () {
123 var command = new AsyncCommand();
124 runner.addCommand(command);
125
126 expect(runner.run(["async"]).then((_) {
127 expect(command.hasRun, isTrue);
128 }), completes);
129 });
130
131 test("runs a hidden comand", () {
132 var command = new HiddenCommand();
133 runner.addCommand(command);
134
135 expect(runner.run(["hidden"]).then((_) {
136 expect(command.hasRun, isTrue);
137 }), completes);
138 });
139
140 test("runs an aliased comand", () {
141 var command = new AliasedCommand();
142 runner.addCommand(command);
143
144 expect(runner.run(["als"]).then((_) {
145 expect(command.hasRun, isTrue);
146 }), completes);
147 });
148
149 test("runs a subcommand", () {
150 var command = new AsyncCommand();
151 runner.addCommand(new FooCommand()..addSubcommand(command));
152
153 expect(runner.run(["foo", "async"]).then((_) {
154 expect(command.hasRun, isTrue);
155 }), completes);
156 });
157
158 group("with --help", () {
159 test("with no command prints the usage", () {
160 expect(() => runner.run(["--help"]), prints("""
161 A test command runner.
162
163 $_DEFAULT_USAGE
164 """));
165 });
166
167 test("with a command prints the usage for that command", () {
168 var command = new FooCommand();
169 runner.addCommand(command);
170
171 expect(() => runner.run(["foo", "--help"]), prints("""
172 Set a value.
173
174 Usage: test foo <arguments>
175 -h, --help Print this usage information.
176
177 Run "test help" to see global options.
178 """));
179 });
180 });
181 });
182
183 group("throws a useful error when", () {
184 test("arg parsing fails", () {
185 expect(runner.run(["--bad"]),
186 throwsUsageError('Could not find an option named "bad".',
187 _DEFAULT_USAGE));
188 });
189
190 test("a top-level command doesn't exist", () {
191 expect(runner.run(["bad"]),
192 throwsUsageError('Could not find a command named "bad".',
193 _DEFAULT_USAGE));
194 });
195
196 test("a subcommand doesn't exist", () {
197 runner.addCommand(
198 new FooCommand()..addSubcommand(new AsyncCommand()));
199
200 expect(runner.run(["foo bad"]),
201 throwsUsageError('Could not find a command named "foo bad".', """
202 Usage: test [command] <arguments>
203
204 Global options:
205 -h, --help Print this usage information.
206
207 Available commands:
208 foo Set a value.
209 help Display help information for test.
210
211 Run "test help [command]" for more information about a command."""));
212 });
213
214 test("a subcommand wasn't passed", () {
215 runner.addCommand(
216 new FooCommand()..addSubcommand(new AsyncCommand()));
217
218 expect(runner.run(["foo"]),
219 throwsUsageError('Missing subcommand for "test foo".', """
220 Usage: test foo [subcommand] <arguments>
221 -h, --help Print this usage information.
222
223 Available subcommands:
224 async Set a value asynchronously.
225
226 Run "test help" to see global options."""));
227 });
228
229 test("a command that doesn't take arguments was given them", () {
230 runner.addCommand(new FooCommand());
231
232 expect(runner.run(["foo", "bar"]),
233 throwsUsageError('Command "foo" does not take any arguments.', """
234 Usage: test foo <arguments>
235 -h, --help Print this usage information.
236
237 Run "test help" to see global options."""));
238 });
239 });
240 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698