OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library usage_test; |
| 6 |
| 7 import 'package:test/test.dart'; |
| 8 import 'package:args/args.dart'; |
| 9 |
| 10 void main() { |
| 11 group('ArgParser.usage', () { |
| 12 test('negatable flags show "no-" in title', () { |
| 13 var parser = new ArgParser(); |
| 14 parser.addFlag('mode', help: 'The mode'); |
| 15 |
| 16 validateUsage(parser, ''' |
| 17 --[no-]mode The mode |
| 18 '''); |
| 19 }); |
| 20 |
| 21 test('non-negatable flags don\'t show "no-" in title', () { |
| 22 var parser = new ArgParser(); |
| 23 parser.addFlag('mode', negatable: false, help: 'The mode'); |
| 24 |
| 25 validateUsage(parser, ''' |
| 26 --mode The mode |
| 27 '''); |
| 28 }); |
| 29 |
| 30 test('if there are no abbreviations, there is no column for them', () { |
| 31 var parser = new ArgParser(); |
| 32 parser.addFlag('mode', help: 'The mode'); |
| 33 |
| 34 validateUsage(parser, ''' |
| 35 --[no-]mode The mode |
| 36 '''); |
| 37 }); |
| 38 |
| 39 test('options are lined up past abbreviations', () { |
| 40 var parser = new ArgParser(); |
| 41 parser.addFlag('mode', abbr: 'm', help: 'The mode'); |
| 42 parser.addOption('long', help: 'Lacks an abbreviation'); |
| 43 |
| 44 validateUsage(parser, ''' |
| 45 -m, --[no-]mode The mode |
| 46 --long Lacks an abbreviation |
| 47 '''); |
| 48 }); |
| 49 |
| 50 test('help text is lined up past the longest option', () { |
| 51 var parser = new ArgParser(); |
| 52 parser.addFlag('mode', abbr: 'm', help: 'Lined up with below'); |
| 53 parser.addOption('a-really-long-name', help: 'Its help text'); |
| 54 |
| 55 validateUsage(parser, ''' |
| 56 -m, --[no-]mode Lined up with below |
| 57 --a-really-long-name Its help text |
| 58 '''); |
| 59 }); |
| 60 |
| 61 test('leading empty lines are ignored in help text', () { |
| 62 var parser = new ArgParser(); |
| 63 parser.addFlag('mode', help: '\n\n\n\nAfter newlines'); |
| 64 |
| 65 validateUsage(parser, ''' |
| 66 --[no-]mode After newlines |
| 67 '''); |
| 68 }); |
| 69 |
| 70 test('trailing empty lines are ignored in help text', () { |
| 71 var parser = new ArgParser(); |
| 72 parser.addFlag('mode', help: 'Before newlines\n\n\n\n'); |
| 73 |
| 74 validateUsage(parser, ''' |
| 75 --[no-]mode Before newlines |
| 76 '''); |
| 77 }); |
| 78 |
| 79 test('options are documented in the order they were added', () { |
| 80 var parser = new ArgParser(); |
| 81 parser.addFlag('zebra', help: 'First'); |
| 82 parser.addFlag('monkey', help: 'Second'); |
| 83 parser.addFlag('wombat', help: 'Third'); |
| 84 |
| 85 validateUsage(parser, ''' |
| 86 --[no-]zebra First |
| 87 --[no-]monkey Second |
| 88 --[no-]wombat Third |
| 89 '''); |
| 90 }); |
| 91 |
| 92 test('the default value for a flag is shown if on', () { |
| 93 var parser = new ArgParser(); |
| 94 parser.addFlag('affirm', help: 'Should be on', defaultsTo: true); |
| 95 parser.addFlag('negate', help: 'Should be off', defaultsTo: false); |
| 96 |
| 97 validateUsage(parser, ''' |
| 98 --[no-]affirm Should be on |
| 99 (defaults to on) |
| 100 |
| 101 --[no-]negate Should be off |
| 102 '''); |
| 103 }); |
| 104 |
| 105 test('the default value for an option with no allowed list is shown', () { |
| 106 var parser = new ArgParser(); |
| 107 parser.addOption('any', help: 'Can be anything', defaultsTo: 'whatevs'); |
| 108 |
| 109 validateUsage(parser, ''' |
| 110 --any Can be anything |
| 111 (defaults to "whatevs") |
| 112 '''); |
| 113 }); |
| 114 |
| 115 test('the value help is shown', () { |
| 116 var parser = new ArgParser(); |
| 117 parser.addOption('out', |
| 118 abbr: 'o', help: 'Where to write file', valueHelp: 'path'); |
| 119 |
| 120 validateUsage(parser, ''' |
| 121 -o, --out=<path> Where to write file |
| 122 '''); |
| 123 }); |
| 124 |
| 125 test('the allowed list is shown', () { |
| 126 var parser = new ArgParser(); |
| 127 parser.addOption('suit', |
| 128 help: 'Like in cards', |
| 129 allowed: ['spades', 'clubs', 'hearts', 'diamonds']); |
| 130 |
| 131 validateUsage(parser, ''' |
| 132 --suit Like in cards |
| 133 [spades, clubs, hearts, diamonds] |
| 134 '''); |
| 135 }); |
| 136 |
| 137 test('the default is highlighted in the allowed list', () { |
| 138 var parser = new ArgParser(); |
| 139 parser.addOption('suit', |
| 140 help: 'Like in cards', |
| 141 defaultsTo: 'clubs', |
| 142 allowed: ['spades', 'clubs', 'hearts', 'diamonds']); |
| 143 |
| 144 validateUsage(parser, ''' |
| 145 --suit Like in cards |
| 146 [spades, clubs (default), hearts, diamonds] |
| 147 '''); |
| 148 }); |
| 149 |
| 150 test('the allowed help is shown', () { |
| 151 var parser = new ArgParser(); |
| 152 parser.addOption('suit', |
| 153 help: 'Like in cards', |
| 154 defaultsTo: 'clubs', |
| 155 allowed: ['spades', 'clubs', 'diamonds', 'hearts'], |
| 156 allowedHelp: { |
| 157 'spades': 'Swords of a soldier', |
| 158 'clubs': 'Weapons of war', |
| 159 'diamonds': 'Money for this art', |
| 160 'hearts': 'The shape of my heart' |
| 161 }); |
| 162 |
| 163 validateUsage(parser, ''' |
| 164 --suit Like in cards |
| 165 |
| 166 [clubs] Weapons of war |
| 167 [diamonds] Money for this art |
| 168 [hearts] The shape of my heart |
| 169 [spades] Swords of a soldier |
| 170 '''); |
| 171 }); |
| 172 |
| 173 test("hidden options don't appear in the help", () { |
| 174 var parser = new ArgParser(); |
| 175 parser.addOption('first', help: 'The first option'); |
| 176 parser.addOption('second', hide: true); |
| 177 parser.addOption('third', help: 'The third option'); |
| 178 |
| 179 validateUsage(parser, ''' |
| 180 --first The first option |
| 181 --third The third option |
| 182 '''); |
| 183 }); |
| 184 |
| 185 test("hidden flags don't appear in the help", () { |
| 186 var parser = new ArgParser(); |
| 187 parser.addFlag('first', help: 'The first flag'); |
| 188 parser.addFlag('second', hide: true); |
| 189 parser.addFlag('third', help: 'The third flag'); |
| 190 |
| 191 validateUsage(parser, ''' |
| 192 --[no-]first The first flag |
| 193 --[no-]third The third flag |
| 194 '''); |
| 195 }); |
| 196 |
| 197 test("hidden options don't affect spacing", () { |
| 198 var parser = new ArgParser(); |
| 199 parser.addFlag('first', help: 'The first flag'); |
| 200 parser.addFlag('second-very-long-option', hide: true); |
| 201 parser.addFlag('third', help: 'The third flag'); |
| 202 |
| 203 validateUsage(parser, ''' |
| 204 --[no-]first The first flag |
| 205 --[no-]third The third flag |
| 206 '''); |
| 207 }); |
| 208 |
| 209 group("separators", () { |
| 210 test("separates options where it's placed", () { |
| 211 var parser = new ArgParser(); |
| 212 parser.addFlag('zebra', help: 'First'); |
| 213 parser.addSeparator('Primate:'); |
| 214 parser.addFlag('monkey', help: 'Second'); |
| 215 parser.addSeparator('Marsupial:'); |
| 216 parser.addFlag('wombat', help: 'Third'); |
| 217 |
| 218 validateUsage(parser, ''' |
| 219 --[no-]zebra First |
| 220 |
| 221 Primate: |
| 222 --[no-]monkey Second |
| 223 |
| 224 Marsupial: |
| 225 --[no-]wombat Third |
| 226 '''); |
| 227 }); |
| 228 |
| 229 test("doesn't add extra newlines after a multiline option", () { |
| 230 var parser = new ArgParser(); |
| 231 parser.addFlag('zebra', help: 'Multi\nline'); |
| 232 parser.addSeparator('Primate:'); |
| 233 parser.addFlag('monkey', help: 'Second'); |
| 234 |
| 235 validateUsage(parser, ''' |
| 236 --[no-]zebra Multi |
| 237 line |
| 238 |
| 239 Primate: |
| 240 --[no-]monkey Second |
| 241 '''); |
| 242 }); |
| 243 |
| 244 test("doesn't add newlines if it's the first component", () { |
| 245 var parser = new ArgParser(); |
| 246 parser.addSeparator('Equine:'); |
| 247 parser.addFlag('zebra', help: 'First'); |
| 248 |
| 249 validateUsage(parser, ''' |
| 250 Equine: |
| 251 --[no-]zebra First |
| 252 '''); |
| 253 }); |
| 254 |
| 255 test("doesn't add trailing newlines if it's the last component", () { |
| 256 var parser = new ArgParser(); |
| 257 parser.addFlag('zebra', help: 'First'); |
| 258 parser.addSeparator('Primate:'); |
| 259 |
| 260 validateUsage(parser, ''' |
| 261 --[no-]zebra First |
| 262 |
| 263 Primate: |
| 264 '''); |
| 265 }); |
| 266 |
| 267 test("adds a newline after another separator", () { |
| 268 var parser = new ArgParser(); |
| 269 parser.addSeparator('First'); |
| 270 parser.addSeparator('Second'); |
| 271 |
| 272 validateUsage(parser, ''' |
| 273 First |
| 274 |
| 275 Second |
| 276 '''); |
| 277 }); |
| 278 }); |
| 279 }); |
| 280 } |
| 281 |
| 282 void validateUsage(ArgParser parser, String expected) { |
| 283 expected = unindentString(expected); |
| 284 expect(parser.usage, equals(expected)); |
| 285 } |
| 286 |
| 287 // TODO(rnystrom): Replace one in test_utils. |
| 288 String unindentString(String text) { |
| 289 var lines = text.split('\n'); |
| 290 |
| 291 // Count the indentation of the last line. |
| 292 var whitespace = new RegExp('^ *'); |
| 293 var indent = whitespace.firstMatch(lines[lines.length - 1])[0].length; |
| 294 |
| 295 // Drop the last line. It only exists for specifying indentation. |
| 296 lines.removeLast(); |
| 297 |
| 298 // Strip indentation from the remaining lines. |
| 299 for (var i = 0; i < lines.length; i++) { |
| 300 var line = lines[i]; |
| 301 if (line.length <= indent) { |
| 302 // It's short, so it must be nothing but whitespace. |
| 303 if (line.trim() != '') { |
| 304 throw new ArgumentError( |
| 305 'Line "$line" does not have enough indentation.'); |
| 306 } |
| 307 |
| 308 lines[i] = ''; |
| 309 } else { |
| 310 if (line.substring(0, indent).trim() != '') { |
| 311 throw new ArgumentError( |
| 312 'Line "$line" does not have enough indentation.'); |
| 313 } |
| 314 |
| 315 lines[i] = line.substring(indent); |
| 316 } |
| 317 } |
| 318 |
| 319 return lines.join('\n'); |
| 320 } |
OLD | NEW |