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

Side by Side Diff: pkg/observe/test/observe_test_utils.dart

Issue 19771010: implement dirty checking for @observable objects (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 5 months 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 library observe.test.observe_test_utils;
6
7 import 'dart:async';
8 import 'package:observe/observe.dart';
9 import 'package:unittest/unittest.dart';
10
11 // TODO(jmesserly): use matchers when we have a way to compare ChangeRecords.
12 // For now just use the toString.
13 expectChanges(actual, expected, {reason}) =>
14 expect('$actual', '$expected', reason: reason);
15
16 /**
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
63 * calling [performMicrotaskCheckpoint] during the test to pump events
64 * that original from observable objects.
65 */
66 observeTest(name, testCase) => test(name, wrapMicrotask(testCase));
67
68 /** The [solo_test] version of [observeTest]. */
69 solo_observeTest(name, testCase) => solo_test(name, wrapMicrotask(testCase));
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698