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

Unified Diff: lib/src/utils.dart

Issue 934413002: Replace the existing unittest APIs with the new runner infrastructure. (Closed) Base URL: git@github.com:dart-lang/unittest@master
Patch Set: Created 5 years, 10 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: lib/src/utils.dart
diff --git a/lib/src/utils.dart b/lib/src/utils.dart
index da2192764a1af63e56830205f26e5129dc3acd06..6b0939fe5ccf805bcf9ffb33e5e9d86b045eb1c1 100644
--- a/lib/src/utils.dart
+++ b/lib/src/utils.dart
@@ -13,48 +13,38 @@ import 'package:stack_trace/stack_trace.dart';
/// The return type should only ever by [Future] or void.
typedef AsyncFunction();
+/// A regular expression to match the exception prefix that some exceptions'
+/// [Object.toString] values contain.
+final _exceptionPrefix = new RegExp(r'^([A-Z][a-zA-Z]*)?(Exception|Error): ');
+
+/// Get a string description of an exception.
+///
+/// Many exceptions include the exception class name at the beginning of their
+/// [toString], so we remove that if it exists.
+String getErrorMessage(error) =>
+ error.toString().replaceFirst(_exceptionPrefix, '');
+
/// Indent each line in [str] by two spaces.
String indent(String str) =>
str.replaceAll(new RegExp("^", multiLine: true), " ");
-/// A pair of values.
-class Pair<E, F> {
- final E first;
- final F last;
-
- Pair(this.first, this.last);
-
- String toString() => '($first, $last)';
-
- bool operator ==(other) {
- if (other is! Pair) return false;
- return other.first == first && other.last == last;
- }
-
- int get hashCode => first.hashCode ^ last.hashCode;
-}
-
-/// Returns a Trace object from a StackTrace object or a String, or the
-/// unchanged input if formatStacks is false;
-Trace getTrace(stack, bool formatStacks, bool filterStacks) {
- Trace trace;
- if (stack == null || !formatStacks) return null;
- if (stack is String) {
- trace = new Trace.parse(stack);
- } else if (stack is StackTrace) {
- trace = new Trace.from(stack);
- } else {
- throw new Exception('Invalid stack type ${stack.runtimeType} for $stack.');
- }
+/// A regular expression matching the path to a temporary file used to start an
+/// isolate.
+///
+/// These paths aren't relevant and are removed from stack traces.
+final _isolatePath =
+ new RegExp(r"/unittest_[A-Za-z0-9]{6}/runInIsolate\.dart$");
- if (!filterStacks) return trace;
+/// Returns [stackTrace] converted to a [Chain] with all irrelevant frames
+/// folded together.
+Chain terseChain(StackTrace stackTrace) {
+ return new Chain.forTrace(stackTrace).foldFrames((frame) {
+ if (frame.package == 'unittest') return true;
- // Format the stack trace by removing everything above TestCase._runTest,
- // which is usually going to be irrelevant. Also fold together unittest and
- // core library calls so only the function the user called is visible.
- return new Trace(trace.frames.takeWhile((frame) {
- return frame.package != 'unittest' || frame.member != 'TestCase._runTest';
- })).terse.foldFrames((frame) => frame.package == 'unittest' || frame.isCore);
+ // Filter out frames from our isolate bootstrap as well.
+ if (frame.uri.scheme != 'file') return false;
+ return frame.uri.path.contains(_isolatePath);
+ }, terse: true);
}
/// Flattens nested [Iterable]s inside an [Iterable] into a single [List]
« no previous file with comments | « lib/src/throws_matcher.dart ('k') | lib/src/vm_listener.dart » ('j') | lib/unittest.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698