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

Side by Side Diff: pkg/scheduled_test/lib/src/utils.dart

Issue 14070010: Refactor Future constructors. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 8 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
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 utils; 5 library utils;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 8
9 import 'package:stack_trace/stack_trace.dart'; 9 import 'package:stack_trace/stack_trace.dart';
10 10
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
43 var firstLine = "$firstPrefix${lines.first}"; 43 var firstLine = "$firstPrefix${lines.first}";
44 lines = lines.skip(1).map((line) => '$prefix$line').toList(); 44 lines = lines.skip(1).map((line) => '$prefix$line').toList();
45 lines.insert(0, firstLine); 45 lines.insert(0, firstLine);
46 return lines.join('\n'); 46 return lines.join('\n');
47 } 47 }
48 48
49 /// Returns a [Future] that completes after pumping the event queue [times] 49 /// Returns a [Future] that completes after pumping the event queue [times]
50 /// times. By default, this should pump the event queue enough times to allow 50 /// times. By default, this should pump the event queue enough times to allow
51 /// any code to run, as long as it's not waiting on some external event. 51 /// any code to run, as long as it's not waiting on some external event.
52 Future pumpEventQueue([int times=20]) { 52 Future pumpEventQueue([int times=20]) {
53 if (times == 0) return new Future.immediate(null); 53 if (times == 0) return new Future.value();
54 return new Future.immediate(null).then((_) => pumpEventQueue(times - 1)); 54 return new Future.value().then((_) => pumpEventQueue(times - 1));
55 } 55 }
56 56
57 /// Returns whether [iterable1] has the same elements in the same order as 57 /// Returns whether [iterable1] has the same elements in the same order as
58 /// [iterable2]. The elements are compared using `==`. 58 /// [iterable2]. The elements are compared using `==`.
59 bool orderedIterableEquals(Iterable iterable1, Iterable iterable2) { 59 bool orderedIterableEquals(Iterable iterable1, Iterable iterable2) {
60 var iter1 = iterable1.iterator; 60 var iter1 = iterable1.iterator;
61 var iter2 = iterable2.iterator; 61 var iter2 = iterable2.iterator;
62 62
63 while (true) { 63 while (true) {
64 var hasNext1 = iter1.moveNext(); 64 var hasNext1 = iter1.moveNext();
65 var hasNext2 = iter2.moveNext(); 65 var hasNext2 = iter2.moveNext();
66 if (hasNext1 != hasNext2) return false; 66 if (hasNext1 != hasNext2) return false;
67 if (!hasNext1) return true; 67 if (!hasNext1) return true;
68 if (iter1.current != iter2.current) return false; 68 if (iter1.current != iter2.current) return false;
69 } 69 }
70 } 70 }
71 71
72 // TODO(nweiz): remove this when issue 8731 is fixed. 72 // TODO(nweiz): remove this when issue 8731 is fixed.
73 /// Returns a [Stream] that will immediately emit [error] and then close. 73 /// Returns a [Stream] that will immediately emit [error] and then close.
74 Stream errorStream(error) => new Future.immediateError(error).asStream(); 74 Stream errorStream(error) => new Future.error(error).asStream();
75 75
76 /// Returns a buffered stream that will emit the same values as the stream 76 /// Returns a buffered stream that will emit the same values as the stream
77 /// returned by [future] once [future] completes. If [future] completes to an 77 /// returned by [future] once [future] completes. If [future] completes to an
78 /// error, the return value will emit that error and then close. 78 /// error, the return value will emit that error and then close.
79 Stream futureStream(Future<Stream> future) { 79 Stream futureStream(Future<Stream> future) {
80 var controller = new StreamController(); 80 var controller = new StreamController();
81 future.then((stream) { 81 future.then((stream) {
82 stream.listen( 82 stream.listen(
83 controller.add, 83 controller.add,
84 onError: (error) => controller.addError(error), 84 onError: (error) => controller.addError(error),
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 155
156 /// Takes a simple data structure (composed of [Map]s, [Iterable]s, scalar 156 /// Takes a simple data structure (composed of [Map]s, [Iterable]s, scalar
157 /// objects, and [Future]s) and recursively resolves all the [Future]s contained 157 /// objects, and [Future]s) and recursively resolves all the [Future]s contained
158 /// within. Completes with the fully resolved structure. 158 /// within. Completes with the fully resolved structure.
159 Future awaitObject(object) { 159 Future awaitObject(object) {
160 // Unroll nested futures. 160 // Unroll nested futures.
161 if (object is Future) return object.then(awaitObject); 161 if (object is Future) return object.then(awaitObject);
162 if (object is Iterable) { 162 if (object is Iterable) {
163 return Future.wait(object.map(awaitObject).toList()); 163 return Future.wait(object.map(awaitObject).toList());
164 } 164 }
165 if (object is! Map) return new Future.immediate(object); 165 if (object is! Map) return new Future.value(object);
166 166
167 var pairs = <Future<Pair>>[]; 167 var pairs = <Future<Pair>>[];
168 object.forEach((key, value) { 168 object.forEach((key, value) {
169 pairs.add(awaitObject(value) 169 pairs.add(awaitObject(value)
170 .then((resolved) => new Pair(key, resolved))); 170 .then((resolved) => new Pair(key, resolved)));
171 }); 171 });
172 return Future.wait(pairs).then((resolvedPairs) { 172 return Future.wait(pairs).then((resolvedPairs) {
173 var map = {}; 173 var map = {};
174 for (var pair in resolvedPairs) { 174 for (var pair in resolvedPairs) {
175 map[pair.first] = pair.last; 175 map[pair.first] = pair.last;
(...skipping 10 matching lines...) Expand all
186 } 186 }
187 187
188 /// Returns a string representation of [trace] that has the core and test frames 188 /// Returns a string representation of [trace] that has the core and test frames
189 /// folded together. 189 /// folded together.
190 String terseTraceString(StackTrace trace) { 190 String terseTraceString(StackTrace trace) {
191 return new Trace.from(trace).terse.foldFrames((frame) { 191 return new Trace.from(trace).terse.foldFrames((frame) {
192 return frame.package == 'scheduled_test' || frame.package == 'unittest' || 192 return frame.package == 'scheduled_test' || frame.package == 'unittest' ||
193 frame.isCore; 193 frame.isCore;
194 }).toString().trim(); 194 }).toString().trim();
195 } 195 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698