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