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

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

Issue 119673002: Add a ScheduledStream class and some stream matchers. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years 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
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 one and only one of two types of values.
29 class Either<E, F> {
30 /// Whether this is a value of type `E`.
31 final bool isFirst;
32
33 /// Whether this is a value of type `F`.
34 bool get isSecond => !isFirst;
35
36 /// The value, either of type `E` or `F`.
37 final _value;
38
39 /// The value of type `E`.
40 ///
41 /// It's an error to access this is this is of type `F`.
Bob Nystrom 2013/12/21 00:11:34 "this is this" -> "this if the value"
nweiz 2014/01/07 03:16:39 N/A
42 E get first {
43 assert(isFirst);
44 return _value;
45 }
46
47 /// The value of type `F`.
48 ///
49 /// It's an error to access this is this is of type `E`.
Bob Nystrom 2013/12/21 00:11:34 Ditto.
nweiz 2014/01/07 03:16:39 N/A
50 F get second {
51 assert(isSecond);
52 return _value;
53 }
54
55 /// Creates an [Either] with type `E`.
56 Either.withFirst(this._value)
Bob Nystrom 2013/12/21 00:11:34 Maybe just Either.first()?
nweiz 2014/01/07 03:16:39 N/A
57 : isFirst = true;
58
59 /// Creates an [Either] with type `F`.
60 Either.withSecond(this._value)
61 : isFirst = false;
62
63 /// Runs [whenFirst] or [whenSecond] depending on the type of [this].
64 ///
65 /// Returns the result of whichvever function was run.
66 match(whenFirst(E value), whenSecond(F value)) {
67 if (isFirst) return whenFirst(first);
68 return whenSecond(second);
69 }
70
71 String toString() => "$_value (${isFirst? 'first' : 'second'})";
72 }
73
28 /// Configures [future] so that its result (success or exception) is passed on 74 /// Configures [future] so that its result (success or exception) is passed on
29 /// to [completer]. 75 /// to [completer].
30 void chainToCompleter(Future future, Completer completer) { 76 void chainToCompleter(Future future, Completer completer) {
31 future.then(completer.complete, onError: completer.completeError); 77 future.then(completer.complete, onError: completer.completeError);
32 } 78 }
33 79
34 /// Like [Future.sync], but wraps the Future in [Chain.track] as well. 80 /// Like [Future.sync], but wraps the Future in [Chain.track] as well.
35 Future syncFuture(callback()) => Chain.track(new Future.sync(callback)); 81 Future syncFuture(callback()) => Chain.track(new Future.sync(callback));
36 82
37 /// Prepends each line in [text] with [prefix]. If [firstPrefix] is passed, the 83 /// Prepends each line in [text] with [prefix]. If [firstPrefix] is passed, the
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after
224 /// Returns whether [pattern] matches all of [string]. 270 /// Returns whether [pattern] matches all of [string].
225 bool fullMatch(String string, Pattern pattern) { 271 bool fullMatch(String string, Pattern pattern) {
226 var matches = pattern.allMatches(string); 272 var matches = pattern.allMatches(string);
227 if (matches.isEmpty) return false; 273 if (matches.isEmpty) return false;
228 return matches.first.start == 0 && matches.first.end == string.length; 274 return matches.first.start == 0 && matches.first.end == string.length;
229 } 275 }
230 276
231 /// Returns a string representation of [trace] that has the core and test frames 277 /// Returns a string representation of [trace] that has the core and test frames
232 /// folded together. 278 /// folded together.
233 String terseTraceString(StackTrace trace) { 279 String terseTraceString(StackTrace trace) {
234 return new Chain(new Chain.forTrace(trace).traces.map((trace) { 280 return new Chain.forTrace(trace).terse.toString().trim();
235 return trace.foldFrames((frame) {
236 return frame.package == 'scheduled_test' ||
237 frame.package == 'unittest' ||
238 frame.package == 'stack_trace' ||
239 frame.isCore;
240 });
241 })).terse.toString().trim();
242 } 281 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698