| 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 library test.analysis.updateContent; | 5 library test.analysis.updateContent; |
| 6 | 6 |
| 7 import 'package:analysis_server/src/constants.dart'; | 7 import 'package:analysis_server/src/constants.dart'; |
| 8 import 'package:analysis_server/src/protocol.dart'; | 8 import 'package:analysis_server/src/protocol.dart'; |
| 9 import 'package:analysis_server/src/services/index/index.dart'; |
| 10 import 'package:analyzer/src/generated/ast.dart'; |
| 11 import 'package:typed_mock/typed_mock.dart'; |
| 9 import 'package:unittest/unittest.dart'; | 12 import 'package:unittest/unittest.dart'; |
| 10 | 13 |
| 11 import '../analysis_abstract.dart'; | 14 import '../analysis_abstract.dart'; |
| 12 import '../reflective_tests.dart'; | 15 import '../reflective_tests.dart'; |
| 13 | 16 |
| 14 | 17 |
| 15 main() { | 18 main() { |
| 16 groupSep = ' | '; | 19 groupSep = ' | '; |
| 17 runReflectiveTests(UpdateContentTest); | 20 runReflectiveTests(UpdateContentTest); |
| 18 } | 21 } |
| 19 | 22 |
| 20 | 23 |
| 24 compilationUnitMatcher(String file) { |
| 25 return new _ArgumentMatcher_CompilationUnit(file); |
| 26 } |
| 27 |
| 28 |
| 21 @reflectiveTest | 29 @reflectiveTest |
| 22 class UpdateContentTest extends AbstractAnalysisTest { | 30 class UpdateContentTest extends AbstractAnalysisTest { |
| 23 Map<String, List<AnalysisError>> filesErrors = {}; | 31 Map<String, List<AnalysisError>> filesErrors = {}; |
| 24 int serverErrorCount = 0; | 32 int serverErrorCount = 0; |
| 25 int navigationCount = 0; | 33 int navigationCount = 0; |
| 26 | 34 |
| 35 Index createIndex() { |
| 36 return new _MockIndex(); |
| 37 } |
| 38 |
| 27 @override | 39 @override |
| 28 void processNotification(Notification notification) { | 40 void processNotification(Notification notification) { |
| 29 if (notification.event == ANALYSIS_ERRORS) { | 41 if (notification.event == ANALYSIS_ERRORS) { |
| 30 var decoded = new AnalysisErrorsParams.fromNotification(notification); | 42 var decoded = new AnalysisErrorsParams.fromNotification(notification); |
| 31 filesErrors[decoded.file] = decoded.errors; | 43 filesErrors[decoded.file] = decoded.errors; |
| 32 } | 44 } |
| 33 if (notification.event == ANALYSIS_NAVIGATION) { | 45 if (notification.event == ANALYSIS_NAVIGATION) { |
| 34 navigationCount++; | 46 navigationCount++; |
| 35 } | 47 } |
| 36 if (notification.event == SERVER_ERROR) { | 48 if (notification.event == SERVER_ERROR) { |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 server.updateContent(id, { | 85 server.updateContent(id, { |
| 74 testFile: new ChangeContentOverlay([new SourceEdit(8, 3, 'bar')]) | 86 testFile: new ChangeContentOverlay([new SourceEdit(8, 3, 'bar')]) |
| 75 }); | 87 }); |
| 76 fail('Expected an exception to be thrown'); | 88 fail('Expected an exception to be thrown'); |
| 77 } on RequestFailure catch (e) { | 89 } on RequestFailure catch (e) { |
| 78 expect(e.response.id, id); | 90 expect(e.response.id, id); |
| 79 expect(e.response.error.code, RequestErrorCode.INVALID_OVERLAY_CHANGE); | 91 expect(e.response.error.code, RequestErrorCode.INVALID_OVERLAY_CHANGE); |
| 80 } | 92 } |
| 81 } | 93 } |
| 82 | 94 |
| 95 test_indexUnitAfterNopChange() async { |
| 96 var testUnitMatcher = compilationUnitMatcher(testFile) as dynamic; |
| 97 createProject(); |
| 98 addTestFile('main() { print(1); }'); |
| 99 await server.onAnalysisComplete; |
| 100 verify(server.index.indexUnit(anyObject, testUnitMatcher)).times(1); |
| 101 // add an overlay |
| 102 server.updateContent('1', { |
| 103 testFile: new AddContentOverlay('main() { print(2); }') |
| 104 }); |
| 105 // Perform a single operation: analysis. |
| 106 // It will schedule an indexing operation. |
| 107 server.performOperation(); |
| 108 // Update the file and remove an overlay. |
| 109 resourceProvider.updateFile(testFile, 'main() { print(2); }'); |
| 110 server.updateContent('2', { |
| 111 testFile: new RemoveContentOverlay() |
| 112 }); |
| 113 // Validate that at the end the unit was indexed. |
| 114 await server.onAnalysisComplete; |
| 115 verify(server.index.indexUnit(anyObject, testUnitMatcher)).times(2); |
| 116 } |
| 117 |
| 83 test_multiple_contexts() { | 118 test_multiple_contexts() { |
| 84 String fooPath = '/project1/foo.dart'; | 119 String fooPath = '/project1/foo.dart'; |
| 85 resourceProvider.newFile(fooPath, ''' | 120 resourceProvider.newFile(fooPath, ''' |
| 86 library foo; | 121 library foo; |
| 87 import '../project2/baz.dart'; | 122 import '../project2/baz.dart'; |
| 88 main() { f(); }'''); | 123 main() { f(); }'''); |
| 89 String barPath = '/project2/bar.dart'; | 124 String barPath = '/project2/bar.dart'; |
| 90 resourceProvider.newFile(barPath, ''' | 125 resourceProvider.newFile(barPath, ''' |
| 91 library bar; | 126 library bar; |
| 92 import 'baz.dart'; | 127 import 'baz.dart'; |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 filesErrors.clear(); | 188 filesErrors.clear(); |
| 154 server.test_flushResolvedUnit(testFile); | 189 server.test_flushResolvedUnit(testFile); |
| 155 server.updateContent('2', { | 190 server.updateContent('2', { |
| 156 testFile: new ChangeContentOverlay([new SourceEdit(0, 4, 'main')]) | 191 testFile: new ChangeContentOverlay([new SourceEdit(0, 4, 'main')]) |
| 157 }); | 192 }); |
| 158 await server.onAnalysisComplete; | 193 await server.onAnalysisComplete; |
| 159 // errors should have been resent | 194 // errors should have been resent |
| 160 expect(filesErrors, isNotEmpty); | 195 expect(filesErrors, isNotEmpty); |
| 161 } | 196 } |
| 162 } | 197 } |
| 198 |
| 199 |
| 200 class _ArgumentMatcher_CompilationUnit extends ArgumentMatcher { |
| 201 final String file; |
| 202 |
| 203 _ArgumentMatcher_CompilationUnit(this.file); |
| 204 |
| 205 @override |
| 206 bool matches(arg) { |
| 207 return arg is CompilationUnit && arg.element.source.fullName == file; |
| 208 } |
| 209 } |
| 210 |
| 211 |
| 212 class _MockIndex extends TypedMock implements Index { |
| 213 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); |
| 214 } |
| OLD | NEW |