| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 library test.services.completion.dart.keyword; | 5 library test.services.completion.dart.keyword; |
| 6 | 6 |
| 7 import 'package:analysis_server/src/protocol.dart'; | 7 import 'package:analysis_server/src/protocol.dart'; |
| 8 import 'package:analysis_server/src/services/completion/keyword_computer.dart'; | 8 import 'package:analysis_server/src/services/completion/keyword_computer.dart'; |
| 9 import 'package:analyzer/src/generated/scanner.dart'; | 9 import 'package:analyzer/src/generated/scanner.dart'; |
| 10 import 'package:unittest/unittest.dart'; | 10 import 'package:unittest/unittest.dart'; |
| 11 | 11 |
| 12 import '../../reflective_tests.dart'; | 12 import '../../reflective_tests.dart'; |
| 13 import 'completion_test_util.dart'; | 13 import 'completion_test_util.dart'; |
| 14 | 14 |
| 15 main() { | 15 main() { |
| 16 groupSep = ' | '; | 16 groupSep = ' | '; |
| 17 runReflectiveTests(KeywordComputerTest); | 17 runReflectiveTests(KeywordComputerTest); |
| 18 } | 18 } |
| 19 | 19 |
| 20 @ReflectiveTestCase() | 20 @ReflectiveTestCase() |
| 21 class KeywordComputerTest extends AbstractCompletionTest { | 21 class KeywordComputerTest extends AbstractCompletionTest { |
| 22 | 22 |
| 23 void assertSuggestKeywords(Iterable<Keyword> expectedKeywords) { | 23 void assertSuggestKeywords(Iterable<Keyword> expectedKeywords, |
| 24 [CompletionRelevance relevance = CompletionRelevance.DEFAULT]) { |
| 24 Set<Keyword> actualKeywords = new Set<Keyword>(); | 25 Set<Keyword> actualKeywords = new Set<Keyword>(); |
| 25 request.suggestions.forEach((CompletionSuggestion s) { | 26 request.suggestions.forEach((CompletionSuggestion s) { |
| 26 if (s.kind == CompletionSuggestionKind.KEYWORD) { | 27 if (s.kind == CompletionSuggestionKind.KEYWORD) { |
| 27 Keyword k = Keyword.keywords[s.completion]; | 28 Keyword k = Keyword.keywords[s.completion]; |
| 28 if (k == null) { | 29 if (k == null) { |
| 29 fail('Invalid keyword suggested: ${s.completion}'); | 30 fail('Invalid keyword suggested: ${s.completion}'); |
| 30 } else { | 31 } else { |
| 31 if (!actualKeywords.add(k)) { | 32 if (!actualKeywords.add(k)) { |
| 32 fail('Duplicate keyword suggested: ${s.completion}'); | 33 fail('Duplicate keyword suggested: ${s.completion}'); |
| 33 } | 34 } |
| 34 } | 35 } |
| 35 expect(s.relevance, equals(CompletionRelevance.DEFAULT)); | 36 expect(s.relevance, equals(relevance)); |
| 36 expect(s.selectionOffset, equals(s.completion.length)); | 37 expect(s.selectionOffset, equals(s.completion.length)); |
| 37 expect(s.selectionLength, equals(0)); | 38 expect(s.selectionLength, equals(0)); |
| 38 expect(s.isDeprecated, equals(false)); | 39 expect(s.isDeprecated, equals(false)); |
| 39 expect(s.isPotential, equals(false)); | 40 expect(s.isPotential, equals(false)); |
| 40 } | 41 } |
| 41 }); | 42 }); |
| 42 if (expectedKeywords.any((k) => k is String)) { | 43 if (expectedKeywords.any((k) => k is String)) { |
| 43 StringBuffer msg = new StringBuffer(); | 44 StringBuffer msg = new StringBuffer(); |
| 44 msg.writeln('Expected set should be:'); | 45 msg.writeln('Expected set should be:'); |
| 45 expectedKeywords.forEach((n) { | 46 expectedKeywords.forEach((n) { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 67 test_after_class() { | 68 test_after_class() { |
| 68 addTestSource('class A {} ^'); | 69 addTestSource('class A {} ^'); |
| 69 expect(computeFast(), isTrue); | 70 expect(computeFast(), isTrue); |
| 70 assertSuggestKeywords( | 71 assertSuggestKeywords( |
| 71 [ | 72 [ |
| 72 Keyword.ABSTRACT, | 73 Keyword.ABSTRACT, |
| 73 Keyword.CLASS, | 74 Keyword.CLASS, |
| 74 Keyword.CONST, | 75 Keyword.CONST, |
| 75 Keyword.FINAL, | 76 Keyword.FINAL, |
| 76 Keyword.TYPEDEF, | 77 Keyword.TYPEDEF, |
| 77 Keyword.VAR]); | 78 Keyword.VAR], |
| 79 CompletionRelevance.HIGH); |
| 78 } | 80 } |
| 79 | 81 |
| 80 test_before_import() { | 82 test_before_import() { |
| 81 addTestSource('^ import foo;'); | 83 addTestSource('^ import foo;'); |
| 82 expect(computeFast(), isTrue); | 84 expect(computeFast(), isTrue); |
| 83 assertSuggestKeywords( | 85 assertSuggestKeywords( |
| 84 [Keyword.EXPORT, Keyword.IMPORT, Keyword.LIBRARY, Keyword.PART]); | 86 [Keyword.EXPORT, Keyword.IMPORT, Keyword.LIBRARY, Keyword.PART], |
| 87 CompletionRelevance.HIGH); |
| 85 } | 88 } |
| 86 | 89 |
| 87 test_class() { | 90 test_class() { |
| 88 addTestSource('class A ^'); | 91 addTestSource('class A ^'); |
| 89 expect(computeFast(), isTrue); | 92 expect(computeFast(), isTrue); |
| 90 assertSuggestKeywords([Keyword.EXTENDS, Keyword.IMPLEMENTS]); | 93 assertSuggestKeywords( |
| 94 [Keyword.EXTENDS, Keyword.IMPLEMENTS], |
| 95 CompletionRelevance.HIGH); |
| 91 } | 96 } |
| 92 | 97 |
| 93 test_class_extends() { | 98 test_class_extends() { |
| 94 addTestSource('class A extends foo ^'); | 99 addTestSource('class A extends foo ^'); |
| 95 expect(computeFast(), isTrue); | 100 expect(computeFast(), isTrue); |
| 96 assertSuggestKeywords([Keyword.IMPLEMENTS, Keyword.WITH]); | 101 assertSuggestKeywords( |
| 102 [Keyword.IMPLEMENTS, Keyword.WITH], |
| 103 CompletionRelevance.HIGH); |
| 97 } | 104 } |
| 98 | 105 |
| 99 test_class_extends_name() { | 106 test_class_extends_name() { |
| 100 addTestSource('class A extends ^'); | 107 addTestSource('class A extends ^'); |
| 101 expect(computeFast(), isTrue); | 108 expect(computeFast(), isTrue); |
| 102 assertSuggestKeywords([]); | 109 assertSuggestKeywords([]); |
| 103 } | 110 } |
| 104 | 111 |
| 105 test_class_implements() { | 112 test_class_implements() { |
| 106 addTestSource('class A ^ implements foo'); | 113 addTestSource('class A ^ implements foo'); |
| 107 expect(computeFast(), isTrue); | 114 expect(computeFast(), isTrue); |
| 108 assertSuggestKeywords([Keyword.EXTENDS]); | 115 assertSuggestKeywords([Keyword.EXTENDS], CompletionRelevance.HIGH); |
| 109 } | 116 } |
| 110 | 117 |
| 111 test_class_implements_name() { | 118 test_class_implements_name() { |
| 112 addTestSource('class A implements ^'); | 119 addTestSource('class A implements ^'); |
| 113 expect(computeFast(), isTrue); | 120 expect(computeFast(), isTrue); |
| 114 assertSuggestKeywords([]); | 121 assertSuggestKeywords([]); |
| 115 } | 122 } |
| 116 | 123 |
| 117 test_class_name() { | 124 test_class_name() { |
| 118 addTestSource('class ^'); | 125 addTestSource('class ^'); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 133 [ | 140 [ |
| 134 Keyword.ABSTRACT, | 141 Keyword.ABSTRACT, |
| 135 Keyword.CLASS, | 142 Keyword.CLASS, |
| 136 Keyword.CONST, | 143 Keyword.CONST, |
| 137 Keyword.EXPORT, | 144 Keyword.EXPORT, |
| 138 Keyword.FINAL, | 145 Keyword.FINAL, |
| 139 Keyword.IMPORT, | 146 Keyword.IMPORT, |
| 140 Keyword.LIBRARY, | 147 Keyword.LIBRARY, |
| 141 Keyword.PART, | 148 Keyword.PART, |
| 142 Keyword.TYPEDEF, | 149 Keyword.TYPEDEF, |
| 143 Keyword.VAR]); | 150 Keyword.VAR], |
| 151 CompletionRelevance.HIGH); |
| 144 } | 152 } |
| 145 | 153 |
| 146 test_function_body() { | 154 test_function_body() { |
| 147 addTestSource('main() {^}'); | 155 addTestSource('main() {^}'); |
| 148 expect(computeFast(), isTrue); | 156 expect(computeFast(), isTrue); |
| 149 assertSuggestKeywords( | 157 assertSuggestKeywords( |
| 150 [ | 158 [ |
| 151 Keyword.ASSERT, | 159 Keyword.ASSERT, |
| 152 Keyword.CASE, | 160 Keyword.CASE, |
| 153 Keyword.CONTINUE, | 161 Keyword.CONTINUE, |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 assertSuggestKeywords( | 200 assertSuggestKeywords( |
| 193 [ | 201 [ |
| 194 Keyword.ABSTRACT, | 202 Keyword.ABSTRACT, |
| 195 Keyword.CLASS, | 203 Keyword.CLASS, |
| 196 Keyword.CONST, | 204 Keyword.CONST, |
| 197 Keyword.EXPORT, | 205 Keyword.EXPORT, |
| 198 Keyword.FINAL, | 206 Keyword.FINAL, |
| 199 Keyword.IMPORT, | 207 Keyword.IMPORT, |
| 200 Keyword.PART, | 208 Keyword.PART, |
| 201 Keyword.TYPEDEF, | 209 Keyword.TYPEDEF, |
| 202 Keyword.VAR]); | 210 Keyword.VAR], |
| 211 CompletionRelevance.HIGH); |
| 203 } | 212 } |
| 204 | 213 |
| 205 test_library_name() { | 214 test_library_name() { |
| 206 addTestSource('library ^'); | 215 addTestSource('library ^'); |
| 207 expect(computeFast(), isTrue); | 216 expect(computeFast(), isTrue); |
| 208 assertSuggestKeywords([]); | 217 assertSuggestKeywords([]); |
| 209 } | 218 } |
| 210 | 219 |
| 211 test_method_body() { | 220 test_method_body() { |
| 212 addTestSource('class A { foo() {^}}'); | 221 addTestSource('class A { foo() {^}}'); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 246 assertSuggestKeywords( | 255 assertSuggestKeywords( |
| 247 [ | 256 [ |
| 248 Keyword.ABSTRACT, | 257 Keyword.ABSTRACT, |
| 249 Keyword.CLASS, | 258 Keyword.CLASS, |
| 250 Keyword.CONST, | 259 Keyword.CONST, |
| 251 Keyword.EXPORT, | 260 Keyword.EXPORT, |
| 252 Keyword.FINAL, | 261 Keyword.FINAL, |
| 253 Keyword.IMPORT, | 262 Keyword.IMPORT, |
| 254 Keyword.PART, | 263 Keyword.PART, |
| 255 Keyword.TYPEDEF, | 264 Keyword.TYPEDEF, |
| 256 Keyword.VAR]); | 265 Keyword.VAR], |
| 266 CompletionRelevance.HIGH); |
| 257 } | 267 } |
| 258 | 268 |
| 259 test_partial_class() { | 269 test_partial_class() { |
| 260 addTestSource('cl^'); | 270 addTestSource('cl^'); |
| 261 expect(computeFast(), isTrue); | 271 expect(computeFast(), isTrue); |
| 262 assertSuggestKeywords( | 272 assertSuggestKeywords( |
| 263 [ | 273 [ |
| 264 Keyword.ABSTRACT, | 274 Keyword.ABSTRACT, |
| 265 Keyword.CLASS, | 275 Keyword.CLASS, |
| 266 Keyword.CONST, | 276 Keyword.CONST, |
| 267 Keyword.EXPORT, | 277 Keyword.EXPORT, |
| 268 Keyword.FINAL, | 278 Keyword.FINAL, |
| 269 Keyword.IMPORT, | 279 Keyword.IMPORT, |
| 270 Keyword.LIBRARY, | 280 Keyword.LIBRARY, |
| 271 Keyword.PART, | 281 Keyword.PART, |
| 272 Keyword.TYPEDEF, | 282 Keyword.TYPEDEF, |
| 273 Keyword.VAR]); | 283 Keyword.VAR], |
| 284 CompletionRelevance.HIGH); |
| 274 } | 285 } |
| 275 | 286 |
| 276 test_partial_class2() { | 287 test_partial_class2() { |
| 277 addTestSource('library a; cl^'); | 288 addTestSource('library a; cl^'); |
| 278 expect(computeFast(), isTrue); | 289 expect(computeFast(), isTrue); |
| 279 assertSuggestKeywords( | 290 assertSuggestKeywords( |
| 280 [ | 291 [ |
| 281 Keyword.ABSTRACT, | 292 Keyword.ABSTRACT, |
| 282 Keyword.CLASS, | 293 Keyword.CLASS, |
| 283 Keyword.CONST, | 294 Keyword.CONST, |
| 284 Keyword.EXPORT, | 295 Keyword.EXPORT, |
| 285 Keyword.FINAL, | 296 Keyword.FINAL, |
| 286 Keyword.IMPORT, | 297 Keyword.IMPORT, |
| 287 Keyword.PART, | 298 Keyword.PART, |
| 288 Keyword.TYPEDEF, | 299 Keyword.TYPEDEF, |
| 289 Keyword.VAR]); | 300 Keyword.VAR], |
| 301 CompletionRelevance.HIGH); |
| 290 } | 302 } |
| 291 | 303 |
| 292 void _appendKeywords(StringBuffer msg, Iterable<Keyword> keywords) { | 304 void _appendKeywords(StringBuffer msg, Iterable<Keyword> keywords) { |
| 293 List<Keyword> sorted = keywords.toList(); | 305 List<Keyword> sorted = keywords.toList(); |
| 294 sorted.sort((k1, k2) => k1.name.compareTo(k2.name)); | 306 sorted.sort((k1, k2) => k1.name.compareTo(k2.name)); |
| 295 sorted.forEach((k) => msg.writeln(' Keyword.${k.name},')); | 307 sorted.forEach((k) => msg.writeln(' Keyword.${k.name},')); |
| 296 } | 308 } |
| 297 | 309 |
| 298 bool _equalSets(Iterable<Keyword> iter1, Iterable<Keyword> iter2) { | 310 bool _equalSets(Iterable<Keyword> iter1, Iterable<Keyword> iter2) { |
| 299 if (iter1.length != iter2.length) return false; | 311 if (iter1.length != iter2.length) return false; |
| 300 if (iter1.any((k) => !iter2.contains(k))) return false; | 312 if (iter1.any((k) => !iter2.contains(k))) return false; |
| 301 if (iter2.any((k) => !iter1.contains(k))) return false; | 313 if (iter2.any((k) => !iter1.contains(k))) return false; |
| 302 return true; | 314 return true; |
| 303 } | 315 } |
| 304 } | 316 } |
| OLD | NEW |