| 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.analysis; | 5 library test.domain.analysis; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'package:analysis_server/src/analysis_server.dart'; | 9 import 'package:analysis_server/src/analysis_server.dart'; |
| 10 import 'package:analysis_server/src/constants.dart'; | 10 import 'package:analysis_server/src/constants.dart'; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 | 27 |
| 28 setUp(() { | 28 setUp(() { |
| 29 serverChannel = new MockServerChannel(); | 29 serverChannel = new MockServerChannel(); |
| 30 server = new AnalysisServer(serverChannel, resourceProvider); | 30 server = new AnalysisServer(serverChannel, resourceProvider); |
| 31 handler = new AnalysisDomainHandler(server); | 31 handler = new AnalysisDomainHandler(server); |
| 32 }); | 32 }); |
| 33 | 33 |
| 34 group('notification.errors', testNotificationErrors); | 34 group('notification.errors', testNotificationErrors); |
| 35 group('notification.highlights', testNotificationHighlights); | 35 group('notification.highlights', testNotificationHighlights); |
| 36 group('updateContent', testUpdateContent); | 36 group('updateContent', testUpdateContent); |
| 37 group('setSubscriptions', test_setSubscriptions); |
| 37 | 38 |
| 38 group('AnalysisDomainHandler', () { | 39 group('AnalysisDomainHandler', () { |
| 39 test('getFixes', () { | 40 test('getFixes', () { |
| 40 var request = new Request('0', METHOD_GET_FIXES); | 41 var request = new Request('0', METHOD_GET_FIXES); |
| 41 request.setParameter(ERRORS, []); | 42 request.setParameter(ERRORS, []); |
| 42 var response = handler.handleRequest(request); | 43 var response = handler.handleRequest(request); |
| 43 // TODO(scheglov) implement | 44 // TODO(scheglov) implement |
| 44 expect(response, isNull); | 45 expect(response, isNull); |
| 45 }); | 46 }); |
| 46 | 47 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 test('setPriorityFiles', () { | 94 test('setPriorityFiles', () { |
| 94 var request = new Request('0', METHOD_SET_PRIORITY_FILES); | 95 var request = new Request('0', METHOD_SET_PRIORITY_FILES); |
| 95 request.setParameter( | 96 request.setParameter( |
| 96 FILES, | 97 FILES, |
| 97 ['projectA/aa.dart', 'projectB/ba.dart']); | 98 ['projectA/aa.dart', 'projectB/ba.dart']); |
| 98 var response = handler.handleRequest(request); | 99 var response = handler.handleRequest(request); |
| 99 // TODO(scheglov) implement | 100 // TODO(scheglov) implement |
| 100 expect(response, isNull); | 101 expect(response, isNull); |
| 101 }); | 102 }); |
| 102 | 103 |
| 103 test('setSubscriptions', () { | |
| 104 var request = new Request('0', METHOD_SET_SUBSCRIPTIONS); | |
| 105 request.setParameter( | |
| 106 SUBSCRIPTIONS, | |
| 107 { | |
| 108 AnalysisService.HIGHLIGHTS : ['project/a.dart', 'project/b.dart'], | |
| 109 AnalysisService.NAVIGATION : ['project/c.dart'], | |
| 110 AnalysisService.OUTLINE : ['project/d.dart', 'project/e.dart'] | |
| 111 }); | |
| 112 var response = handler.handleRequest(request); | |
| 113 // TODO(scheglov) implement | |
| 114 expect(response, isNull); | |
| 115 }); | |
| 116 | |
| 117 test('updateOptions', () { | 104 test('updateOptions', () { |
| 118 var request = new Request('0', METHOD_UPDATE_OPTIONS); | 105 var request = new Request('0', METHOD_UPDATE_OPTIONS); |
| 119 request.setParameter( | 106 request.setParameter( |
| 120 OPTIONS, | 107 OPTIONS, |
| 121 { | 108 { |
| 122 'analyzeAngular' : true, | 109 'analyzeAngular' : true, |
| 123 'enableDeferredLoading': true, | 110 'enableDeferredLoading': true, |
| 124 'enableEnums': false | 111 'enableEnums': false |
| 125 }); | 112 }); |
| 126 var response = handler.handleRequest(request); | 113 var response = handler.handleRequest(request); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 | 163 |
| 177 /** | 164 /** |
| 178 * A helper to test 'analysis.*' requests. | 165 * A helper to test 'analysis.*' requests. |
| 179 */ | 166 */ |
| 180 class AnalysisTestHelper { | 167 class AnalysisTestHelper { |
| 181 MockServerChannel serverChannel; | 168 MockServerChannel serverChannel; |
| 182 MemoryResourceProvider resourceProvider; | 169 MemoryResourceProvider resourceProvider; |
| 183 AnalysisServer server; | 170 AnalysisServer server; |
| 184 AnalysisDomainHandler handler; | 171 AnalysisDomainHandler handler; |
| 185 | 172 |
| 173 Map<String, List<String>> analysisSubscriptions = {}; |
| 174 |
| 186 Map<String, List<AnalysisError>> filesErrors = {}; | 175 Map<String, List<AnalysisError>> filesErrors = {}; |
| 187 Map<String, List<Map<String, Object>>> filesHighlights = {}; | 176 Map<String, List<Map<String, Object>>> filesHighlights = {}; |
| 188 | 177 |
| 189 String testFile = '/project/bin/test.dart'; | 178 String testFile = '/project/bin/test.dart'; |
| 190 String testCode; | 179 String testCode; |
| 191 | 180 |
| 192 AnalysisTestHelper() { | 181 AnalysisTestHelper() { |
| 193 serverChannel = new MockServerChannel(); | 182 serverChannel = new MockServerChannel(); |
| 194 resourceProvider = new MemoryResourceProvider(); | 183 resourceProvider = new MemoryResourceProvider(); |
| 195 server = new AnalysisServer(serverChannel, resourceProvider); | 184 server = new AnalysisServer(serverChannel, resourceProvider); |
| 196 handler = new AnalysisDomainHandler(server); | 185 handler = new AnalysisDomainHandler(server); |
| 197 // listen for notifications | 186 // listen for notifications |
| 198 Stream<Notification> notificationStream = serverChannel.notificationControll
er.stream; | 187 Stream<Notification> notificationStream = serverChannel.notificationControll
er.stream; |
| 199 notificationStream.listen((Notification notification) { | 188 notificationStream.listen((Notification notification) { |
| 200 if (notification.event == NOTIFICATION_ERRORS) { | 189 if (notification.event == NOTIFICATION_ERRORS) { |
| 201 String file = notification.getParameter(FILE); | 190 String file = notification.getParameter(FILE); |
| 202 List<Map<String, Object>> errorMaps = notification.getParameter(ERRORS); | 191 List<Map<String, Object>> errorMaps = notification.getParameter(ERRORS); |
| 203 filesErrors[file] = errorMaps.map(jsonToAnalysisError).toList(); | 192 filesErrors[file] = errorMaps.map(jsonToAnalysisError).toList(); |
| 204 } | 193 } |
| 205 if (notification.event == NOTIFICATION_HIGHLIGHTS) { | 194 if (notification.event == NOTIFICATION_HIGHLIGHTS) { |
| 206 String file = notification.getParameter(FILE); | 195 String file = notification.getParameter(FILE); |
| 207 filesHighlights[file] = notification.getParameter(REGIONS); | 196 filesHighlights[file] = notification.getParameter(REGIONS); |
| 208 } | 197 } |
| 209 }); | 198 }); |
| 210 } | 199 } |
| 211 | 200 |
| 201 void addAnalysisSubscriptionHighlight(String file) { |
| 202 var service = AnalysisService.HIGHLIGHTS; |
| 203 // add file to subscription |
| 204 var files = analysisSubscriptions[service.name]; |
| 205 if (files == null) { |
| 206 files = <String>[]; |
| 207 analysisSubscriptions[service.name] = files; |
| 208 } |
| 209 files.add(file); |
| 210 // set subscriptions |
| 211 Request request = new Request('0', METHOD_SET_SUBSCRIPTIONS); |
| 212 request.setParameter(SUBSCRIPTIONS, analysisSubscriptions); |
| 213 handleSuccessfulRequest(request); |
| 214 } |
| 215 |
| 212 /** | 216 /** |
| 213 * Returns a [Future] that completes when this this helper finished all its | 217 * Returns a [Future] that completes when this this helper finished all its |
| 214 * scheduled tasks. | 218 * scheduled tasks. |
| 215 */ | 219 */ |
| 216 Future waitForTasksFinished() { | 220 Future waitForTasksFinished() { |
| 217 return waitForServerTasksFinished(server); | 221 return waitForServerTasksFinished(server); |
| 218 } | 222 } |
| 219 | 223 |
| 220 /** | 224 /** |
| 221 * Returns [AnalysisError]s recorded for the given [file]. | 225 * Returns [AnalysisError]s recorded for the given [file]. |
| (...skipping 29 matching lines...) Expand all Loading... |
| 251 | 255 |
| 252 /** | 256 /** |
| 253 * Returns highlights recorded for the given [testFile]. | 257 * Returns highlights recorded for the given [testFile]. |
| 254 * May be empty, but not `null`. | 258 * May be empty, but not `null`. |
| 255 */ | 259 */ |
| 256 List<Map<String, Object>> getTestHighlights() { | 260 List<Map<String, Object>> getTestHighlights() { |
| 257 return getHighlights(testFile); | 261 return getHighlights(testFile); |
| 258 } | 262 } |
| 259 | 263 |
| 260 /** | 264 /** |
| 265 * Creates an empty project `/project/`. |
| 266 */ |
| 267 void createEmptyProject() { |
| 268 resourceProvider.newFolder('/project'); |
| 269 Request request = new Request('0', METHOD_SET_ANALYSIS_ROOTS); |
| 270 request.setParameter(INCLUDED, ['/project']); |
| 271 request.setParameter(EXCLUDED, []); |
| 272 handleSuccessfulRequest(request); |
| 273 } |
| 274 |
| 275 /** |
| 261 * Creates a project with a single Dart file `/project/bin/test.dart` with | 276 * Creates a project with a single Dart file `/project/bin/test.dart` with |
| 262 * the given [code]. | 277 * the given [code]. |
| 263 */ | 278 */ |
| 264 void createSingleFileProject(code) { | 279 void createSingleFileProject(code) { |
| 265 this.testCode = _getCodeString(code); | 280 this.testCode = _getCodeString(code); |
| 266 resourceProvider.newFolder('/project'); | 281 resourceProvider.newFolder('/project'); |
| 267 resourceProvider.newFile('/project/pubspec.yaml', 'name: project'); | |
| 268 resourceProvider.newFile(testFile, testCode); | 282 resourceProvider.newFile(testFile, testCode); |
| 269 Request request = new Request('0', METHOD_SET_ANALYSIS_ROOTS); | 283 Request request = new Request('0', METHOD_SET_ANALYSIS_ROOTS); |
| 270 request.setParameter(INCLUDED, ['/project']); | 284 request.setParameter(INCLUDED, ['/project']); |
| 271 request.setParameter(EXCLUDED, []); | 285 request.setParameter(EXCLUDED, []); |
| 272 handleSuccessfulRequest(request); | 286 handleSuccessfulRequest(request); |
| 273 } | 287 } |
| 274 | 288 |
| 275 void setFileContent(String path, String content) { | 289 void setFileContent(String path, String content) { |
| 276 resourceProvider.newFile(path, content); | 290 resourceProvider.newFile(path, content); |
| 277 } | 291 } |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 333 expect(error.errorCode, 'StaticWarningCode.UNDEFINED_IDENTIFIER'); | 347 expect(error.errorCode, 'StaticWarningCode.UNDEFINED_IDENTIFIER'); |
| 334 }); | 348 }); |
| 335 }); | 349 }); |
| 336 } | 350 } |
| 337 | 351 |
| 338 | 352 |
| 339 class NotificationHighlightHelper extends AnalysisTestHelper { | 353 class NotificationHighlightHelper extends AnalysisTestHelper { |
| 340 List<Map<String, Object>> regions; | 354 List<Map<String, Object>> regions; |
| 341 | 355 |
| 342 Future prepareRegions(then()) { | 356 Future prepareRegions(then()) { |
| 357 addAnalysisSubscriptionHighlight(testFile); |
| 343 return waitForTasksFinished().then((_) { | 358 return waitForTasksFinished().then((_) { |
| 344 regions = getTestHighlights(); | 359 regions = getTestHighlights(); |
| 345 then(); | 360 then(); |
| 346 }); | 361 }); |
| 347 } | 362 } |
| 348 | 363 |
| 349 void assertHasRawRegion(HighlightType type, int offset, int length) { | 364 void assertHasRawRegion(HighlightType type, int offset, int length) { |
| 350 for (Map<String, Object> region in regions) { | 365 for (Map<String, Object> region in regions) { |
| 351 if (region['offset'] == offset && region['length'] == length | 366 if (region['offset'] == offset && region['length'] == length |
| 352 && region['type'] == type.name) { | 367 && region['type'] == type.name) { |
| (...skipping 765 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1118 helper.handleSuccessfulRequest(request); | 1133 helper.handleSuccessfulRequest(request); |
| 1119 } | 1134 } |
| 1120 // wait, there is an error | 1135 // wait, there is an error |
| 1121 helper.waitForTasksFinished().then((_) { | 1136 helper.waitForTasksFinished().then((_) { |
| 1122 List<AnalysisError> errors = helper.getTestErrors(); | 1137 List<AnalysisError> errors = helper.getTestErrors(); |
| 1123 expect(errors, hasLength(1)); | 1138 expect(errors, hasLength(1)); |
| 1124 }); | 1139 }); |
| 1125 }); | 1140 }); |
| 1126 }); | 1141 }); |
| 1127 } | 1142 } |
| 1143 |
| 1144 |
| 1145 void test_setSubscriptions() { |
| 1146 test('before analysis', () { |
| 1147 AnalysisTestHelper helper = new AnalysisTestHelper(); |
| 1148 // subscribe |
| 1149 { |
| 1150 var request = new Request('0', METHOD_SET_SUBSCRIPTIONS); |
| 1151 request.setParameter( |
| 1152 SUBSCRIPTIONS, |
| 1153 { |
| 1154 AnalysisService.HIGHLIGHTS.name : [helper.testFile], |
| 1155 }); |
| 1156 helper.handleSuccessfulRequest(request); |
| 1157 } |
| 1158 // create project |
| 1159 helper.createSingleFileProject('int V = 42;'); |
| 1160 // wait, there are highlight regions |
| 1161 helper.waitForTasksFinished().then((_) { |
| 1162 var highlights = helper.getHighlights(helper.testFile); |
| 1163 expect(highlights, isNot(isEmpty)); |
| 1164 }); |
| 1165 }); |
| 1166 |
| 1167 test('after analysis', () { |
| 1168 AnalysisTestHelper helper = new AnalysisTestHelper(); |
| 1169 // create project |
| 1170 helper.createSingleFileProject('int V = 42;'); |
| 1171 // wait, no regions initially |
| 1172 return helper.waitForTasksFinished().then((_) { |
| 1173 var highlights = helper.getHighlights(helper.testFile); |
| 1174 expect(highlights, isEmpty); |
| 1175 // subscribe |
| 1176 { |
| 1177 var request = new Request('0', METHOD_SET_SUBSCRIPTIONS); |
| 1178 request.setParameter( |
| 1179 SUBSCRIPTIONS, |
| 1180 { |
| 1181 AnalysisService.HIGHLIGHTS.name : [helper.testFile], |
| 1182 }); |
| 1183 helper.handleSuccessfulRequest(request); |
| 1184 } |
| 1185 // wait, has regions |
| 1186 return helper.waitForTasksFinished().then((_) { |
| 1187 var highlights = helper.getHighlights(helper.testFile); |
| 1188 expect(highlights, isNot(isEmpty)); |
| 1189 }); |
| 1190 }); |
| 1191 }); |
| 1192 } |
| OLD | NEW |