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 unittest.utils; | 5 library unittest.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 typedef for a possibly-asynchronous function. | 11 /// A typedef for a possibly-asynchronous function. |
12 /// | 12 /// |
13 /// The return type should only ever by [Future] or void. | 13 /// The return type should only ever by [Future] or void. |
14 typedef AsyncFunction(); | 14 typedef AsyncFunction(); |
15 | 15 |
| 16 /// A regular expression to match the exception prefix that some exceptions' |
| 17 /// [Object.toString] values contain. |
| 18 final _exceptionPrefix = new RegExp(r'^([A-Z][a-zA-Z]*)?(Exception|Error): '); |
| 19 |
| 20 /// Get a string description of an exception. |
| 21 /// |
| 22 /// Many exceptions include the exception class name at the beginning of their |
| 23 /// [toString], so we remove that if it exists. |
| 24 String getErrorMessage(error) => |
| 25 error.toString().replaceFirst(_exceptionPrefix, ''); |
| 26 |
16 /// Indent each line in [str] by two spaces. | 27 /// Indent each line in [str] by two spaces. |
17 String indent(String str) => | 28 String indent(String str) => |
18 str.replaceAll(new RegExp("^", multiLine: true), " "); | 29 str.replaceAll(new RegExp("^", multiLine: true), " "); |
19 | 30 |
20 /// A pair of values. | 31 /// A pair of values. |
21 class Pair<E, F> { | 32 class Pair<E, F> { |
22 final E first; | 33 final E first; |
23 final F last; | 34 final F last; |
24 | 35 |
25 Pair(this.first, this.last); | 36 Pair(this.first, this.last); |
26 | 37 |
27 String toString() => '($first, $last)'; | 38 String toString() => '($first, $last)'; |
28 | 39 |
29 bool operator ==(other) { | 40 bool operator ==(other) { |
30 if (other is! Pair) return false; | 41 if (other is! Pair) return false; |
31 return other.first == first && other.last == last; | 42 return other.first == first && other.last == last; |
32 } | 43 } |
33 | 44 |
34 int get hashCode => first.hashCode ^ last.hashCode; | 45 int get hashCode => first.hashCode ^ last.hashCode; |
35 } | 46 } |
36 | 47 |
| 48 /// A regular expression matching the path to a temporary file used to start an |
| 49 /// isolate. |
| 50 /// |
| 51 /// These paths aren't relevant and are removed from stack traces. |
| 52 final _isolatePath = |
| 53 new RegExp(r"/unittest_[A-Za-z0-9]{6}/runInIsolate\.dart$"); |
| 54 |
| 55 /// Returns [stackTrace] converted to a [Chain] with all irrelevant frames |
| 56 /// folded together. |
| 57 Chain terseChain(StackTrace stackTrace) { |
| 58 return new Chain.forTrace(stackTrace).foldFrames((frame) { |
| 59 if (frame.package == 'unittest') return true; |
| 60 |
| 61 // Filter out frames from our isolate bootstrap as well. |
| 62 if (frame.uri.scheme != 'file') return false; |
| 63 return frame.uri.path.contains(_isolatePath); |
| 64 }, terse: true); |
| 65 } |
| 66 |
37 /// Returns a Trace object from a StackTrace object or a String, or the | 67 /// Returns a Trace object from a StackTrace object or a String, or the |
38 /// unchanged input if formatStacks is false; | 68 /// unchanged input if formatStacks is false; |
39 Trace getTrace(stack, bool formatStacks, bool filterStacks) { | 69 Trace getTrace(stack, bool formatStacks, bool filterStacks) { |
40 Trace trace; | 70 Trace trace; |
41 if (stack == null || !formatStacks) return null; | 71 if (stack == null || !formatStacks) return null; |
42 if (stack is String) { | 72 if (stack is String) { |
43 trace = new Trace.parse(stack); | 73 trace = new Trace.parse(stack); |
44 } else if (stack is StackTrace) { | 74 } else if (stack is StackTrace) { |
45 trace = new Trace.from(stack); | 75 trace = new Trace.from(stack); |
46 } else { | 76 } else { |
(...skipping 19 matching lines...) Expand all Loading... |
66 if (element is Iterable) { | 96 if (element is Iterable) { |
67 helper(element); | 97 helper(element); |
68 } else { | 98 } else { |
69 result.add(element); | 99 result.add(element); |
70 } | 100 } |
71 } | 101 } |
72 } | 102 } |
73 helper(nested); | 103 helper(nested); |
74 return result; | 104 return result; |
75 } | 105 } |
OLD | NEW |