Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(33)

Unified Diff: pkg/analysis_server/test/mocks.dart

Issue 725143004: Format and sort analyzer and analysis_server packages. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pkg/analysis_server/test/mocks.dart
diff --git a/pkg/analysis_server/test/mocks.dart b/pkg/analysis_server/test/mocks.dart
index 1bb28d5a35c47ca0d12963f708702d05cbece4f0..003ea4c3ba61f6ec11d0daa1972a07c7b4510ab5 100644
--- a/pkg/analysis_server/test/mocks.dart
+++ b/pkg/analysis_server/test/mocks.dart
@@ -4,21 +4,20 @@
library mocks;
-import 'dart:async';
-import 'dart:io';
-
@MirrorsUsed(targets: 'mocks', override: '*')
import 'dart:mirrors';
+import 'dart:async';
+import 'dart:io';
-import 'package:analysis_server/src/services/index/index.dart';
-import 'package:analyzer/file_system/file_system.dart' as resource;
-import 'package:analyzer/file_system/memory_file_system.dart' as resource;
import 'package:analysis_server/src/analysis_server.dart';
import 'package:analysis_server/src/channel/channel.dart';
-import 'package:analysis_server/src/operation/operation_analysis.dart';
import 'package:analysis_server/src/operation/operation.dart';
-import 'package:analyzer/source/package_map_provider.dart';
+import 'package:analysis_server/src/operation/operation_analysis.dart';
import 'package:analysis_server/src/protocol.dart' hide Element, ElementKind;
+import 'package:analysis_server/src/services/index/index.dart';
+import 'package:analyzer/file_system/file_system.dart' as resource;
+import 'package:analyzer/file_system/memory_file_system.dart' as resource;
+import 'package:analyzer/source/package_map_provider.dart';
import 'package:analyzer/src/generated/element.dart';
import 'package:analyzer/src/generated/engine.dart';
import 'package:analyzer/src/generated/source.dart';
@@ -43,6 +42,19 @@ String get sdkPath {
}
/**
+ * A [Matcher] that check that the given [Response] has an expected identifier
+ * and has an error. The error code may optionally be checked.
+ */
+Matcher isResponseFailure(String id, [RequestErrorCode code]) =>
+ new _IsResponseFailure(id, code);
+
+/**
+ * A [Matcher] that check that the given [Response] has an expected identifier
+ * and no error.
+ */
+Matcher isResponseSuccess(String id) => new _IsResponseSuccess(id);
+
+/**
* Returns a [Future] that completes after pumping the event queue [times]
* times. By default, this should pump the event queue enough times to allow
* any code to run, as long as it's not waiting on some external event.
@@ -68,80 +80,180 @@ Future waitForServerOperationsPerformed(AnalysisServer server) {
// Future.value or Future() constructors use scheduleMicrotask themselves and
// would therefore not wait for microtask callbacks that are scheduled after
// invoking this method.
- return new Future.delayed(Duration.ZERO,
+ return new Future.delayed(
+ Duration.ZERO,
() => waitForServerOperationsPerformed(server));
}
-/**
- * A mock [WebSocket] for testing.
- */
-class MockSocket<T> implements WebSocket {
- StreamController controller = new StreamController();
- MockSocket twin;
- Stream stream;
+typedef void MockServerOperationPerformFunction(AnalysisServer server);
- factory MockSocket.pair() {
- MockSocket socket1 = new MockSocket();
- MockSocket socket2 = new MockSocket();
- socket1.twin = socket2;
- socket2.twin = socket1;
- socket1.stream = socket2.controller.stream;
- socket2.stream = socket1.controller.stream;
- return socket1;
- }
+class MockAnalysisContext extends StringTypedMock implements AnalysisContext {
+ MockAnalysisContext(String name) : super(name);
+ noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
+}
- MockSocket();
+class MockClassElement extends TypedMock implements ClassElement {
+ final ElementKind kind = ElementKind.CLASS;
+ noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
+}
- void add(T text) => controller.add(text);
- void allowMultipleListeners() {
- stream = stream.asBroadcastStream();
- }
+class MockCompilationUnitElement extends TypedMock implements
+ CompilationUnitElement {
+ final ElementKind kind = ElementKind.COMPILATION_UNIT;
+ noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
+}
- Future close([int code, String reason]) => controller.close()
- .then((_) => twin.controller.close());
+class MockConstructorElement extends TypedMock implements ConstructorElement {
+ final kind = ElementKind.CONSTRUCTOR;
+ noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
+}
- StreamSubscription<T> listen(void onData(T event),
- { Function onError, void onDone(), bool cancelOnError}) =>
- stream.listen(onData, onError: onError, onDone: onDone,
- cancelOnError: cancelOnError);
- Stream<T> where(bool test(T)) => stream.where(test);
+class MockElement extends StringTypedMock implements Element {
+ MockElement([String name = '<element>']) : super(name);
+
+ @override
+ String get displayName => _toString;
+
+ @override
+ String get name => _toString;
noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
}
-class NoResponseException implements Exception {
+class MockFieldElement extends TypedMock implements FieldElement {
+ final ElementKind kind = ElementKind.FIELD;
+ noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
+}
+
+
+class MockFunctionElement extends TypedMock implements FunctionElement {
+ final ElementKind kind = ElementKind.FUNCTION;
+ noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
+}
+
+
+class MockFunctionTypeAliasElement extends TypedMock implements
+ FunctionTypeAliasElement {
+ final ElementKind kind = ElementKind.FUNCTION_TYPE_ALIAS;
+ noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
+}
+
+
+class MockHtmlElement extends TypedMock implements HtmlElement {
+ final ElementKind kind = ElementKind.HTML;
+ noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
+}
+
+
+class MockImportElement extends TypedMock implements ImportElement {
+ final ElementKind kind = ElementKind.IMPORT;
+ noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
+}
+
+
+class MockLibraryElement extends TypedMock implements LibraryElement {
+ final ElementKind kind = ElementKind.LIBRARY;
+ noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
+}
+
+
+class MockLocalVariableElement extends TypedMock implements LocalVariableElement
+ {
+ final ElementKind kind = ElementKind.LOCAL_VARIABLE;
+ noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
+}
+
+
+class MockLogger extends TypedMock implements Logger {
+ noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
+}
+
+
+class MockMethodElement extends StringTypedMock implements MethodElement {
+ final kind = ElementKind.METHOD;
+ MockMethodElement([String name = 'method']) : super(name);
+ noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
+}
+
+
+/**
+ * A mock [PackageMapProvider].
+ */
+class MockPackageMapProvider implements PackageMapProvider {
/**
- * The request that was not responded to.
+ * Package map that will be returned by the next call to [computePackageMap].
*/
- final Request request;
+ Map<String, List<resource.Folder>> packageMap = <String,
+ List<resource.Folder>>{};
- NoResponseException(this.request);
+ /**
+ * Package maps that will be returned by the next call to [computePackageMap].
+ */
+ Map<String, Map<String, List<resource.Folder>>> packageMaps = null;
- String toString() {
- return "NoResponseException after request ${request.toJson()}";
+ /**
+ * Dependency list that will be returned by the next call to [computePackageMap].
+ */
+ Set<String> dependencies = new Set<String>();
+
+ @override
+ PackageMapInfo computePackageMap(resource.Folder folder) {
+ if (packageMaps != null) {
+ return new PackageMapInfo(packageMaps[folder.path], dependencies);
+ }
+ return new PackageMapInfo(packageMap, dependencies);
}
}
+
+class MockParameterElement extends TypedMock implements ParameterElement {
+ final ElementKind kind = ElementKind.PARAMETER;
+ noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
+}
+
+
+class MockPropertyAccessorElement extends TypedMock implements
+ PropertyAccessorElement {
+ final ElementKind kind;
+ MockPropertyAccessorElement(this.kind);
+ noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
+}
+
+
/**
* A mock [ServerCommunicationChannel] for testing [AnalysisServer].
*/
class MockServerChannel implements ServerCommunicationChannel {
StreamController<Request> requestController = new StreamController<Request>();
- StreamController<Response> responseController = new StreamController<Response>.broadcast();
- StreamController<Notification> notificationController = new StreamController<Notification>(sync: true);
+ StreamController<Response> responseController =
+ new StreamController<Response>.broadcast();
+ StreamController<Notification> notificationController =
+ new StreamController<Notification>(sync: true);
List<Response> responsesReceived = [];
List<Notification> notificationsReceived = [];
bool _closed = false;
- MockServerChannel() {
+ MockServerChannel();
+ @override
+ void close() {
+ _closed = true;
+ }
+
+ void expectMsgCount({responseCount: 0, notificationCount: 0}) {
+ expect(responsesReceived, hasLength(responseCount));
+ expect(notificationsReceived, hasLength(notificationCount));
}
@override
- void listen(void onRequest(Request request), {Function onError, void onDone()}) {
- requestController.stream.listen(onRequest, onError: onError, onDone: onDone);
+ void listen(void onRequest(Request request), {Function onError, void
+ onDone()}) {
+ requestController.stream.listen(
+ onRequest,
+ onError: onError,
+ onDone: onDone);
}
@override
@@ -181,11 +293,6 @@ class MockServerChannel implements ServerCommunicationChannel {
new Future(() => responseController.add(response));
}
- void expectMsgCount({responseCount: 0, notificationCount: 0}) {
- expect(responsesReceived, hasLength(responseCount));
- expect(notificationsReceived, hasLength(notificationCount));
- }
-
Future<Response> waitForResponse(Request request) {
String id = request.id;
pumpEventQueue().then((_) {
@@ -198,14 +305,8 @@ class MockServerChannel implements ServerCommunicationChannel {
// return response.id == id;
// });
}
-
- @override
- void close() {
- _closed = true;
- }
}
-typedef void MockServerOperationPerformFunction(AnalysisServer server);
/**
* A mock [ServerOperation] for testing [AnalysisServer].
@@ -217,15 +318,15 @@ class MockServerOperation implements PerformAnalysisOperation {
MockServerOperation(this.priority, this._perform);
@override
- void perform(AnalysisServer server) => this._perform(server);
-
- @override
AnalysisContext get context => null;
@override
bool get isContinue => false;
@override
+ void perform(AnalysisServer server) => this._perform(server);
+
+ @override
void sendNotices(AnalysisServer server, List<ChangeNotice> notices) {
}
@@ -236,58 +337,98 @@ class MockServerOperation implements PerformAnalysisOperation {
/**
- * A [Matcher] that check that the given [Response] has an expected identifier
- * and no error.
+ * A mock [WebSocket] for testing.
*/
-Matcher isResponseSuccess(String id) => new _IsResponseSuccess(id);
+class MockSocket<T> implements WebSocket {
+ StreamController controller = new StreamController();
+ MockSocket twin;
+ Stream stream;
-/**
- * A [Matcher] that check that there are no `error` in a given [Response].
- */
-class _IsResponseSuccess extends Matcher {
- final String _id;
+ MockSocket();
- _IsResponseSuccess(this._id);
+ factory MockSocket.pair() {
+ MockSocket socket1 = new MockSocket();
+ MockSocket socket2 = new MockSocket();
+ socket1.twin = socket2;
+ socket2.twin = socket1;
+ socket1.stream = socket2.controller.stream;
+ socket2.stream = socket1.controller.stream;
+ return socket1;
+ }
- @override
- Description describe(Description description) {
- return description.addDescriptionOf(
- 'response with identifier "$_id" and without error');
+ void add(T text) => controller.add(text);
+
+ void allowMultipleListeners() {
+ stream = stream.asBroadcastStream();
}
- @override
- bool matches(item, Map matchState) {
- Response response = item;
- return response != null && response.id == _id && response.error == null;
+ Future close([int code, String reason]) =>
+ controller.close().then((_) => twin.controller.close());
+
+ StreamSubscription<T> listen(void onData(T event), {Function onError, void
+ onDone(), bool cancelOnError}) =>
+ stream.listen(
+ onData,
+ onError: onError,
+ onDone: onDone,
+ cancelOnError: cancelOnError);
+
+ noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
+
+ Stream<T> where(bool test(T)) => stream.where(test);
+}
+
+
+class MockSource extends StringTypedMock implements Source {
+ MockSource([String name = 'mocked.dart']) : super(name);
+ noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
+}
+
+
+class MockTopLevelVariableElement extends TypedMock implements
+ TopLevelVariableElement {
+ final ElementKind kind = ElementKind.TOP_LEVEL_VARIABLE;
+ noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
+}
+
+
+class MockTypeParameterElement extends TypedMock implements TypeParameterElement
+ {
+ final ElementKind kind = ElementKind.TYPE_PARAMETER;
+ noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
+}
+
+
+class NoResponseException implements Exception {
+ /**
+ * The request that was not responded to.
+ */
+ final Request request;
+
+ NoResponseException(this.request);
+
+ String toString() {
+ return "NoResponseException after request ${request.toJson()}";
}
+}
+
+
+class StringTypedMock extends TypedMock {
+ String _toString;
+
+ StringTypedMock(this._toString);
@override
- Description describeMismatch(item, Description mismatchDescription,
- Map matchState, bool verbose) {
- Response response = item;
- if (response == null) {
- mismatchDescription.add('is null response');
- } else {
- var id = response.id;
- RequestError error = response.error;
- mismatchDescription.add('has identifier "$id"');
- if (error != null) {
- mismatchDescription.add(' and has error $error');
- }
+ String toString() {
+ if (_toString != null) {
+ return _toString;
}
- return mismatchDescription;
+ return super.toString();
}
}
/**
- * A [Matcher] that check that the given [Response] has an expected identifier
- * and has an error. The error code may optionally be checked.
- */
-Matcher isResponseFailure(String id, [RequestErrorCode code]) =>
- new _IsResponseFailure(id, code);
-
-/**
* A [Matcher] that check that there are no `error` in a given [Response].
*/
class _IsResponseFailure extends Matcher {
@@ -298,8 +439,8 @@ class _IsResponseFailure extends Matcher {
@override
Description describe(Description description) {
- description = description.add(
- 'response with identifier "$_id" and an error');
+ description =
+ description.add('response with identifier "$_id" and an error');
if (_code != null) {
description = description.add(' with code ${this._code.name}');
}
@@ -307,20 +448,8 @@ class _IsResponseFailure extends Matcher {
}
@override
- bool matches(item, Map matchState) {
- Response response = item;
- if (response.id != _id || response.error == null) {
- return false;
- }
- if (_code != null && response.error.code != _code) {
- return false;
- }
- return true;
- }
-
- @override
Description describeMismatch(item, Description mismatchDescription,
- Map matchState, bool verbose) {
+ Map matchState, bool verbose) {
Response response = item;
var id = response.id;
RequestError error = response.error;
@@ -328,180 +457,60 @@ class _IsResponseFailure extends Matcher {
if (error == null) {
mismatchDescription.add(' and has no error');
} else {
- mismatchDescription.add(' and has error code ${response.error.code.name}');
+ mismatchDescription.add(
+ ' and has error code ${response.error.code.name}');
}
return mismatchDescription;
}
-}
-
-
-/**
- * A mock [PackageMapProvider].
- */
-class MockPackageMapProvider implements PackageMapProvider {
- /**
- * Package map that will be returned by the next call to [computePackageMap].
- */
- Map<String, List<resource.Folder>> packageMap = <String, List<resource.Folder>>{};
-
- /**
- * Package maps that will be returned by the next call to [computePackageMap].
- */
- Map<String, Map<String, List<resource.Folder>>> packageMaps = null;
-
- /**
- * Dependency list that will be returned by the next call to [computePackageMap].
- */
- Set<String> dependencies = new Set<String>();
@override
- PackageMapInfo computePackageMap(resource.Folder folder) {
- if (packageMaps != null) {
- return new PackageMapInfo(packageMaps[folder.path], dependencies);
+ bool matches(item, Map matchState) {
+ Response response = item;
+ if (response.id != _id || response.error == null) {
+ return false;
}
- return new PackageMapInfo(packageMap, dependencies);
+ if (_code != null && response.error.code != _code) {
+ return false;
+ }
+ return true;
}
}
-class MockAnalysisContext extends StringTypedMock implements AnalysisContext {
- MockAnalysisContext(String name) : super(name);
- noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
-}
-
-
-class MockClassElement extends TypedMock implements ClassElement {
- final ElementKind kind = ElementKind.CLASS;
- noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
-}
-
-
-class MockCompilationUnitElement extends TypedMock implements
- CompilationUnitElement {
- final ElementKind kind = ElementKind.COMPILATION_UNIT;
- noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
-}
-
-
-class MockConstructorElement extends TypedMock implements ConstructorElement {
- final kind = ElementKind.CONSTRUCTOR;
- noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
-}
-
+/**
+ * A [Matcher] that check that there are no `error` in a given [Response].
+ */
+class _IsResponseSuccess extends Matcher {
+ final String _id;
-class MockElement extends StringTypedMock implements Element {
- MockElement([String name = '<element>']) : super(name);
+ _IsResponseSuccess(this._id);
@override
- String get displayName => _toString;
+ Description describe(Description description) {
+ return description.addDescriptionOf(
+ 'response with identifier "$_id" and without error');
+ }
@override
- String get name => _toString;
-
- noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
-}
-
-
-class MockFieldElement extends TypedMock implements FieldElement {
- final ElementKind kind = ElementKind.FIELD;
- noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
-}
-
-
-class MockFunctionElement extends TypedMock implements FunctionElement {
- final ElementKind kind = ElementKind.FUNCTION;
- noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
-}
-
-
-class MockFunctionTypeAliasElement extends TypedMock implements
- FunctionTypeAliasElement {
- final ElementKind kind = ElementKind.FUNCTION_TYPE_ALIAS;
- noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
-}
-
-
-class MockHtmlElement extends TypedMock implements HtmlElement {
- final ElementKind kind = ElementKind.HTML;
- noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
-}
-
-
-class MockImportElement extends TypedMock implements ImportElement {
- final ElementKind kind = ElementKind.IMPORT;
- noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
-}
-
-
-class MockLibraryElement extends TypedMock implements LibraryElement {
- final ElementKind kind = ElementKind.LIBRARY;
- noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
-}
-
-
-class MockLocalVariableElement extends TypedMock implements LocalVariableElement
- {
- final ElementKind kind = ElementKind.LOCAL_VARIABLE;
- noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
-}
-
-
-class MockLogger extends TypedMock implements Logger {
- noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
-}
-
-
-class MockMethodElement extends StringTypedMock implements MethodElement {
- final kind = ElementKind.METHOD;
- MockMethodElement([String name = 'method']) : super(name);
- noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
-}
-
-
-class MockParameterElement extends TypedMock implements ParameterElement {
- final ElementKind kind = ElementKind.PARAMETER;
- noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
-}
-
-
-class MockPropertyAccessorElement extends TypedMock implements
- PropertyAccessorElement {
- final ElementKind kind;
- MockPropertyAccessorElement(this.kind);
- noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
-}
-
-
-class MockSource extends StringTypedMock implements Source {
- MockSource([String name = 'mocked.dart']) : super(name);
- noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
-}
-
-
-class MockTopLevelVariableElement extends TypedMock implements
- TopLevelVariableElement {
- final ElementKind kind = ElementKind.TOP_LEVEL_VARIABLE;
- noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
-}
-
-
-class MockTypeParameterElement extends TypedMock implements TypeParameterElement
- {
- final ElementKind kind = ElementKind.TYPE_PARAMETER;
- noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
-}
-
-
-class StringTypedMock extends TypedMock {
- String _toString;
-
- StringTypedMock(this._toString);
+ Description describeMismatch(item, Description mismatchDescription,
+ Map matchState, bool verbose) {
+ Response response = item;
+ if (response == null) {
+ mismatchDescription.add('is null response');
+ } else {
+ var id = response.id;
+ RequestError error = response.error;
+ mismatchDescription.add('has identifier "$id"');
+ if (error != null) {
+ mismatchDescription.add(' and has error $error');
+ }
+ }
+ return mismatchDescription;
+ }
@override
- String toString() {
- if (_toString != null) {
- return _toString;
- }
- return super.toString();
+ bool matches(item, Map matchState) {
+ Response response = item;
+ return response != null && response.id == _id && response.error == null;
}
}

Powered by Google App Engine
This is Rietveld 408576698