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 'package:analysis_server/src/channel.dart'; | 7 import 'dart:io'; |
| 8 import 'dart:async'; |
8 | 9 |
9 /** | 10 /** |
10 * Instances of the class [MockChannel] implement a [CommunicationChannel] that | 11 * A mock [WebSocket] that immediately passes data to the listener. |
11 * does nothing in response to every method invoked on it. | |
12 */ | 12 */ |
13 class MockChannel implements CommunicationChannel { | 13 class MockSocket<T> implements WebSocket { |
14 dynamic noSuchMethod(Invocation invocation) { | 14 var onData; |
15 // Do nothing | 15 StreamSubscription<T> listen(void onData(T event), |
| 16 {Function onError, void onDone(), bool cancelOnError}) { |
| 17 this.onData = onData; |
16 return null; | 18 return null; |
17 } | 19 } |
| 20 void add(T event) => onData(event); |
| 21 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); |
18 } | 22 } |
| 23 |
| 24 /** |
| 25 * A mock [WebSocket] for sending invalid JSON data and counting responses. |
| 26 */ |
| 27 class InvalidJsonMockSocket<T> implements WebSocket { |
| 28 int responseCount = 0; |
| 29 var onData; |
| 30 StreamSubscription<T> listen(void onData(T event), |
| 31 {Function onError, void onDone(), bool cancelOnError}) { |
| 32 this.onData = onData; |
| 33 return null; |
| 34 } |
| 35 void addInvalid(T event) => onData(event); |
| 36 void add(T event) { responseCount++; } |
| 37 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); |
| 38 } |
OLD | NEW |