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 mocks; | 5 library mocks; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:io'; | 8 import 'dart:io'; |
9 @MirrorsUsed(targets: 'mocks', override: '*') | 9 @MirrorsUsed(targets: 'mocks', override: '*') |
10 import 'dart:mirrors'; | 10 import 'dart:mirrors'; |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 // Future.value or Future() constructors use scheduleMicrotask themselves and | 65 // Future.value or Future() constructors use scheduleMicrotask themselves and |
66 // would therefore not wait for microtask callbacks that are scheduled after | 66 // would therefore not wait for microtask callbacks that are scheduled after |
67 // invoking this method. | 67 // invoking this method. |
68 return new Future.delayed(Duration.ZERO, () => pumpEventQueue(times - 1)); | 68 return new Future.delayed(Duration.ZERO, () => pumpEventQueue(times - 1)); |
69 } | 69 } |
70 | 70 |
71 typedef void MockServerOperationPerformFunction(AnalysisServer server); | 71 typedef void MockServerOperationPerformFunction(AnalysisServer server); |
72 | 72 |
73 class MockAnalysisContext extends StringTypedMock implements AnalysisContext { | 73 class MockAnalysisContext extends StringTypedMock implements AnalysisContext { |
74 MockAnalysisContext(String name) : super(name); | 74 MockAnalysisContext(String name) : super(name); |
75 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); | |
76 } | 75 } |
77 | 76 |
78 class MockClassElement extends TypedMock implements ClassElement { | 77 class MockClassElement extends TypedMock implements ClassElement { |
79 final ElementKind kind = ElementKind.CLASS; | 78 final ElementKind kind = ElementKind.CLASS; |
80 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); | |
81 } | 79 } |
82 | 80 |
83 class MockCompilationUnitElement extends TypedMock | 81 class MockCompilationUnitElement extends TypedMock |
84 implements CompilationUnitElement { | 82 implements CompilationUnitElement { |
85 final ElementKind kind = ElementKind.COMPILATION_UNIT; | 83 final ElementKind kind = ElementKind.COMPILATION_UNIT; |
86 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); | |
87 } | 84 } |
88 | 85 |
89 class MockConstructorElement extends TypedMock implements ConstructorElement { | 86 class MockConstructorElement extends TypedMock implements ConstructorElement { |
90 final kind = ElementKind.CONSTRUCTOR; | 87 final kind = ElementKind.CONSTRUCTOR; |
91 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); | |
92 } | 88 } |
93 | 89 |
94 class MockElement extends StringTypedMock implements Element { | 90 class MockElement extends StringTypedMock implements Element { |
95 MockElement([String name = '<element>']) : super(name); | 91 MockElement([String name = '<element>']) : super(name); |
96 | 92 |
97 @override | 93 @override |
98 String get displayName => _toString; | 94 String get displayName => _toString; |
99 | 95 |
100 @override | 96 @override |
101 String get name => _toString; | 97 String get name => _toString; |
102 | |
103 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); | |
104 } | 98 } |
105 | 99 |
106 class MockFieldElement extends TypedMock implements FieldElement { | 100 class MockFieldElement extends TypedMock implements FieldElement { |
107 final ElementKind kind = ElementKind.FIELD; | 101 final ElementKind kind = ElementKind.FIELD; |
108 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); | |
109 } | 102 } |
110 | 103 |
111 class MockFunctionElement extends TypedMock implements FunctionElement { | 104 class MockFunctionElement extends TypedMock implements FunctionElement { |
112 final ElementKind kind = ElementKind.FUNCTION; | 105 final ElementKind kind = ElementKind.FUNCTION; |
113 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); | |
114 } | 106 } |
115 | 107 |
116 class MockFunctionTypeAliasElement extends TypedMock | 108 class MockFunctionTypeAliasElement extends TypedMock |
117 implements FunctionTypeAliasElement { | 109 implements FunctionTypeAliasElement { |
118 final ElementKind kind = ElementKind.FUNCTION_TYPE_ALIAS; | 110 final ElementKind kind = ElementKind.FUNCTION_TYPE_ALIAS; |
119 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); | |
120 } | 111 } |
121 | 112 |
122 class MockImportElement extends TypedMock implements ImportElement { | 113 class MockImportElement extends TypedMock implements ImportElement { |
123 final ElementKind kind = ElementKind.IMPORT; | 114 final ElementKind kind = ElementKind.IMPORT; |
124 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); | |
125 } | 115 } |
126 | 116 |
127 class MockLibraryElement extends TypedMock implements LibraryElement { | 117 class MockLibraryElement extends TypedMock implements LibraryElement { |
128 final ElementKind kind = ElementKind.LIBRARY; | 118 final ElementKind kind = ElementKind.LIBRARY; |
129 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); | |
130 } | 119 } |
131 | 120 |
132 class MockLocalVariableElement extends TypedMock | 121 class MockLocalVariableElement extends TypedMock |
133 implements LocalVariableElement { | 122 implements LocalVariableElement { |
134 final ElementKind kind = ElementKind.LOCAL_VARIABLE; | 123 final ElementKind kind = ElementKind.LOCAL_VARIABLE; |
135 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); | |
136 } | 124 } |
137 | 125 |
138 class MockLogger extends TypedMock implements Logger { | 126 class MockLogger extends TypedMock implements Logger {} |
139 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); | |
140 } | |
141 | 127 |
142 class MockMethodElement extends StringTypedMock implements MethodElement { | 128 class MockMethodElement extends StringTypedMock implements MethodElement { |
143 final kind = ElementKind.METHOD; | 129 final kind = ElementKind.METHOD; |
144 MockMethodElement([String name = 'method']) : super(name); | 130 MockMethodElement([String name = 'method']) : super(name); |
145 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); | |
146 } | 131 } |
147 | 132 |
148 /** | 133 /** |
149 * A mock [PackageMapProvider]. | 134 * A mock [PackageMapProvider]. |
150 */ | 135 */ |
151 class MockPackageMapProvider implements PubPackageMapProvider { | 136 class MockPackageMapProvider implements PubPackageMapProvider { |
152 /** | 137 /** |
153 * Package map that will be returned by the next call to [computePackageMap]. | 138 * Package map that will be returned by the next call to [computePackageMap]. |
154 */ | 139 */ |
155 Map<String, List<resource.Folder>> packageMap = | 140 Map<String, List<resource.Folder>> packageMap = |
(...skipping 24 matching lines...) Expand all Loading... |
180 } | 165 } |
181 | 166 |
182 noSuchMethod(Invocation invocation) { | 167 noSuchMethod(Invocation invocation) { |
183 // No other methods should be called. | 168 // No other methods should be called. |
184 return super.noSuchMethod(invocation); | 169 return super.noSuchMethod(invocation); |
185 } | 170 } |
186 } | 171 } |
187 | 172 |
188 class MockParameterElement extends TypedMock implements ParameterElement { | 173 class MockParameterElement extends TypedMock implements ParameterElement { |
189 final ElementKind kind = ElementKind.PARAMETER; | 174 final ElementKind kind = ElementKind.PARAMETER; |
190 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); | |
191 } | 175 } |
192 | 176 |
193 class MockPropertyAccessorElement extends TypedMock | 177 class MockPropertyAccessorElement extends TypedMock |
194 implements PropertyAccessorElement { | 178 implements PropertyAccessorElement { |
195 final ElementKind kind; | 179 final ElementKind kind; |
196 MockPropertyAccessorElement(this.kind); | 180 MockPropertyAccessorElement(this.kind); |
197 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); | |
198 } | 181 } |
199 | 182 |
200 /** | 183 /** |
201 * A mock [ServerCommunicationChannel] for testing [AnalysisServer]. | 184 * A mock [ServerCommunicationChannel] for testing [AnalysisServer]. |
202 */ | 185 */ |
203 class MockServerChannel implements ServerCommunicationChannel { | 186 class MockServerChannel implements ServerCommunicationChannel { |
204 StreamController<Request> requestController = new StreamController<Request>(); | 187 StreamController<Request> requestController = new StreamController<Request>(); |
205 StreamController<Response> responseController = | 188 StreamController<Response> responseController = |
206 new StreamController<Response>.broadcast(); | 189 new StreamController<Response>.broadcast(); |
207 StreamController<Notification> notificationController = | 190 StreamController<Notification> notificationController = |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 } | 226 } |
244 | 227 |
245 /** | 228 /** |
246 * Simulate request/response pair. | 229 * Simulate request/response pair. |
247 */ | 230 */ |
248 Future<Response> sendRequest(Request request) { | 231 Future<Response> sendRequest(Request request) { |
249 // No further requests should be sent after the connection is closed. | 232 // No further requests should be sent after the connection is closed. |
250 if (_closed) { | 233 if (_closed) { |
251 throw new Exception('sendRequest after connection closed'); | 234 throw new Exception('sendRequest after connection closed'); |
252 } | 235 } |
253 // Wrap send request in future to simulate websocket | 236 // Wrap send request in future to simulate WebSocket. |
254 new Future(() => requestController.add(request)); | 237 new Future(() => requestController.add(request)); |
255 return waitForResponse(request); | 238 return waitForResponse(request); |
256 } | 239 } |
257 | 240 |
258 @override | 241 @override |
259 void sendResponse(Response response) { | 242 void sendResponse(Response response) { |
260 // Don't deliver responses after the connection is closed. | 243 // Don't deliver responses after the connection is closed. |
261 if (_closed) { | 244 if (_closed) { |
262 return; | 245 return; |
263 } | 246 } |
264 responsesReceived.add(response); | 247 responsesReceived.add(response); |
265 // Wrap send response in future to simulate websocket | 248 // Wrap send response in future to simulate WebSocket. |
266 new Future(() => responseController.add(response)); | 249 new Future(() => responseController.add(response)); |
267 } | 250 } |
268 | 251 |
269 Future<Response> waitForResponse(Request request) { | 252 Future<Response> waitForResponse(Request request) { |
270 String id = request.id; | 253 String id = request.id; |
271 pumpEventQueue().then((_) { | 254 pumpEventQueue().then((_) { |
272 responseController.addError(new NoResponseException(request)); | 255 responseController.addError(new NoResponseException(request)); |
273 }); | 256 }); |
274 return responseController.stream.firstWhere((response) { | 257 return responseController.stream.firstWhere((response) { |
275 return response.id == id; | 258 return response.id == id; |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
330 stream.listen(onData, | 313 stream.listen(onData, |
331 onError: onError, onDone: onDone, cancelOnError: cancelOnError); | 314 onError: onError, onDone: onDone, cancelOnError: cancelOnError); |
332 | 315 |
333 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); | 316 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); |
334 | 317 |
335 Stream<T> where(bool test(T)) => stream.where(test); | 318 Stream<T> where(bool test(T)) => stream.where(test); |
336 } | 319 } |
337 | 320 |
338 class MockSource extends StringTypedMock implements Source { | 321 class MockSource extends StringTypedMock implements Source { |
339 MockSource([String name = 'mocked.dart']) : super(name); | 322 MockSource([String name = 'mocked.dart']) : super(name); |
340 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); | |
341 } | 323 } |
342 | 324 |
343 class MockTopLevelVariableElement extends TypedMock | 325 class MockTopLevelVariableElement extends TypedMock |
344 implements TopLevelVariableElement { | 326 implements TopLevelVariableElement { |
345 final ElementKind kind = ElementKind.TOP_LEVEL_VARIABLE; | 327 final ElementKind kind = ElementKind.TOP_LEVEL_VARIABLE; |
346 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); | |
347 } | 328 } |
348 | 329 |
349 class MockTypeParameterElement extends TypedMock | 330 class MockTypeParameterElement extends TypedMock |
350 implements TypeParameterElement { | 331 implements TypeParameterElement { |
351 final ElementKind kind = ElementKind.TYPE_PARAMETER; | 332 final ElementKind kind = ElementKind.TYPE_PARAMETER; |
352 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); | |
353 } | 333 } |
354 | 334 |
355 class NoResponseException implements Exception { | 335 class NoResponseException implements Exception { |
356 /** | 336 /** |
357 * The request that was not responded to. | 337 * The request that was not responded to. |
358 */ | 338 */ |
359 final Request request; | 339 final Request request; |
360 | 340 |
361 NoResponseException(this.request); | 341 NoResponseException(this.request); |
362 | 342 |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
457 } | 437 } |
458 return mismatchDescription; | 438 return mismatchDescription; |
459 } | 439 } |
460 | 440 |
461 @override | 441 @override |
462 bool matches(item, Map matchState) { | 442 bool matches(item, Map matchState) { |
463 Response response = item; | 443 Response response = item; |
464 return response != null && response.id == _id && response.error == null; | 444 return response != null && response.id == _id && response.error == null; |
465 } | 445 } |
466 } | 446 } |
OLD | NEW |