| 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/index/index.dart' show Index; | 12 import 'package:analysis_services/index/index.dart' show Index; |
| 13 import 'package:analysis_services/index/local_memory_index.dart'; | 13 import 'package:analysis_services/index/local_memory_index.dart'; |
| 14 import 'package:analysis_testing/reflective_tests.dart'; | 14 import 'package:analysis_testing/reflective_tests.dart'; |
| 15 import 'package:unittest/unittest.dart'; | 15 import 'package:unittest/unittest.dart'; |
| 16 | 16 |
| 17 import 'analysis_abstract.dart'; | 17 import 'analysis_abstract.dart'; |
| 18 | 18 |
| 19 main() { | 19 main() { |
| 20 group('completion', () { | 20 group('completion', () { |
| 21 runReflectiveTests(CompletionTest); | 21 runReflectiveTests(CompletionTest); |
| 22 }); | 22 }); |
| 23 } | 23 } |
| 24 | 24 |
| 25 @ReflectiveTestCase() | 25 @ReflectiveTestCase() |
| 26 class CompletionTest extends AbstractAnalysisTest { | 26 class CompletionTest extends AbstractAnalysisTest { |
| 27 String completionId; | 27 String completionId; |
| 28 int completionOffset; |
| 28 List<CompletionSuggestion> suggestions = []; | 29 List<CompletionSuggestion> suggestions = []; |
| 29 bool suggestionsDone = false; | 30 bool suggestionsDone = false; |
| 30 | 31 |
| 32 String addTestFile(String content) { |
| 33 completionOffset = content.indexOf('^'); |
| 34 expect(completionOffset, isNot(equals(-1)), reason: 'missing ^'); |
| 35 int nextOffset = content.indexOf('^', completionOffset + 1); |
| 36 expect(nextOffset, equals(-1), reason: 'too many ^'); |
| 37 return super.addTestFile( |
| 38 content.substring(0, completionOffset) |
| 39 + content.substring(completionOffset + 1)); |
| 40 } |
| 41 |
| 31 void assertHasResult(String completion) { | 42 void assertHasResult(String completion) { |
| 32 var cs = suggestions.firstWhere((cs) => cs.completion == completion, orElse:
() { | 43 var cs = suggestions.firstWhere((cs) => cs.completion == completion, orElse:
() { |
| 33 var completions = suggestions.map((s) => s.completion).toList(); | 44 var completions = suggestions.map((s) => s.completion).toList(); |
| 34 fail('expected "$completion" but found\n $completions'); | 45 fail('expected "$completion" but found\n $completions'); |
| 35 }); | 46 }); |
| 36 } | 47 } |
| 37 | 48 |
| 38 void assertValidId(String id) { | 49 void assertValidId(String id) { |
| 39 expect(id, isNotNull); | 50 expect(id, isNotNull); |
| 40 expect(id.isNotEmpty, isTrue); | 51 expect(id.isNotEmpty, isTrue); |
| 41 } | 52 } |
| 42 | 53 |
| 43 @override | 54 @override |
| 44 Index createIndex() { | 55 Index createIndex() { |
| 45 return createLocalMemoryIndex(); | 56 return createLocalMemoryIndex(); |
| 46 } | 57 } |
| 47 | 58 |
| 48 Future getSuggestions(String pattern, int offsetFromPatternStart) { | 59 Future getSuggestions() { |
| 49 return waitForTasksFinished().then((_) { | 60 return waitForTasksFinished().then((_) { |
| 50 int offset = testCode.indexOf(pattern) + offsetFromPatternStart; | |
| 51 Request request = new Request('0', COMPLETION_GET_SUGGESTIONS); | 61 Request request = new Request('0', COMPLETION_GET_SUGGESTIONS); |
| 52 request.setParameter(FILE, testFile); | 62 request.setParameter(FILE, testFile); |
| 53 request.setParameter(OFFSET, offset); | 63 request.setParameter(OFFSET, completionOffset); |
| 54 Response response = handleSuccessfulRequest(request); | 64 Response response = handleSuccessfulRequest(request); |
| 55 completionId = response.getResult(ID); | 65 completionId = response.getResult(ID); |
| 56 assertValidId(completionId); | 66 assertValidId(completionId); |
| 57 return waitForSuggestions(); | 67 return waitForSuggestions(); |
| 58 }); | 68 }); |
| 59 } | 69 } |
| 60 | 70 |
| 61 void processNotification(Notification notification) { | 71 void processNotification(Notification notification) { |
| 62 if (notification.event == COMPLETION_RESULTS) { | 72 if (notification.event == COMPLETION_RESULTS) { |
| 63 String id = notification.getParameter(ID); | 73 String id = notification.getParameter(ID); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 84 Future waitForSuggestions() { | 94 Future waitForSuggestions() { |
| 85 if (suggestionsDone) { | 95 if (suggestionsDone) { |
| 86 return new Future.value(); | 96 return new Future.value(); |
| 87 } | 97 } |
| 88 return new Future.delayed(Duration.ZERO, waitForSuggestions); | 98 return new Future.delayed(Duration.ZERO, waitForSuggestions); |
| 89 } | 99 } |
| 90 | 100 |
| 91 test_suggestions() { | 101 test_suggestions() { |
| 92 addTestFile(''' | 102 addTestFile(''' |
| 93 import 'dart:html'; | 103 import 'dart:html'; |
| 94 main() {} | 104 main() {^} |
| 95 '''); | 105 '''); |
| 96 return getSuggestions('}', 0).then((_) { | 106 return getSuggestions().then((_) { |
| 97 assertHasResult('Object'); | 107 assertHasResult('Object'); |
| 98 assertHasResult('HtmlElement'); | 108 assertHasResult('HtmlElement'); |
| 99 }); | 109 }); |
| 100 } | 110 } |
| 101 } | 111 } |
| OLD | NEW |