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 regular expression matching the path to a temporary file used to start an |
21 class Pair<E, F> { | 32 /// isolate. |
22 final E first; | 33 /// |
23 final F last; | 34 /// These paths aren't relevant and are removed from stack traces. |
| 35 final _isolatePath = |
| 36 new RegExp(r"/unittest_[A-Za-z0-9]{6}/runInIsolate\.dart$"); |
24 | 37 |
25 Pair(this.first, this.last); | 38 /// Returns [stackTrace] converted to a [Chain] with all irrelevant frames |
| 39 /// folded together. |
| 40 Chain terseChain(StackTrace stackTrace) { |
| 41 return new Chain.forTrace(stackTrace).foldFrames((frame) { |
| 42 if (frame.package == 'unittest') return true; |
26 | 43 |
27 String toString() => '($first, $last)'; | 44 // Filter out frames from our isolate bootstrap as well. |
28 | 45 if (frame.uri.scheme != 'file') return false; |
29 bool operator ==(other) { | 46 return frame.uri.path.contains(_isolatePath); |
30 if (other is! Pair) return false; | 47 }, terse: true); |
31 return other.first == first && other.last == last; | |
32 } | |
33 | |
34 int get hashCode => first.hashCode ^ last.hashCode; | |
35 } | |
36 | |
37 /// Returns a Trace object from a StackTrace object or a String, or the | |
38 /// unchanged input if formatStacks is false; | |
39 Trace getTrace(stack, bool formatStacks, bool filterStacks) { | |
40 Trace trace; | |
41 if (stack == null || !formatStacks) return null; | |
42 if (stack is String) { | |
43 trace = new Trace.parse(stack); | |
44 } else if (stack is StackTrace) { | |
45 trace = new Trace.from(stack); | |
46 } else { | |
47 throw new Exception('Invalid stack type ${stack.runtimeType} for $stack.'); | |
48 } | |
49 | |
50 if (!filterStacks) return trace; | |
51 | |
52 // Format the stack trace by removing everything above TestCase._runTest, | |
53 // which is usually going to be irrelevant. Also fold together unittest and | |
54 // core library calls so only the function the user called is visible. | |
55 return new Trace(trace.frames.takeWhile((frame) { | |
56 return frame.package != 'unittest' || frame.member != 'TestCase._runTest'; | |
57 })).terse.foldFrames((frame) => frame.package == 'unittest' || frame.isCore); | |
58 } | 48 } |
59 | 49 |
60 /// Flattens nested [Iterable]s inside an [Iterable] into a single [List] | 50 /// Flattens nested [Iterable]s inside an [Iterable] into a single [List] |
61 /// containing only non-[Iterable] elements. | 51 /// containing only non-[Iterable] elements. |
62 List flatten(Iterable nested) { | 52 List flatten(Iterable nested) { |
63 var result = []; | 53 var result = []; |
64 helper(iter) { | 54 helper(iter) { |
65 for (var element in iter) { | 55 for (var element in iter) { |
66 if (element is Iterable) { | 56 if (element is Iterable) { |
67 helper(element); | 57 helper(element); |
68 } else { | 58 } else { |
69 result.add(element); | 59 result.add(element); |
70 } | 60 } |
71 } | 61 } |
72 } | 62 } |
73 helper(nested); | 63 helper(nested); |
74 return result; | 64 return result; |
75 } | 65 } |
OLD | NEW |