| 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.domain.completion; | 5 library test.domain.completion; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'package:analysis_server/src/constants.dart'; | 9 import 'package:analysis_server/src/constants.dart'; |
| 10 import 'package:analysis_server/src/domain_completion.dart'; | 10 import 'package:analysis_server/src/domain_completion.dart'; |
| 11 import 'package:analysis_server/src/protocol.dart'; | 11 import 'package:analysis_server/src/protocol.dart'; |
| 12 import 'package:analysis_services/completion/completion_suggestion.dart'; | 12 import 'package:analysis_services/completion/completion_suggestion.dart'; |
| 13 import 'package:analysis_services/constants.dart'; | 13 import 'package:analysis_services/constants.dart'; |
| 14 import 'package:analysis_services/index/index.dart' show Index; | 14 import 'package:analysis_services/index/index.dart' show Index; |
| 15 import 'package:analysis_services/index/local_memory_index.dart'; | 15 import 'package:analysis_services/index/local_memory_index.dart'; |
| 16 import 'package:analysis_testing/reflective_tests.dart'; | 16 import 'package:analysis_testing/reflective_tests.dart'; |
| 17 import 'package:unittest/unittest.dart'; | 17 import 'package:unittest/unittest.dart'; |
| 18 | 18 |
| 19 import 'analysis_abstract.dart'; | 19 import 'analysis_abstract.dart'; |
| 20 import 'package:analysis_server/src/domain_analysis.dart'; |
| 20 | 21 |
| 21 main() { | 22 main() { |
| 22 groupSep = ' | '; | 23 groupSep = ' | '; |
| 23 runReflectiveTests(CompletionTest); | 24 runReflectiveTests(CompletionTest); |
| 24 } | 25 } |
| 25 | 26 |
| 26 @ReflectiveTestCase() | 27 @ReflectiveTestCase() |
| 27 class CompletionTest extends AbstractAnalysisTest { | 28 class CompletionTest extends AbstractAnalysisTest { |
| 28 String completionId; | 29 String completionId; |
| 29 int completionOffset; | 30 int completionOffset; |
| 30 List<CompletionSuggestion> suggestions = []; | 31 List<CompletionSuggestion> suggestions = []; |
| 31 bool suggestionsDone = false; | 32 bool suggestionsDone = false; |
| 32 | 33 |
| 33 String addTestFile(String content) { | 34 String addTestFile(String content) { |
| 34 completionOffset = content.indexOf('^'); | 35 completionOffset = content.indexOf('^'); |
| 35 expect(completionOffset, isNot(equals(-1)), reason: 'missing ^'); | 36 expect(completionOffset, isNot(equals(-1)), reason: 'missing ^'); |
| 36 int nextOffset = content.indexOf('^', completionOffset + 1); | 37 int nextOffset = content.indexOf('^', completionOffset + 1); |
| 37 expect(nextOffset, equals(-1), reason: 'too many ^'); | 38 expect(nextOffset, equals(-1), reason: 'too many ^'); |
| 38 return super.addTestFile( | 39 return super.addTestFile( |
| 39 content.substring(0, completionOffset) | 40 content.substring(0, completionOffset) |
| 40 + content.substring(completionOffset + 1)); | 41 + content.substring(completionOffset + 1)); |
| 41 } | 42 } |
| 42 | 43 |
| 43 void assertHasResult(CompletionSuggestionKind kind, | 44 void assertHasResult(CompletionSuggestionKind kind, String completion, |
| 44 CompletionRelevance relevance, String completion, | 45 [CompletionRelevance relevance = CompletionRelevance.DEFAULT, |
| 45 bool isDeprecated, bool isPotential) { | 46 bool isDeprecated = false, bool isPotential = false]) { |
| 46 var cs = suggestions.firstWhere((cs) => cs.completion == completion, orElse:
() { | 47 var cs = suggestions.firstWhere((cs) => cs.completion == completion, orElse:
() { |
| 47 var completions = suggestions.map((s) => s.completion).toList(); | 48 var completions = suggestions.map((s) => s.completion).toList(); |
| 48 fail('expected "$completion" but found\n $completions'); | 49 fail('expected "$completion" but found\n $completions'); |
| 49 }); | 50 }); |
| 50 expect(cs.kind, equals(kind)); | 51 expect(cs.kind, equals(kind)); |
| 51 expect(cs.relevance, equals(relevance)); | 52 expect(cs.relevance, equals(relevance)); |
| 52 expect(cs.selectionOffset, equals(completion.length)); | 53 expect(cs.selectionOffset, equals(completion.length)); |
| 53 expect(cs.selectionLength, equals(0)); | 54 expect(cs.selectionLength, equals(0)); |
| 54 expect(cs.isDeprecated, equals(isDeprecated)); | 55 expect(cs.isDeprecated, equals(isDeprecated)); |
| 55 expect(cs.isPotential, equals(isPotential)); | 56 expect(cs.isPotential, equals(isPotential)); |
| 56 } | 57 } |
| 57 | 58 |
| 59 void assertNoResult(String completion) { |
| 60 if (suggestions.any((cs) => cs.completion == completion)) { |
| 61 fail('did not expect completion: $completion'); |
| 62 } |
| 63 } |
| 64 |
| 58 void assertValidId(String id) { | 65 void assertValidId(String id) { |
| 59 expect(id, isNotNull); | 66 expect(id, isNotNull); |
| 60 expect(id.isNotEmpty, isTrue); | 67 expect(id.isNotEmpty, isTrue); |
| 61 } | 68 } |
| 62 | 69 |
| 63 @override | 70 @override |
| 64 Index createIndex() { | 71 Index createIndex() { |
| 65 return createLocalMemoryIndex(); | 72 return createLocalMemoryIndex(); |
| 66 } | 73 } |
| 67 | 74 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 handler = new CompletionDomainHandler(server); | 107 handler = new CompletionDomainHandler(server); |
| 101 } | 108 } |
| 102 | 109 |
| 103 Future waitForSuggestions() { | 110 Future waitForSuggestions() { |
| 104 if (suggestionsDone) { | 111 if (suggestionsDone) { |
| 105 return new Future.value(); | 112 return new Future.value(); |
| 106 } | 113 } |
| 107 return new Future.delayed(Duration.ZERO, waitForSuggestions); | 114 return new Future.delayed(Duration.ZERO, waitForSuggestions); |
| 108 } | 115 } |
| 109 | 116 |
| 110 test_suggestions() { | 117 test_suggestions_importedType() { |
| 111 addTestFile(''' | 118 addTestFile(''' |
| 112 import 'dart:html'; | 119 import 'dart:html'; |
| 113 main() {^} | 120 main() {^} |
| 114 '''); | 121 '''); |
| 115 return getSuggestions().then((_) { | 122 return getSuggestions().then((_) { |
| 116 assertHasResult(CompletionSuggestionKind.CLASS, | 123 assertHasResult(CompletionSuggestionKind.CLASS, 'Object'); |
| 117 CompletionRelevance.DEFAULT, 'Object', false, false); | 124 assertHasResult(CompletionSuggestionKind.CLASS, 'HtmlElement'); |
| 118 assertHasResult(CompletionSuggestionKind.CLASS, | 125 assertNoResult('test'); |
| 119 CompletionRelevance.DEFAULT, 'HtmlElement', false, false); | 126 }); |
| 127 } |
| 128 |
| 129 test_suggestions_topLevel() { |
| 130 addTestFile(''' |
| 131 typedef foo(); |
| 132 var test^ = ''; |
| 133 main() {test.} |
| 134 '''); |
| 135 return getSuggestions().then((_) { |
| 136 assertHasResult(CompletionSuggestionKind.CLASS, 'Object'); |
| 137 assertHasResult(CompletionSuggestionKind.TOP_LEVEL_VARIABLE, 'test'); |
| 138 assertNoResult('HtmlElement'); |
| 120 }); | 139 }); |
| 121 } | 140 } |
| 122 } | 141 } |
| OLD | NEW |