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

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: Added co19 issue number. 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 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 156
157 /// Takes a simple data structure (composed of [Map]s, [Iterable]s, scalar 157 /// Takes a simple data structure (composed of [Map]s, [Iterable]s, scalar
158 /// objects, and [Future]s) and recursively resolves all the [Future]s contained 158 /// objects, and [Future]s) and recursively resolves all the [Future]s contained
159 /// within. Completes with the fully resolved structure. 159 /// within. Completes with the fully resolved structure.
160 Future awaitObject(object) { 160 Future awaitObject(object) {
161 // Unroll nested futures. 161 // Unroll nested futures.
162 if (object is Future) return object.then(awaitObject); 162 if (object is Future) return object.then(awaitObject);
163 if (object is Iterable) { 163 if (object is Iterable) {
164 return Future.wait(object.map(awaitObject).toList()); 164 return Future.wait(object.map(awaitObject).toList());
165 } 165 }
166 if (object is! Map) return new Future.immediate(object); 166 if (object is! Map) return new Future.value(object);
167 167
168 var pairs = <Future<Pair>>[]; 168 var pairs = <Future<Pair>>[];
169 object.forEach((key, value) { 169 object.forEach((key, value) {
170 pairs.add(awaitObject(value) 170 pairs.add(awaitObject(value)
171 .then((resolved) => new Pair(key, resolved))); 171 .then((resolved) => new Pair(key, resolved)));
172 }); 172 });
173 return Future.wait(pairs).then((resolvedPairs) { 173 return Future.wait(pairs).then((resolvedPairs) {
174 var map = {}; 174 var map = {};
175 for (var pair in resolvedPairs) { 175 for (var pair in resolvedPairs) {
176 map[pair.first] = pair.last; 176 map[pair.first] = pair.last;
(...skipping 10 matching lines...) Expand all
187 } 187 }
188 188
189 /// Returns a string representation of [trace] that has the core and test frames 189 /// Returns a string representation of [trace] that has the core and test frames
190 /// folded together. 190 /// folded together.
191 String terseTraceString(StackTrace trace) { 191 String terseTraceString(StackTrace trace) {
192 return new Trace.from(trace).terse.foldFrames((frame) { 192 return new Trace.from(trace).terse.foldFrames((frame) {
193 return frame.package == 'scheduled_test' || frame.package == 'unittest' || 193 return frame.package == 'scheduled_test' || frame.package == 'unittest' ||
194 frame.isCore; 194 frame.isCore;
195 }).toString().trim(); 195 }).toString().trim();
196 } 196 }
OLDNEW
« no previous file with comments | « pkg/scheduled_test/lib/src/task.dart ('k') | pkg/scheduled_test/test/descriptor/async_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698