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 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 |
11 /// A pair of values. | 11 /// A pair of values. |
12 class Pair<E, F> { | 12 class Pair<E, F> { |
13 E first; | 13 E first; |
14 F last; | 14 F last; |
15 | 15 |
16 Pair(this.first, this.last); | 16 Pair(this.first, this.last); |
17 | 17 |
18 String toString() => '($first, $last)'; | 18 String toString() => '($first, $last)'; |
19 | 19 |
20 bool operator==(other) { | 20 bool operator ==(other) { |
21 if (other is! Pair) return false; | 21 if (other is! Pair) return false; |
22 return other.first == first && other.last == last; | 22 return other.first == first && other.last == last; |
23 } | 23 } |
24 | 24 |
25 int get hashCode => first.hashCode ^ last.hashCode; | 25 int get hashCode => first.hashCode ^ last.hashCode; |
26 } | 26 } |
27 | 27 |
28 /// A class that represents a value or an error. | 28 /// A class that represents a value or an error. |
29 class Fallible<E> { | 29 class Fallible<E> { |
30 /// Whether [this] has a [value], as opposed to an [error]. | 30 /// Whether [this] has a [value], as opposed to an [error]. |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 | 119 |
120 var firstLine = "$firstPrefix${lines.first}"; | 120 var firstLine = "$firstPrefix${lines.first}"; |
121 lines = lines.skip(1).map((line) => '$prefix$line').toList(); | 121 lines = lines.skip(1).map((line) => '$prefix$line').toList(); |
122 lines.insert(0, firstLine); | 122 lines.insert(0, firstLine); |
123 return lines.join('\n'); | 123 return lines.join('\n'); |
124 } | 124 } |
125 | 125 |
126 /// Returns a [Future] that completes after pumping the event queue [times] | 126 /// Returns a [Future] that completes after pumping the event queue [times] |
127 /// times. By default, this should pump the event queue enough times to allow | 127 /// times. By default, this should pump the event queue enough times to allow |
128 /// any code to run, as long as it's not waiting on some external event. | 128 /// any code to run, as long as it's not waiting on some external event. |
129 Future pumpEventQueue([int times=20]) { | 129 Future pumpEventQueue([int times = 20]) { |
130 if (times == 0) return new Future.value(); | 130 if (times == 0) return new Future.value(); |
131 // We use a delayed future to allow microtask events to finish. The | 131 // We use a delayed future to allow microtask events to finish. The |
132 // Future.value or Future() constructors use scheduleMicrotask themselves and | 132 // Future.value or Future() constructors use scheduleMicrotask themselves and |
133 // would therefore not wait for microtask callbacks that are scheduled after | 133 // would therefore not wait for microtask callbacks that are scheduled after |
134 // invoking this method. | 134 // invoking this method. |
135 return new Future.delayed(Duration.ZERO, () => pumpEventQueue(times - 1)); | 135 return new Future.delayed(Duration.ZERO, () => pumpEventQueue(times - 1)); |
136 } | 136 } |
137 | 137 |
138 /// Returns whether [iterable1] has the same elements in the same order as | 138 /// Returns whether [iterable1] has the same elements in the same order as |
139 /// [iterable2]. The elements are compared using `==`. | 139 /// [iterable2]. The elements are compared using `==`. |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
301 var matches = pattern.allMatches(string); | 301 var matches = pattern.allMatches(string); |
302 if (matches.isEmpty) return false; | 302 if (matches.isEmpty) return false; |
303 return matches.first.start == 0 && matches.first.end == string.length; | 303 return matches.first.start == 0 && matches.first.end == string.length; |
304 } | 304 } |
305 | 305 |
306 /// Returns a string representation of [trace] that has the core and test frames | 306 /// Returns a string representation of [trace] that has the core and test frames |
307 /// folded together. | 307 /// folded together. |
308 String terseTraceString(StackTrace trace) { | 308 String terseTraceString(StackTrace trace) { |
309 return new Chain.forTrace(trace).terse.toString().trim(); | 309 return new Chain.forTrace(trace).terse.toString().trim(); |
310 } | 310 } |
OLD | NEW |