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 | 9 |
| 10 import 'package:analysis_server/src/analysis_server.dart'; |
10 import 'package:analysis_server/src/channel.dart'; | 11 import 'package:analysis_server/src/channel.dart'; |
11 import 'package:analysis_server/src/protocol.dart'; | 12 import 'package:analysis_server/src/protocol.dart'; |
| 13 import 'package:unittest/matcher.dart'; |
| 14 |
| 15 /** |
| 16 * Answer the path the the SDK relative to the currently running script |
| 17 * or throw an exception if it cannot be found. |
| 18 */ |
| 19 String get sdkPath { |
| 20 Uri sdkUri = Uri.base.resolveUri(Platform.script).resolve('../../../sdk/'); |
| 21 // Verify that the internal library file exists |
| 22 Uri libFileUri = sdkUri.resolve('lib/_internal/libraries.dart'); |
| 23 if (!new File.fromUri(libFileUri).existsSync()) { |
| 24 throw 'Expected Dart SDK at ${sdkUri.path}'; |
| 25 } |
| 26 return sdkUri.path; |
| 27 } |
12 | 28 |
13 /** | 29 /** |
14 * A mock [WebSocket] for testing. | 30 * A mock [WebSocket] for testing. |
15 */ | 31 */ |
16 class MockSocket<T> implements WebSocket { | 32 class MockSocket<T> implements WebSocket { |
17 | |
18 StreamController controller = new StreamController(); | 33 StreamController controller = new StreamController(); |
19 MockSocket twin; | 34 MockSocket twin; |
20 Stream stream; | 35 Stream stream; |
21 | 36 |
22 factory MockSocket.pair() { | 37 factory MockSocket.pair() { |
23 MockSocket socket1 = new MockSocket(); | 38 MockSocket socket1 = new MockSocket(); |
24 MockSocket socket2 = new MockSocket(); | 39 MockSocket socket2 = new MockSocket(); |
25 socket1.twin = socket2; | 40 socket1.twin = socket2; |
26 socket2.twin = socket1; | 41 socket2.twin = socket1; |
27 socket1.stream = socket2.controller.stream; | 42 socket1.stream = socket2.controller.stream; |
(...skipping 16 matching lines...) Expand all Loading... |
44 { Function onError, void onDone(), bool cancelOnError}) => | 59 { Function onError, void onDone(), bool cancelOnError}) => |
45 stream.listen(onData, onError: onError, onDone: onDone, | 60 stream.listen(onData, onError: onError, onDone: onDone, |
46 cancelOnError: cancelOnError); | 61 cancelOnError: cancelOnError); |
47 | 62 |
48 Stream<T> where(bool test(T)) => stream.where(test); | 63 Stream<T> where(bool test(T)) => stream.where(test); |
49 | 64 |
50 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); | 65 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); |
51 } | 66 } |
52 | 67 |
53 /** | 68 /** |
54 * A mock [ServerCommunicationChannel] channel that does nothing. | 69 * A mock [ServerCommunicationChannel] for testing [AnalysisServer]. |
55 */ | 70 */ |
56 class MockServerChannel implements ServerCommunicationChannel { | 71 class MockServerChannel implements ServerCommunicationChannel { |
| 72 StreamController<Request> requestController = new StreamController<Request>(); |
| 73 StreamController<Response> responseController = new StreamController<Response>
(); |
| 74 StreamController<Notification> notificationController = new StreamController<N
otification>(); |
| 75 |
| 76 List<Response> responsesReceived = []; |
| 77 List<Notification> notificationsReceived = []; |
| 78 |
| 79 MockServerChannel() { |
| 80 } |
| 81 |
57 @override | 82 @override |
58 void listen(void onRequest(Request request), {void onError(), void onDone()})
{ | 83 void listen(void onRequest(Request request), {void onError(), void onDone()})
{ |
| 84 requestController.stream.listen(onRequest, onError: onError, onDone: onDone)
; |
59 } | 85 } |
60 | 86 |
61 @override | 87 @override |
62 void sendNotification(Notification notification) { | 88 void sendNotification(Notification notification) { |
| 89 notificationsReceived.add(notification); |
| 90 // Wrap send notification in future to simulate websocket |
| 91 new Future(() => notificationController.add(notification)); |
| 92 } |
| 93 |
| 94 /// Simulate request/response pair |
| 95 Future<Response> sendRequest(Request request) { |
| 96 var id = request.id; |
| 97 // Wrap send request in future to simulate websocket |
| 98 new Future(() => requestController.add(request)); |
| 99 return responseController.stream.firstWhere((response) => response.id == id)
; |
63 } | 100 } |
64 | 101 |
65 @override | 102 @override |
66 void sendResponse(Response response) { | 103 void sendResponse(Response response) { |
| 104 responsesReceived.add(response); |
| 105 // Wrap send response in future to simulate websocket |
| 106 new Future(() => responseController.add(response)); |
| 107 } |
| 108 |
| 109 void expectMsgCount({responseCount: 0, notificationCount: 0}) { |
| 110 expect(responsesReceived, hasLength(responseCount)); |
| 111 expect(notificationsReceived, hasLength(notificationCount)); |
67 } | 112 } |
68 } | 113 } |
OLD | NEW |