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 args_test; |
| 6 |
| 7 import 'package:unittest/unittest.dart'; |
| 8 import 'package:args/args.dart'; |
| 9 |
| 10 main() { |
| 11 group('ArgParser.getUsage()', () { |
| 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 allowed list is shown', () { |
| 126 var parser = new ArgParser(); |
| 127 parser.addOption('suit', help: 'Like in cards', |
| 128 allowed: ['spades', 'clubs', 'hearts', 'diamonds']); |
| 129 |
| 130 validateUsage(parser, |
| 131 ''' |
| 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', help: 'Like in cards', defaultsTo: 'clubs', |
| 140 allowed: ['spades', 'clubs', 'hearts', 'diamonds']); |
| 141 |
| 142 validateUsage(parser, |
| 143 ''' |
| 144 --suit Like in cards |
| 145 [spades, clubs (default), hearts, diamonds] |
| 146 '''); |
| 147 }); |
| 148 |
| 149 test('the allowed help is shown', () { |
| 150 var parser = new ArgParser(); |
| 151 parser.addOption('suit', help: 'Like in cards', defaultsTo: 'clubs', |
| 152 allowed: ['spades', 'clubs', 'diamonds', 'hearts'], |
| 153 allowedHelp: { |
| 154 'spades': 'Swords of a soldier', |
| 155 'clubs': 'Weapons of war', |
| 156 'diamonds': 'Money for this art', |
| 157 'hearts': 'The shape of my heart' |
| 158 }); |
| 159 |
| 160 validateUsage(parser, |
| 161 ''' |
| 162 --suit Like in cards |
| 163 |
| 164 [clubs] Weapons of war |
| 165 [diamonds] Money for this art |
| 166 [hearts] The shape of my heart |
| 167 [spades] Swords of a soldier |
| 168 '''); |
| 169 }); |
| 170 }); |
| 171 } |
| 172 |
| 173 throwsIllegalArg(function) { |
| 174 expect(function, throwsArgumentError); |
| 175 } |
| 176 |
| 177 validateUsage(ArgParser parser, String expected) { |
| 178 expected = unindentString(expected); |
| 179 expect(parser.getUsage(), equals(expected)); |
| 180 } |
| 181 |
| 182 // TODO(rnystrom): Replace one in test_utils. |
| 183 String unindentString(String text) { |
| 184 var lines = text.split('\n'); |
| 185 |
| 186 // Count the indentation of the last line. |
| 187 var whitespace = new RegExp('^ *'); |
| 188 var indent = whitespace.firstMatch(lines[lines.length - 1])[0].length; |
| 189 |
| 190 // Drop the last line. It only exists for specifying indentation. |
| 191 lines.removeLast(); |
| 192 |
| 193 // Strip indentation from the remaining lines. |
| 194 for (var i = 0; i < lines.length; i++) { |
| 195 var line = lines[i]; |
| 196 if (line.length <= indent) { |
| 197 // It's short, so it must be nothing but whitespace. |
| 198 if (line.trim() != '') { |
| 199 throw new ArgumentError( |
| 200 'Line "$line" does not have enough indentation.'); |
| 201 } |
| 202 |
| 203 lines[i] = ''; |
| 204 } else { |
| 205 if (line.substring(0, indent).trim() != '') { |
| 206 throw new ArgumentError( |
| 207 'Line "$line" does not have enough indentation.'); |
| 208 } |
| 209 |
| 210 lines[i] = line.substring(indent); |
| 211 } |
| 212 } |
| 213 |
| 214 return Strings.join(lines, '\n'); |
| 215 } |
OLD | NEW |