OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library usage_test; | 5 library usage_test; |
6 | 6 |
7 import 'package:unittest/unittest.dart'; | 7 import 'package:unittest/unittest.dart'; |
8 import 'package:args/args.dart'; | 8 import 'package:args/args.dart'; |
9 | 9 |
10 void main() { | 10 void main() { |
11 group('ArgParser.usage', () { | 11 group('ArgParser.usage', () { |
12 test('negatable flags show "no-" in title', () { | 12 test('negatable flags show "no-" in title', () { |
13 var parser = new ArgParser(); | 13 var parser = new ArgParser(); |
14 parser.addFlag('mode', help: 'The mode'); | 14 parser.addFlag('mode', help: 'The mode'); |
15 | 15 |
16 validateUsage(parser, | 16 validateUsage(parser, ''' |
17 ''' | |
18 --[no-]mode The mode | 17 --[no-]mode The mode |
19 '''); | 18 '''); |
20 }); | 19 }); |
21 | 20 |
22 test('non-negatable flags don\'t show "no-" in title', () { | 21 test('non-negatable flags don\'t show "no-" in title', () { |
23 var parser = new ArgParser(); | 22 var parser = new ArgParser(); |
24 parser.addFlag('mode', negatable: false, help: 'The mode'); | 23 parser.addFlag('mode', negatable: false, help: 'The mode'); |
25 | 24 |
26 validateUsage(parser, | 25 validateUsage(parser, ''' |
27 ''' | |
28 --mode The mode | 26 --mode The mode |
29 '''); | 27 '''); |
30 }); | 28 }); |
31 | 29 |
32 test('if there are no abbreviations, there is no column for them', () { | 30 test('if there are no abbreviations, there is no column for them', () { |
33 var parser = new ArgParser(); | 31 var parser = new ArgParser(); |
34 parser.addFlag('mode', help: 'The mode'); | 32 parser.addFlag('mode', help: 'The mode'); |
35 | 33 |
36 validateUsage(parser, | 34 validateUsage(parser, ''' |
37 ''' | |
38 --[no-]mode The mode | 35 --[no-]mode The mode |
39 '''); | 36 '''); |
40 }); | 37 }); |
41 | 38 |
42 test('options are lined up past abbreviations', () { | 39 test('options are lined up past abbreviations', () { |
43 var parser = new ArgParser(); | 40 var parser = new ArgParser(); |
44 parser.addFlag('mode', abbr: 'm', help: 'The mode'); | 41 parser.addFlag('mode', abbr: 'm', help: 'The mode'); |
45 parser.addOption('long', help: 'Lacks an abbreviation'); | 42 parser.addOption('long', help: 'Lacks an abbreviation'); |
46 | 43 |
47 validateUsage(parser, | 44 validateUsage(parser, ''' |
48 ''' | |
49 -m, --[no-]mode The mode | 45 -m, --[no-]mode The mode |
50 --long Lacks an abbreviation | 46 --long Lacks an abbreviation |
51 '''); | 47 '''); |
52 }); | 48 }); |
53 | 49 |
54 test('help text is lined up past the longest option', () { | 50 test('help text is lined up past the longest option', () { |
55 var parser = new ArgParser(); | 51 var parser = new ArgParser(); |
56 parser.addFlag('mode', abbr: 'm', help: 'Lined up with below'); | 52 parser.addFlag('mode', abbr: 'm', help: 'Lined up with below'); |
57 parser.addOption('a-really-long-name', help: 'Its help text'); | 53 parser.addOption('a-really-long-name', help: 'Its help text'); |
58 | 54 |
59 validateUsage(parser, | 55 validateUsage(parser, ''' |
60 ''' | |
61 -m, --[no-]mode Lined up with below | 56 -m, --[no-]mode Lined up with below |
62 --a-really-long-name Its help text | 57 --a-really-long-name Its help text |
63 '''); | 58 '''); |
64 }); | 59 }); |
65 | 60 |
66 test('leading empty lines are ignored in help text', () { | 61 test('leading empty lines are ignored in help text', () { |
67 var parser = new ArgParser(); | 62 var parser = new ArgParser(); |
68 parser.addFlag('mode', help: '\n\n\n\nAfter newlines'); | 63 parser.addFlag('mode', help: '\n\n\n\nAfter newlines'); |
69 | 64 |
70 validateUsage(parser, | 65 validateUsage(parser, ''' |
71 ''' | |
72 --[no-]mode After newlines | 66 --[no-]mode After newlines |
73 '''); | 67 '''); |
74 }); | 68 }); |
75 | 69 |
76 test('trailing empty lines are ignored in help text', () { | 70 test('trailing empty lines are ignored in help text', () { |
77 var parser = new ArgParser(); | 71 var parser = new ArgParser(); |
78 parser.addFlag('mode', help: 'Before newlines\n\n\n\n'); | 72 parser.addFlag('mode', help: 'Before newlines\n\n\n\n'); |
79 | 73 |
80 validateUsage(parser, | 74 validateUsage(parser, ''' |
81 ''' | |
82 --[no-]mode Before newlines | 75 --[no-]mode Before newlines |
83 '''); | 76 '''); |
84 }); | 77 }); |
85 | 78 |
86 test('options are documented in the order they were added', () { | 79 test('options are documented in the order they were added', () { |
87 var parser = new ArgParser(); | 80 var parser = new ArgParser(); |
88 parser.addFlag('zebra', help: 'First'); | 81 parser.addFlag('zebra', help: 'First'); |
89 parser.addFlag('monkey', help: 'Second'); | 82 parser.addFlag('monkey', help: 'Second'); |
90 parser.addFlag('wombat', help: 'Third'); | 83 parser.addFlag('wombat', help: 'Third'); |
91 | 84 |
92 validateUsage(parser, | 85 validateUsage(parser, ''' |
93 ''' | |
94 --[no-]zebra First | 86 --[no-]zebra First |
95 --[no-]monkey Second | 87 --[no-]monkey Second |
96 --[no-]wombat Third | 88 --[no-]wombat Third |
97 '''); | 89 '''); |
98 }); | 90 }); |
99 | 91 |
100 test('the default value for a flag is shown if on', () { | 92 test('the default value for a flag is shown if on', () { |
101 var parser = new ArgParser(); | 93 var parser = new ArgParser(); |
102 parser.addFlag('affirm', help: 'Should be on', defaultsTo: true); | 94 parser.addFlag('affirm', help: 'Should be on', defaultsTo: true); |
103 parser.addFlag('negate', help: 'Should be off', defaultsTo: false); | 95 parser.addFlag('negate', help: 'Should be off', defaultsTo: false); |
104 | 96 |
105 validateUsage(parser, | 97 validateUsage(parser, ''' |
106 ''' | |
107 --[no-]affirm Should be on | 98 --[no-]affirm Should be on |
108 (defaults to on) | 99 (defaults to on) |
109 | 100 |
110 --[no-]negate Should be off | 101 --[no-]negate Should be off |
111 '''); | 102 '''); |
112 }); | 103 }); |
113 | 104 |
114 test('the default value for an option with no allowed list is shown', () { | 105 test('the default value for an option with no allowed list is shown', () { |
115 var parser = new ArgParser(); | 106 var parser = new ArgParser(); |
116 parser.addOption('any', help: 'Can be anything', defaultsTo: 'whatevs'); | 107 parser.addOption('any', help: 'Can be anything', defaultsTo: 'whatevs'); |
117 | 108 |
118 validateUsage(parser, | 109 validateUsage(parser, ''' |
119 ''' | |
120 --any Can be anything | 110 --any Can be anything |
121 (defaults to "whatevs") | 111 (defaults to "whatevs") |
122 '''); | 112 '''); |
123 }); | 113 }); |
124 | 114 |
125 test('the value help is shown', () { | 115 test('the value help is shown', () { |
126 var parser = new ArgParser(); | 116 var parser = new ArgParser(); |
127 parser.addOption('out', abbr: 'o', help: 'Where to write file', | 117 parser.addOption('out', |
128 valueHelp: 'path'); | 118 abbr: 'o', help: 'Where to write file', valueHelp: 'path'); |
129 | 119 |
130 validateUsage(parser, | 120 validateUsage(parser, ''' |
131 ''' | |
132 -o, --out=<path> Where to write file | 121 -o, --out=<path> Where to write file |
133 '''); | 122 '''); |
134 }); | 123 }); |
135 | 124 |
136 test('the allowed list is shown', () { | 125 test('the allowed list is shown', () { |
137 var parser = new ArgParser(); | 126 var parser = new ArgParser(); |
138 parser.addOption('suit', help: 'Like in cards', | 127 parser.addOption('suit', |
| 128 help: 'Like in cards', |
139 allowed: ['spades', 'clubs', 'hearts', 'diamonds']); | 129 allowed: ['spades', 'clubs', 'hearts', 'diamonds']); |
140 | 130 |
141 validateUsage(parser, | 131 validateUsage(parser, ''' |
142 ''' | |
143 --suit Like in cards | 132 --suit Like in cards |
144 [spades, clubs, hearts, diamonds] | 133 [spades, clubs, hearts, diamonds] |
145 '''); | 134 '''); |
146 }); | 135 }); |
147 | 136 |
148 test('the default is highlighted in the allowed list', () { | 137 test('the default is highlighted in the allowed list', () { |
149 var parser = new ArgParser(); | 138 var parser = new ArgParser(); |
150 parser.addOption('suit', help: 'Like in cards', defaultsTo: 'clubs', | 139 parser.addOption('suit', |
| 140 help: 'Like in cards', |
| 141 defaultsTo: 'clubs', |
151 allowed: ['spades', 'clubs', 'hearts', 'diamonds']); | 142 allowed: ['spades', 'clubs', 'hearts', 'diamonds']); |
152 | 143 |
153 validateUsage(parser, | 144 validateUsage(parser, ''' |
154 ''' | |
155 --suit Like in cards | 145 --suit Like in cards |
156 [spades, clubs (default), hearts, diamonds] | 146 [spades, clubs (default), hearts, diamonds] |
157 '''); | 147 '''); |
158 }); | 148 }); |
159 | 149 |
160 test('the allowed help is shown', () { | 150 test('the allowed help is shown', () { |
161 var parser = new ArgParser(); | 151 var parser = new ArgParser(); |
162 parser.addOption('suit', help: 'Like in cards', defaultsTo: 'clubs', | 152 parser.addOption('suit', |
| 153 help: 'Like in cards', |
| 154 defaultsTo: 'clubs', |
163 allowed: ['spades', 'clubs', 'diamonds', 'hearts'], | 155 allowed: ['spades', 'clubs', 'diamonds', 'hearts'], |
164 allowedHelp: { | 156 allowedHelp: { |
165 'spades': 'Swords of a soldier', | 157 'spades': 'Swords of a soldier', |
166 'clubs': 'Weapons of war', | 158 'clubs': 'Weapons of war', |
167 'diamonds': 'Money for this art', | 159 'diamonds': 'Money for this art', |
168 'hearts': 'The shape of my heart' | 160 'hearts': 'The shape of my heart' |
169 }); | 161 }); |
170 | 162 |
171 validateUsage(parser, | 163 validateUsage(parser, ''' |
172 ''' | |
173 --suit Like in cards | 164 --suit Like in cards |
174 | 165 |
175 [clubs] Weapons of war | 166 [clubs] Weapons of war |
176 [diamonds] Money for this art | 167 [diamonds] Money for this art |
177 [hearts] The shape of my heart | 168 [hearts] The shape of my heart |
178 [spades] Swords of a soldier | 169 [spades] Swords of a soldier |
179 '''); | 170 '''); |
180 }); | 171 }); |
181 | 172 |
182 test("hidden options don't appear in the help", () { | 173 test("hidden options don't appear in the help", () { |
183 var parser = new ArgParser(); | 174 var parser = new ArgParser(); |
184 parser.addOption('first', help: 'The first option'); | 175 parser.addOption('first', help: 'The first option'); |
185 parser.addOption('second', hide: true); | 176 parser.addOption('second', hide: true); |
186 parser.addOption('third', help: 'The third option'); | 177 parser.addOption('third', help: 'The third option'); |
187 | 178 |
188 | 179 validateUsage(parser, ''' |
189 validateUsage(parser, | |
190 ''' | |
191 --first The first option | 180 --first The first option |
192 --third The third option | 181 --third The third option |
193 '''); | 182 '''); |
194 }); | 183 }); |
195 | 184 |
196 test("hidden flags don't appear in the help", () { | 185 test("hidden flags don't appear in the help", () { |
197 var parser = new ArgParser(); | 186 var parser = new ArgParser(); |
198 parser.addFlag('first', help: 'The first flag'); | 187 parser.addFlag('first', help: 'The first flag'); |
199 parser.addFlag('second', hide: true); | 188 parser.addFlag('second', hide: true); |
200 parser.addFlag('third', help: 'The third flag'); | 189 parser.addFlag('third', help: 'The third flag'); |
201 | 190 |
202 | 191 validateUsage(parser, ''' |
203 validateUsage(parser, | |
204 ''' | |
205 --[no-]first The first flag | 192 --[no-]first The first flag |
206 --[no-]third The third flag | 193 --[no-]third The third flag |
207 '''); | 194 '''); |
208 }); | 195 }); |
209 | 196 |
210 test("hidden options don't affect spacing", () { | 197 test("hidden options don't affect spacing", () { |
211 var parser = new ArgParser(); | 198 var parser = new ArgParser(); |
212 parser.addFlag('first', help: 'The first flag'); | 199 parser.addFlag('first', help: 'The first flag'); |
213 parser.addFlag('second-very-long-option', hide: true); | 200 parser.addFlag('second-very-long-option', hide: true); |
214 parser.addFlag('third', help: 'The third flag'); | 201 parser.addFlag('third', help: 'The third flag'); |
215 | 202 |
216 | 203 validateUsage(parser, ''' |
217 validateUsage(parser, | |
218 ''' | |
219 --[no-]first The first flag | 204 --[no-]first The first flag |
220 --[no-]third The third flag | 205 --[no-]third The third flag |
221 '''); | 206 '''); |
222 }); | 207 }); |
223 }); | 208 }); |
224 } | 209 } |
225 | 210 |
226 void validateUsage(ArgParser parser, String expected) { | 211 void validateUsage(ArgParser parser, String expected) { |
227 expected = unindentString(expected); | 212 expected = unindentString(expected); |
228 expect(parser.usage, equals(expected)); | 213 expect(parser.usage, equals(expected)); |
(...skipping 26 matching lines...) Expand all Loading... |
255 throw new ArgumentError( | 240 throw new ArgumentError( |
256 'Line "$line" does not have enough indentation.'); | 241 'Line "$line" does not have enough indentation.'); |
257 } | 242 } |
258 | 243 |
259 lines[i] = line.substring(indent); | 244 lines[i] = line.substring(indent); |
260 } | 245 } |
261 } | 246 } |
262 | 247 |
263 return lines.join('\n'); | 248 return lines.join('\n'); |
264 } | 249 } |
OLD | NEW |