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