| 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 json_rpc_2.test.server.util; | 5 library json_rpc_2.test.server.util; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:convert'; | 8 import 'dart:convert'; |
| 9 | 9 |
| 10 import 'package:json_rpc_2/json_rpc_2.dart' as json_rpc; | 10 import 'package:json_rpc_2/json_rpc_2.dart' as json_rpc; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 'jsonrpc': '2.0', | 54 'jsonrpc': '2.0', |
| 55 'id': id, | 55 'id': id, |
| 56 'error': { | 56 'error': { |
| 57 'code': errorCode, | 57 'code': errorCode, |
| 58 'message': message, | 58 'message': message, |
| 59 'data': data | 59 'data': data |
| 60 } | 60 } |
| 61 }))); | 61 }))); |
| 62 } | 62 } |
| 63 | 63 |
| 64 /// Returns a matcher that matches [Future]s that never complete. |
| 65 Matcher get doesNotComplete => predicate((future) { |
| 66 future.then(expectAsync((_) { |
| 67 // This will never be called. [expectAsync] with `count: 0` ensures that an |
| 68 // error will be thrown when [future] completes. |
| 69 }, count: 0)); |
| 70 |
| 71 // Make sure there's enough time in the test for [expectAsync] to fail if it's |
| 72 // going to. |
| 73 expect(pumpEventQueue(), completes); |
| 74 |
| 75 return true; |
| 76 }); |
| 77 |
| 64 /// Returns a matcher that matches a [json_rpc.RpcException] with an | 78 /// Returns a matcher that matches a [json_rpc.RpcException] with an |
| 65 /// `invalid_params` error code. | 79 /// `invalid_params` error code. |
| 66 Matcher throwsInvalidParams(String message) { | 80 Matcher throwsInvalidParams(String message) { |
| 67 return throwsA(predicate((error) { | 81 return throwsA(predicate((error) { |
| 68 expect(error, new isInstanceOf<json_rpc.RpcException>()); | 82 expect(error, new isInstanceOf<json_rpc.RpcException>()); |
| 69 expect(error.code, equals(error_code.INVALID_PARAMS)); | 83 expect(error.code, equals(error_code.INVALID_PARAMS)); |
| 70 expect(error.message, equals(message)); | 84 expect(error.message, equals(message)); |
| 71 return true; | 85 return true; |
| 72 })); | 86 })); |
| 73 } | 87 } |
| 74 | 88 |
| 75 /// Returns a [Future] that completes after pumping the event queue [times] | 89 /// Returns a [Future] that completes after pumping the event queue [times] |
| 76 /// times. By default, this should pump the event queue enough times to allow | 90 /// times. By default, this should pump the event queue enough times to allow |
| 77 /// any code to run, as long as it's not waiting on some external event. | 91 /// any code to run, as long as it's not waiting on some external event. |
| 78 Future pumpEventQueue([int times = 20]) { | 92 Future pumpEventQueue([int times = 20]) { |
| 79 if (times == 0) return new Future.value(); | 93 if (times == 0) return new Future.value(); |
| 80 // We use a delayed future to allow microtask events to finish. The | 94 // We use a delayed future to allow microtask events to finish. The |
| 81 // Future.value or Future() constructors use scheduleMicrotask themselves and | 95 // Future.value or Future() constructors use scheduleMicrotask themselves and |
| 82 // would therefore not wait for microtask callbacks that are scheduled after | 96 // would therefore not wait for microtask callbacks that are scheduled after |
| 83 // invoking this method. | 97 // invoking this method. |
| 84 return new Future.delayed(Duration.ZERO, () => pumpEventQueue(times - 1)); | 98 return new Future.delayed(Duration.ZERO, () => pumpEventQueue(times - 1)); |
| 85 } | 99 } |
| OLD | NEW |