| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library test.domain.analysis.abstract; |
| 6 |
| 7 import 'dart:async'; |
| 8 |
| 9 import 'package:analysis_server/src/analysis_server.dart'; |
| 10 import 'package:analysis_server/src/domain_analysis.dart'; |
| 11 import 'package:analysis_server/src/constants.dart'; |
| 12 import 'package:analysis_server/src/protocol.dart'; |
| 13 import 'package:analysis_server/src/resource.dart'; |
| 14 import 'package:analyzer/src/generated/source.dart'; |
| 15 import 'package:unittest/unittest.dart'; |
| 16 |
| 17 import 'mocks.dart'; |
| 18 |
| 19 |
| 20 /** |
| 21 * An abstract base for all 'analysis' domain tests. |
| 22 */ |
| 23 class AbstractAnalysisTest { |
| 24 MockServerChannel serverChannel; |
| 25 MemoryResourceProvider resourceProvider; |
| 26 AnalysisServer server; |
| 27 AnalysisDomainHandler handler; |
| 28 |
| 29 // Map<String, List<String>> analysisSubscriptions = {}; |
| 30 |
| 31 String projectPath = '/project'; |
| 32 String testFile = '/project/bin/test.dart'; |
| 33 String testCode; |
| 34 |
| 35 // Map<String, List<AnalysisError>> filesErrors = {}; |
| 36 // Map<String, List<Map<String, Object>>> filesHighlights = {}; |
| 37 // Map<String, List<Map<String, Object>>> filesNavigation = {}; |
| 38 |
| 39 |
| 40 AbstractAnalysisTest() { |
| 41 } |
| 42 |
| 43 void setUp() { |
| 44 serverChannel = new MockServerChannel(); |
| 45 resourceProvider = new MemoryResourceProvider(); |
| 46 server = new AnalysisServer(serverChannel, resourceProvider); |
| 47 handler = new AnalysisDomainHandler(server); |
| 48 // listen for notifications |
| 49 Stream<Notification> notificationStream = serverChannel.notificationControll
er.stream; |
| 50 notificationStream.listen((Notification notification) { |
| 51 processNotification(notification); |
| 52 }); |
| 53 // create an empty project |
| 54 _createEmptyProject(); |
| 55 } |
| 56 |
| 57 void tearDown() { |
| 58 server.done(); |
| 59 handler = null; |
| 60 server = null; |
| 61 resourceProvider = null; |
| 62 serverChannel = null; |
| 63 } |
| 64 |
| 65 void processNotification(Notification notification) { |
| 66 // if (notification.event == NOTIFICATION_ERRORS) { |
| 67 // String file = notification.getParameter(FILE); |
| 68 // List<Map<String, Object>> errorMaps = notification.getParameter(ERRORS); |
| 69 // filesErrors[file] = errorMaps.map(jsonToAnalysisError).toList(); |
| 70 // } |
| 71 // if (notification.event == NOTIFICATION_HIGHLIGHTS) { |
| 72 // String file = notification.getParameter(FILE); |
| 73 // filesHighlights[file] = notification.getParameter(REGIONS); |
| 74 // } |
| 75 // if (notification.event == NOTIFICATION_NAVIGATION) { |
| 76 // String file = notification.getParameter(FILE); |
| 77 // filesNavigation[file] = notification.getParameter(REGIONS); |
| 78 // } |
| 79 } |
| 80 |
| 81 /** |
| 82 * Returns a [Future] that completes when the [AnalysisServer] finishes |
| 83 * all its scheduled tasks. |
| 84 */ |
| 85 Future waitForTasksFinished() { |
| 86 return waitForServerOperationsPerformed(server); |
| 87 } |
| 88 |
| 89 /** |
| 90 * Returns the offset of [search] in [testCode]. |
| 91 * Fails if not found. |
| 92 */ |
| 93 int findFileOffset(String path, String search) { |
| 94 File file = resourceProvider.getResource(path) as File; |
| 95 String code = file.createSource(UriKind.FILE_URI).contents.data; |
| 96 int offset = code.indexOf(search); |
| 97 expect(offset, isNot(-1), reason: '"$search" in\n$code'); |
| 98 return offset; |
| 99 } |
| 100 |
| 101 /** |
| 102 * Returns the offset of [search] in [testCode]. |
| 103 * Fails if not found. |
| 104 */ |
| 105 int findOffset(String search) { |
| 106 int offset = testCode.indexOf(search); |
| 107 expect(offset, isNot(-1)); |
| 108 return offset; |
| 109 } |
| 110 |
| 111 // /** |
| 112 // * Returns [AnalysisError]s recorded for the given [file]. |
| 113 // * May be empty, but not `null`. |
| 114 // */ |
| 115 // List<AnalysisError> getErrors(String file) { |
| 116 // List<AnalysisError> errors = filesErrors[file]; |
| 117 // if (errors != null) { |
| 118 // return errors; |
| 119 // } |
| 120 // return <AnalysisError>[]; |
| 121 // } |
| 122 // |
| 123 // /** |
| 124 // * Returns highlights recorded for the given [file]. |
| 125 // * May be empty, but not `null`. |
| 126 // */ |
| 127 // List<Map<String, Object>> getHighlights(String file) { |
| 128 // List<Map<String, Object>> highlights = filesHighlights[file]; |
| 129 // if (highlights != null) { |
| 130 // return highlights; |
| 131 // } |
| 132 // return []; |
| 133 // } |
| 134 // |
| 135 // /** |
| 136 // * Returns navigation regions recorded for the given [file]. |
| 137 // * May be empty, but not `null`. |
| 138 // */ |
| 139 // List<Map<String, Object>> getNavigation(String file) { |
| 140 // List<Map<String, Object>> navigation = filesNavigation[file]; |
| 141 // if (navigation != null) { |
| 142 // return navigation; |
| 143 // } |
| 144 // return []; |
| 145 // } |
| 146 // |
| 147 // /** |
| 148 // * Returns [AnalysisError]s recorded for the [testFile]. |
| 149 // * May be empty, but not `null`. |
| 150 // */ |
| 151 // List<AnalysisError> getTestErrors() { |
| 152 // return getErrors(testFile); |
| 153 // } |
| 154 // |
| 155 // /** |
| 156 // * Returns highlights recorded for the given [testFile]. |
| 157 // * May be empty, but not `null`. |
| 158 // */ |
| 159 // List<Map<String, Object>> getTestHighlights() { |
| 160 // return getHighlights(testFile); |
| 161 // } |
| 162 // |
| 163 // /** |
| 164 // * Returns navigation information recorded for the given [testFile]. |
| 165 // * May be empty, but not `null`. |
| 166 // */ |
| 167 // List<Map<String, Object>> getTestNavigation() { |
| 168 // return getNavigation(testFile); |
| 169 // } |
| 170 |
| 171 /** |
| 172 * Creates an empty project `/project/`. |
| 173 */ |
| 174 void _createEmptyProject() { |
| 175 resourceProvider.newFolder(projectPath); |
| 176 Request request = new Request('0', METHOD_SET_ANALYSIS_ROOTS); |
| 177 request.setParameter(INCLUDED, [projectPath]); |
| 178 request.setParameter(EXCLUDED, []); |
| 179 handleSuccessfulRequest(request); |
| 180 } |
| 181 |
| 182 // /** |
| 183 // * Creates a project with a single Dart file `/project/bin/test.dart` with |
| 184 // * the given [code]. |
| 185 // */ |
| 186 // void createSingleFileProject(code) { |
| 187 // this.testCode = _getCodeString(code); |
| 188 // resourceProvider.newFolder('/project'); |
| 189 // resourceProvider.newFile(testFile, testCode); |
| 190 // Request request = new Request('0', METHOD_SET_ANALYSIS_ROOTS); |
| 191 // request.setParameter(INCLUDED, ['/project']); |
| 192 // request.setParameter(EXCLUDED, []); |
| 193 // handleSuccessfulRequest(request); |
| 194 // } |
| 195 |
| 196 String addFile(String path, String content) { |
| 197 resourceProvider.newFile(path, content); |
| 198 return path; |
| 199 } |
| 200 |
| 201 String addTestFile(String content) { |
| 202 addFile(testFile, content); |
| 203 this.testCode = content; |
| 204 return testFile; |
| 205 } |
| 206 |
| 207 /** |
| 208 * Validates that the given [request] is handled successfully. |
| 209 */ |
| 210 void handleSuccessfulRequest(Request request) { |
| 211 Response response = handler.handleRequest(request); |
| 212 expect(response, isResponseSuccess('0')); |
| 213 } |
| 214 |
| 215 static String _getCodeString(code) { |
| 216 if (code is List<String>) { |
| 217 code = code.join('\n'); |
| 218 } |
| 219 return code as String; |
| 220 } |
| 221 } |
| 222 |
| 223 |
| 224 |
| 225 class AnalysisError { |
| 226 final String file; |
| 227 final String errorCode; |
| 228 final int offset; |
| 229 final int length; |
| 230 final String message; |
| 231 final String correction; |
| 232 AnalysisError(this.file, this.errorCode, this.offset, this.length, |
| 233 this.message, this.correction); |
| 234 |
| 235 @override |
| 236 String toString() { |
| 237 return 'NotificationError(file=$file; errorCode=$errorCode; ' |
| 238 'offset=$offset; length=$length; message=$message)'; |
| 239 } |
| 240 } |
| 241 |
| 242 |
| 243 AnalysisError _jsonToAnalysisError(Map<String, Object> json) { |
| 244 return new AnalysisError( |
| 245 json['file'], |
| 246 json['errorCode'], |
| 247 json['offset'], |
| 248 json['length'], |
| 249 json['message'], |
| 250 json['correction']); |
| 251 } |
| 252 |
| 253 |
| 254 int findIdentifierLength(String search) { |
| 255 int length = 0; |
| 256 while (length < search.length) { |
| 257 int c = search.codeUnitAt(length); |
| 258 if (!(c >= 'a'.codeUnitAt(0) && c <= 'z'.codeUnitAt(0) || |
| 259 c >= 'A'.codeUnitAt(0) && c <= 'Z'.codeUnitAt(0) || |
| 260 c >= '0'.codeUnitAt(0) && c <= '9'.codeUnitAt(0))) { |
| 261 break; |
| 262 } |
| 263 length++; |
| 264 } |
| 265 return length; |
| 266 } |
| OLD | NEW |