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

Unified 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: code review Created 6 years, 11 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 side-by-side diff with in-line comments
Download patch
Index: pkg/scheduled_test/lib/src/utils.dart
diff --git a/pkg/scheduled_test/lib/src/utils.dart b/pkg/scheduled_test/lib/src/utils.dart
index 9cba0447190a1b440d4f9af8dd721abad11f1229..9b65cd1751fe37746ad67a59009741ec5a5ff597 100644
--- a/pkg/scheduled_test/lib/src/utils.dart
+++ b/pkg/scheduled_test/lib/src/utils.dart
@@ -25,6 +25,72 @@ class Pair<E, F> {
int get hashCode => first.hashCode ^ last.hashCode;
}
+/// A class that represents a value or an error.
+class FallibleValue<E> {
+ /// Whether [this] has a [value], as opposed to an [error].
+ final bool hasValue;
+
+ /// Whether [this] has an [error], as opposed to an[value].
+ bool get hasError => !hasValue;
+
+ /// The value.
+ ///
+ /// This will be `null` if [this] has an [error].
+ final E _value;
+
+ /// The value.
+ ///
+ /// This will throw a [StateError] if [this] has an [error].
+ E get value {
+ if (hasValue) return _value;
+ throw new StateError("FallibleValue has no value.\n"
+ "$_error$_stackTraceSuffix");
+ }
+
+ /// The error.
+ ///
+ /// This will be `null` if [this] has a [value].
+ final _error;
+
+ /// The error.
+ ///
+ /// This will throw a [StateError] if [this] has a [value].
+ get error {
+ if (hasError) return _error;
+ throw new StateError("FallibleValue has no error.");
+ }
+
+ /// The stack trace for [_error].
+ ///
+ /// This will be `null` if [this] has a [value], or if no stack trace was
+ /// provided.
+ final StackTrace _stackTrace;
+
+ /// The stack trace for [error].
+ ///
+ /// This will throw a [StateError] if [this] has a [value].
+ StackTrace get stackTrace {
+ if (hasError) return _stackTrace;
+ throw new StateError("FallibleValue has no error.");
+ }
+
+ FallibleValue.withValue(this._value)
+ : hasValue = true;
+
+ FallibleValue.withError(this._error, [this._stackTrace])
+ : hasValue = false;
+
+ String toString() {
+ if (hasValue) return "FallibleValue value: $value";
+ return "FallibleValue error: $error$_stackTraceSuffix";
+ }
+
+ String get _stackTraceSuffix {
+ if (stackTrace == null) return "";
+ return "\nStack trace:\n${new Chain.from(_stackTrace).terse}";
+ }
+}
+
/// Configures [future] so that its result (success or exception) is passed on
/// to [completer].
void chainToCompleter(Future future, Completer completer) {
@@ -231,12 +297,5 @@ bool fullMatch(String string, Pattern pattern) {
/// Returns a string representation of [trace] that has the core and test frames
/// folded together.
String terseTraceString(StackTrace trace) {
- return new Chain(new Chain.forTrace(trace).traces.map((trace) {
- return trace.foldFrames((frame) {
- return frame.package == 'scheduled_test' ||
- frame.package == 'unittest' ||
- frame.package == 'stack_trace' ||
- frame.isCore;
- });
- })).terse.toString().trim();
+ return new Chain.forTrace(trace).terse.toString().trim();
}

Powered by Google App Engine
This is Rietveld 408576698