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 test.utils; | 5 library test.utils; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 | 8 |
9 import 'package:path/path.dart' as p; | 9 import 'package:path/path.dart' as p; |
10 import 'package:stack_trace/stack_trace.dart'; | 10 import 'package:stack_trace/stack_trace.dart'; |
11 | 11 |
12 import 'backend/operating_system.dart'; | 12 import 'backend/operating_system.dart'; |
13 | 13 |
14 /// A typedef for a possibly-asynchronous function. | 14 /// A typedef for a possibly-asynchronous function. |
15 /// | 15 /// |
16 /// The return type should only ever by [Future] or void. | 16 /// The return type should only ever by [Future] or void. |
17 typedef AsyncFunction(); | 17 typedef AsyncFunction(); |
18 | 18 |
| 19 /// A typedef for a zero-argument callback function. |
| 20 typedef void Callback(); |
| 21 |
19 /// A regular expression to match the exception prefix that some exceptions' | 22 /// A regular expression to match the exception prefix that some exceptions' |
20 /// [Object.toString] values contain. | 23 /// [Object.toString] values contain. |
21 final _exceptionPrefix = new RegExp(r'^([A-Z][a-zA-Z]*)?(Exception|Error): '); | 24 final _exceptionPrefix = new RegExp(r'^([A-Z][a-zA-Z]*)?(Exception|Error): '); |
22 | 25 |
23 /// Directories that are specific to OS X. | 26 /// Directories that are specific to OS X. |
24 /// | 27 /// |
25 /// This is used to try to distinguish OS X and Linux in [currentOSGuess]. | 28 /// This is used to try to distinguish OS X and Linux in [currentOSGuess]. |
26 final _macOSDirectories = new Set<String>.from([ | 29 final _macOSDirectories = new Set<String>.from([ |
27 "/Applications", | 30 "/Applications", |
28 "/Library", | 31 "/Library", |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 | 137 |
135 // Otherwise truncate to return the trailing text, but attempt to start at | 138 // Otherwise truncate to return the trailing text, but attempt to start at |
136 // the beginning of a word. | 139 // the beginning of a word. |
137 var result = text.substring(text.length - maxLength + 4); | 140 var result = text.substring(text.length - maxLength + 4); |
138 var firstSpace = result.indexOf(' '); | 141 var firstSpace = result.indexOf(' '); |
139 if (firstSpace > 0) { | 142 if (firstSpace > 0) { |
140 result = result.substring(firstSpace); | 143 result = result.substring(firstSpace); |
141 } | 144 } |
142 return '...$result'; | 145 return '...$result'; |
143 } | 146 } |
| 147 |
| 148 /// Merges [streams] into a single stream that emits events from all sources. |
| 149 Stream mergeStreams(Iterable<Stream> streamIter) { |
| 150 var streams = streamIter.toList(); |
| 151 |
| 152 var subscriptions = new Set(); |
| 153 var controller; |
| 154 controller = new StreamController(sync: true, onListen: () { |
| 155 for (var stream in streams) { |
| 156 var subscription; |
| 157 subscription = stream.listen( |
| 158 controller.add, |
| 159 onError: controller.addError, |
| 160 onDone: () { |
| 161 subscriptions.remove(subscription); |
| 162 if (subscriptions.isEmpty) controller.close(); |
| 163 }); |
| 164 subscriptions.add(subscription); |
| 165 } |
| 166 }, onPause: () { |
| 167 for (var subscription in subscriptions) { |
| 168 subscription.pause(); |
| 169 } |
| 170 }, onResume: () { |
| 171 for (var subscription in subscriptions) { |
| 172 subscription.resume(); |
| 173 } |
| 174 }, onCancel: () { |
| 175 for (var subscription in subscriptions) { |
| 176 subscription.cancel(); |
| 177 } |
| 178 }); |
| 179 |
| 180 return controller.stream; |
| 181 } |
OLD | NEW |