| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 observe.test.observe_test_utils; | 5 library observe.test.observe_test_utils; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'package:observe/observe.dart'; | 8 import 'package:observe/observe.dart'; |
| 9 import 'package:unittest/unittest.dart'; | 9 import 'package:unittest/unittest.dart'; |
| 10 | 10 |
| 11 import 'package:observe/src/microtask.dart'; |
| 12 export 'package:observe/src/microtask.dart'; |
| 13 |
| 11 // TODO(jmesserly): use matchers when we have a way to compare ChangeRecords. | 14 // TODO(jmesserly): use matchers when we have a way to compare ChangeRecords. |
| 12 // For now just use the toString. | 15 // For now just use the toString. |
| 13 expectChanges(actual, expected, {reason}) => | 16 expectChanges(actual, expected, {reason}) => |
| 14 expect('$actual', '$expected', reason: reason); | 17 expect('$actual', '$expected', reason: reason); |
| 15 | 18 |
| 16 /** | 19 /** |
| 17 * This change pumps events relevant to observers and data-binding tests. | |
| 18 * This must be used inside an [observeTest]. | |
| 19 * | |
| 20 * Executes all pending [runAsync] calls on the event loop, as well as | |
| 21 * performing [Observable.dirtyCheck], until there are no more pending events. | |
| 22 * It will always dirty check at least once. | |
| 23 */ | |
| 24 void performMicrotaskCheckpoint() { | |
| 25 Observable.dirtyCheck(); | |
| 26 | |
| 27 while (_pending.length > 0) { | |
| 28 var pending = _pending; | |
| 29 _pending = []; | |
| 30 | |
| 31 for (var callback in pending) { | |
| 32 try { | |
| 33 callback(); | |
| 34 } catch (e, s) { | |
| 35 new Completer().completeError(e, s); | |
| 36 } | |
| 37 } | |
| 38 | |
| 39 Observable.dirtyCheck(); | |
| 40 } | |
| 41 } | |
| 42 | |
| 43 List<Function> _pending = []; | |
| 44 | |
| 45 /** | |
| 46 * Wraps the [testCase] in a zone that supports [performMicrotaskCheckpoint], | |
| 47 * and returns the test case. | |
| 48 */ | |
| 49 wrapMicrotask(void testCase()) { | |
| 50 return () { | |
| 51 runZonedExperimental(() { | |
| 52 try { | |
| 53 testCase(); | |
| 54 } finally { | |
| 55 performMicrotaskCheckpoint(); | |
| 56 } | |
| 57 }, onRunAsync: (callback) => _pending.add(callback)); | |
| 58 }; | |
| 59 } | |
| 60 | |
| 61 /** | |
| 62 * This is a special kind of unit [test], that supports | 20 * This is a special kind of unit [test], that supports |
| 63 * calling [performMicrotaskCheckpoint] during the test to pump events | 21 * calling [performMicrotaskCheckpoint] during the test to pump events |
| 64 * that original from observable objects. | 22 * that original from observable objects. |
| 65 */ | 23 */ |
| 66 observeTest(name, testCase) => test(name, wrapMicrotask(testCase)); | 24 observeTest(name, testCase) => test(name, wrapMicrotask(testCase)); |
| 67 | 25 |
| 68 /** The [solo_test] version of [observeTest]. */ | 26 /** The [solo_test] version of [observeTest]. */ |
| 69 solo_observeTest(name, testCase) => solo_test(name, wrapMicrotask(testCase)); | 27 solo_observeTest(name, testCase) => solo_test(name, wrapMicrotask(testCase)); |
| OLD | NEW |