Index: pkg/analysis_server/test/mocks.dart |
diff --git a/pkg/analysis_server/test/mocks.dart b/pkg/analysis_server/test/mocks.dart |
index 88a6744c908c20e1ab6b1d67540ac375126fee66..82b15fa88d5b7db145336f54fdc30106719ea831 100644 |
--- a/pkg/analysis_server/test/mocks.dart |
+++ b/pkg/analysis_server/test/mocks.dart |
@@ -7,14 +7,29 @@ library mocks; |
import 'dart:async'; |
import 'dart:io'; |
+import 'package:analysis_server/src/analysis_server.dart'; |
import 'package:analysis_server/src/channel.dart'; |
import 'package:analysis_server/src/protocol.dart'; |
+import 'package:unittest/matcher.dart'; |
+ |
+/** |
+ * Answer the path the the SDK relative to the currently running script |
+ * or throw an exception if it cannot be found. |
+ */ |
+String get sdkPath { |
+ Uri sdkUri = Uri.base.resolveUri(Platform.script).resolve('../../../sdk/'); |
+ // Verify that the internal library file exists |
+ Uri libFileUri = sdkUri.resolve('lib/_internal/libraries.dart'); |
+ if (!new File.fromUri(libFileUri).existsSync()) { |
+ throw 'Expected Dart SDK at ${sdkUri.path}'; |
+ } |
+ return sdkUri.path; |
+} |
/** |
* A mock [WebSocket] for testing. |
*/ |
class MockSocket<T> implements WebSocket { |
- |
StreamController controller = new StreamController(); |
MockSocket twin; |
Stream stream; |
@@ -51,18 +66,48 @@ class MockSocket<T> implements WebSocket { |
} |
/** |
- * A mock [ServerCommunicationChannel] channel that does nothing. |
+ * A mock [ServerCommunicationChannel] for testing [AnalysisServer]. |
*/ |
class MockServerChannel implements ServerCommunicationChannel { |
+ StreamController<Request> requestController = new StreamController<Request>(); |
+ StreamController<Response> responseController = new StreamController<Response>(); |
+ StreamController<Notification> notificationController = new StreamController<Notification>(); |
+ |
+ List<Response> responsesReceived = []; |
+ List<Notification> notificationsReceived = []; |
+ |
+ MockServerChannel() { |
+ } |
+ |
@override |
void listen(void onRequest(Request request), {void onError(), void onDone()}) { |
+ requestController.stream.listen(onRequest, onError: onError, onDone: onDone); |
} |
@override |
void sendNotification(Notification notification) { |
+ notificationsReceived.add(notification); |
+ // Wrap send notification in future to simulate websocket |
+ new Future(() => notificationController.add(notification)); |
+ } |
+ |
+ /// Simulate request/response pair |
+ Future<Response> sendRequest(Request request) { |
+ var id = request.id; |
+ // Wrap send request in future to simulate websocket |
+ new Future(() => requestController.add(request)); |
+ return responseController.stream.firstWhere((response) => response.id == id); |
} |
@override |
void sendResponse(Response response) { |
+ responsesReceived.add(response); |
+ // Wrap send response in future to simulate websocket |
+ new Future(() => responseController.add(response)); |
+ } |
+ |
+ void expectMsgCount({responseCount: 0, notificationCount: 0}) { |
+ expect(responsesReceived, hasLength(responseCount)); |
+ expect(notificationsReceived, hasLength(notificationCount)); |
} |
} |