| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2015, 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 // VMOptions=--compile-all --error_on_bad_type --error_on_bad_override --checked | |
| 5 | |
| 6 import 'dart:async'; | |
| 7 | |
| 8 import 'package:observatory/cli.dart'; | |
| 9 import 'package:unittest/unittest.dart'; | |
| 10 | |
| 11 class TestCommand extends Command { | |
| 12 TestCommand(this.out, name, children) : super(name, children); | |
| 13 StringBuffer out; | |
| 14 | |
| 15 Future run(List<String> args) { | |
| 16 out.write('executing ${name}(${args})\n'); | |
| 17 return new Future.value(null); | |
| 18 } | |
| 19 } | |
| 20 | |
| 21 class TestCompleteCommand extends Command { | |
| 22 TestCompleteCommand(this.out, name, children) : super(name, children); | |
| 23 StringBuffer out; | |
| 24 | |
| 25 Future<List<String>> complete(List<String> args) { | |
| 26 var possibles = ['one ', 'two ', 'three ']; | |
| 27 return new Future.value( | |
| 28 possibles.where((possible) => possible.startsWith(args[0])).toList()); | |
| 29 } | |
| 30 | |
| 31 Future run(List<String> args) { | |
| 32 out.write('executing ${name}(${args})\n'); | |
| 33 return new Future.value(null); | |
| 34 } | |
| 35 } | |
| 36 | |
| 37 void testCommandComplete() { | |
| 38 RootCommand cmd = | |
| 39 new RootCommand([new TestCommand(null, 'alpha', []), | |
| 40 | |
| 41 new TestCommand(null, 'game', [ | |
| 42 new TestCommand(null, 'checkers', []), | |
| 43 new TestCommand(null, 'chess', [])]), | |
| 44 | |
| 45 new TestCommand(null, 'gamera', [ | |
| 46 new TestCommand(null, 'london', []), | |
| 47 new TestCommand(null, 'tokyo', []), | |
| 48 new TestCommand(null, 'topeka', [])]), | |
| 49 | |
| 50 new TestCompleteCommand(null, 'count', [ | |
| 51 new TestCommand(null, 'chocula', [])])]); | |
| 52 | |
| 53 // Show all commands. | |
| 54 cmd.completeCommand('').then((result) { | |
| 55 expect(result, equals(['alpha ', 'game ', 'gamera ', 'count '])); | |
| 56 }); | |
| 57 | |
| 58 // Substring completion. | |
| 59 cmd.completeCommand('al').then((result) { | |
| 60 expect(result, equals(['alpha '])); | |
| 61 }); | |
| 62 | |
| 63 // Full string completion. | |
| 64 cmd.completeCommand('alpha').then((result) { | |
| 65 expect(result, equals(['alpha '])); | |
| 66 }); | |
| 67 | |
| 68 // Extra space, no subcommands. | |
| 69 cmd.completeCommand('alpha ').then((result) { | |
| 70 expect(result, equals(['alpha '])); | |
| 71 }); | |
| 72 | |
| 73 // Ambiguous completion. | |
| 74 cmd.completeCommand('g').then((result) { | |
| 75 expect(result, equals(['game ', 'gamera '])); | |
| 76 }); | |
| 77 | |
| 78 // Ambiguous completion, exact match not preferred. | |
| 79 cmd.completeCommand('game').then((result) { | |
| 80 expect(result, equals(['game ', 'gamera '])); | |
| 81 }); | |
| 82 | |
| 83 // Show all subcommands. | |
| 84 cmd.completeCommand('gamera ').then((result) { | |
| 85 expect(result, equals(['gamera london ', 'gamera tokyo ', 'gamera topeka '])
); | |
| 86 }); | |
| 87 | |
| 88 // Subcommand completion. | |
| 89 cmd.completeCommand('gamera l').then((result) { | |
| 90 expect(result, equals(['gamera london '])); | |
| 91 }); | |
| 92 | |
| 93 // Extra space, with subcommand. | |
| 94 cmd.completeCommand('gamera london ').then((result) { | |
| 95 expect(result, equals(['gamera london '])); | |
| 96 }); | |
| 97 | |
| 98 // Ambiguous subcommand completion. | |
| 99 cmd.completeCommand('gamera t').then((result) { | |
| 100 expect(result, equals(['gamera tokyo ', 'gamera topeka '])); | |
| 101 }); | |
| 102 | |
| 103 // Ambiguous subcommand completion with substring prefix. | |
| 104 // Note that the prefix is left alone. | |
| 105 cmd.completeCommand('gamer t').then((result) { | |
| 106 expect(result, equals(['gamer tokyo ', 'gamer topeka '])); | |
| 107 }); | |
| 108 | |
| 109 // Ambiguous but exact prefix is preferred. | |
| 110 cmd.completeCommand('game chec').then((result) { | |
| 111 expect(result, equals(['game checkers '])); | |
| 112 }); | |
| 113 | |
| 114 // Ambiguous non-exact prefix means no matches. | |
| 115 cmd.completeCommand('gam chec').then((result) { | |
| 116 expect(result, equals([])); | |
| 117 }); | |
| 118 | |
| 119 // Locals + subcommands, show all. | |
| 120 cmd.completeCommand('count ').then((result) { | |
| 121 expect(result, equals(['count chocula ', | |
| 122 'count one ', | |
| 123 'count two ', | |
| 124 'count three '])); | |
| 125 }); | |
| 126 | |
| 127 // Locals + subcommands, single local match. | |
| 128 cmd.completeCommand('count th').then((result) { | |
| 129 expect(result, equals(['count three '])); | |
| 130 }); | |
| 131 | |
| 132 // Locals + subcommands, ambiguous local match. | |
| 133 cmd.completeCommand('count t').then((result) { | |
| 134 expect(result, equals(['count two ', 'count three '])); | |
| 135 }); | |
| 136 | |
| 137 // Locals + subcommands, single command match. | |
| 138 cmd.completeCommand('co choc').then((result) { | |
| 139 expect(result, equals(['co chocula '])); | |
| 140 }); | |
| 141 | |
| 142 // We gobble spare spaces in the prefix but not elsewhere. | |
| 143 cmd.completeCommand(' game chec').then((result) { | |
| 144 expect(result, equals(['game checkers '])); | |
| 145 }); | |
| 146 } | |
| 147 | |
| 148 void testCommandRunSimple() { | |
| 149 // Run a simple command. | |
| 150 StringBuffer out = new StringBuffer(); | |
| 151 RootCommand cmd = new RootCommand([new TestCommand(out, 'alpha', [])]); | |
| 152 | |
| 153 // Full name dispatch works. Argument passing works. | |
| 154 cmd.runCommand('alpha dog').then(expectAsync((_) { | |
| 155 expect(out.toString(), contains('executing alpha([dog])\n')); | |
| 156 out.clear(); | |
| 157 // Substring dispatch works. | |
| 158 cmd.runCommand('al cat mouse').then(expectAsync((_) { | |
| 159 expect(out.toString(), contains('executing alpha([cat , mouse])\n')); | |
| 160 })); | |
| 161 })); | |
| 162 } | |
| 163 | |
| 164 void testCommandRunSubcommand() { | |
| 165 // Run a simple command. | |
| 166 StringBuffer out = new StringBuffer(); | |
| 167 RootCommand cmd = | |
| 168 new RootCommand([ | |
| 169 new TestCommand(out, 'alpha', [ | |
| 170 new TestCommand(out, 'beta', []), | |
| 171 new TestCommand(out, 'gamma', [])])]); | |
| 172 | |
| 173 cmd.runCommand('a b').then(expectAsync((_) { | |
| 174 expect(out.toString(), equals('executing beta([])\n')); | |
| 175 out.clear(); | |
| 176 cmd.runCommand('alpha g ').then(expectAsync((_) { | |
| 177 expect(out.toString(), equals('executing gamma([])\n')); | |
| 178 })); | |
| 179 })); | |
| 180 } | |
| 181 | |
| 182 void testCommandRunNotFound() { | |
| 183 // Run a simple command. | |
| 184 StringBuffer out = new StringBuffer(); | |
| 185 RootCommand cmd = new RootCommand([new TestCommand(out, 'alpha', [])]); | |
| 186 | |
| 187 cmd.runCommand('goose').catchError(expectAsync((e) { | |
| 188 expect(e, equals('notfound')); | |
| 189 })); | |
| 190 } | |
| 191 | |
| 192 void testCommandRunAmbiguous() { | |
| 193 // Run a simple command. | |
| 194 StringBuffer out = new StringBuffer(); | |
| 195 RootCommand cmd = new RootCommand([new TestCommand(out, 'alpha', []), | |
| 196 new TestCommand(out, 'ankle', [])]); | |
| 197 | |
| 198 cmd.runCommand('a 55').catchError(expectAsync((e) { | |
| 199 expect(e, equals('ambiguous')); | |
| 200 out.clear(); | |
| 201 cmd.runCommand('ankl 55').then(expectAsync((_) { | |
| 202 expect(out.toString(), equals('executing ankle([55])\n')); | |
| 203 })); | |
| 204 })); | |
| 205 } | |
| 206 | |
| 207 void testCommandRunAlias() { | |
| 208 // Run a simple command. | |
| 209 StringBuffer out = new StringBuffer(); | |
| 210 var aliasCmd = new TestCommand(out, 'alpha', []); | |
| 211 aliasCmd.alias = 'a'; | |
| 212 RootCommand cmd = new RootCommand([aliasCmd, | |
| 213 new TestCommand(out, 'ankle', [])]); | |
| 214 | |
| 215 cmd.runCommand('a 55').then(expectAsync((_) { | |
| 216 expect(out.toString(), equals('executing alpha([55])\n')); | |
| 217 })); | |
| 218 } | |
| 219 | |
| 220 main() { | |
| 221 test('command completion test suite', testCommandComplete); | |
| 222 test('run a simple command', testCommandRunSimple); | |
| 223 test('run a subcommand', testCommandRunSubcommand); | |
| 224 test('run a command which is not found', testCommandRunNotFound); | |
| 225 test('run a command which is ambiguous', testCommandRunAmbiguous); | |
| 226 test('run a command using an alias', testCommandRunAlias); | |
| 227 } | |
| 228 | |
| OLD | NEW |