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