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