Chromium Code Reviews| 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'; | |
| 8 import 'dart:convert'; | |
| 7 import 'dart:io'; | 9 import 'dart:io'; |
| 8 import 'dart:async'; | |
| 9 | 10 |
| 10 /** | 11 /** |
| 11 * A mock [WebSocket] that immediately passes data to the listener. | 12 * A mock [WebSocket] for testing. |
| 12 */ | 13 */ |
| 13 class MockSocket<T> implements WebSocket { | 14 class MockSocket<T> implements WebSocket { |
|
Bob Nystrom
2014/03/04 01:03:45
I wonder if you can extend Stream here too and hav
| |
| 14 var onData; | 15 |
| 16 final JsonEncoder jsonEncoder = const JsonEncoder(null); | |
| 17 | |
| 18 final JsonDecoder jsonDecoder = const JsonDecoder(null); | |
|
Bob Nystrom
2014/03/04 01:03:45
You can probably just using the top-level "JSON" o
| |
| 19 | |
| 20 StreamController controller = new StreamController(); | |
| 21 MockSocket twin; | |
| 22 Stream stream; | |
| 23 | |
| 24 factory MockSocket.pair() { | |
| 25 MockSocket socket1 = new MockSocket(); | |
| 26 MockSocket socket2 = new MockSocket(); | |
|
Bob Nystrom
2014/03/04 01:03:45
I prefer using "var" for locals. Fortunately, ther
| |
| 27 socket1.twin = socket2; | |
| 28 socket2.twin = socket1; | |
| 29 socket1.stream = socket2.controller.stream; | |
| 30 socket2.stream = socket1.controller.stream; | |
| 31 return socket1; | |
| 32 } | |
| 33 | |
| 34 MockSocket(); | |
| 35 | |
| 36 void add(T text) => controller.add(text); | |
| 37 | |
| 38 void allowMultipleListeners() { | |
| 39 stream = stream.asBroadcastStream(); | |
| 40 } | |
| 41 | |
| 15 StreamSubscription<T> listen(void onData(T event), | 42 StreamSubscription<T> listen(void onData(T event), |
| 16 {Function onError, void onDone(), bool cancelOnError}) { | 43 { Function onError, void onDone(), bool cancelOnError}) => |
| 17 this.onData = onData; | 44 stream.listen(onData, onError: onError, onDone: onDone, |
| 18 return null; | 45 cancelOnError: cancelOnError); |
| 19 } | |
| 20 void add(T event) => onData(event); | |
| 21 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); | |
| 22 } | |
| 23 | 46 |
| 24 /** | 47 Stream<T> where(bool test(T)) => stream.where(test); |
|
Bob Nystrom
2014/03/04 01:03:45
If you're going to forward these, it feels like yo
| |
| 25 * A mock [WebSocket] for sending invalid JSON data and counting responses. | 48 |
| 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); | 49 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); |
| 38 } | 50 } |
| OLD | NEW |