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 /** | 7 /// Returns the name of the type of [x], or "Unknown" if the type name can't be |
8 * Returns the name of the type of [x], or "Unknown" if the type name can't be | 8 /// determined. |
9 * determined. | |
10 */ | |
11 String typeName(x) { | 9 String typeName(x) { |
12 // dart2js blows up on some objects (e.g. window.navigator). | 10 // dart2js blows up on some objects (e.g. window.navigator). |
13 // So we play safe here. | 11 // So we play safe here. |
14 try { | 12 try { |
15 if (x == null) return "null"; | 13 if (x == null) return "null"; |
16 var type = x.runtimeType.toString(); | 14 var type = x.runtimeType.toString(); |
17 // TODO(nweiz): if the object's type is private, find a public superclass to | 15 // TODO(nweiz): if the object's type is private, find a public superclass to |
18 // display once there's a portable API to do that. | 16 // display once there's a portable API to do that. |
19 return type.startsWith("_") ? "?" : type; | 17 return type.startsWith("_") ? "?" : type; |
20 } catch (e) { | 18 } catch (e) { |
21 return "?"; | 19 return "?"; |
22 } | 20 } |
23 } | 21 } |
24 | 22 |
25 /** | 23 /// Returns [source] with any control characters replaced by their escape |
26 * Returns [source] with any control characters replaced by their escape | 24 /// sequences. |
27 * sequences. | 25 /// |
28 * | 26 /// This doesn't add quotes to the string, but it does escape single quote |
29 * This doesn't add quotes to the string, but it does escape single quote | 27 /// characters so that single quotes can be applied externally. |
30 * characters so that single quotes can be applied externally. | |
31 */ | |
32 String escapeString(String source) => | 28 String escapeString(String source) => |
33 source.split("").map(_escapeChar).join(""); | 29 source.split("").map(_escapeChar).join(""); |
34 | 30 |
35 /** Return the escaped form of a character [ch]. */ | 31 /// Return the escaped form of a character [ch]. |
36 String _escapeChar(String ch) { | 32 String _escapeChar(String ch) { |
37 if (ch == "'") | 33 if (ch == "'") |
38 return "\\'"; | 34 return "\\'"; |
39 else if (ch == '\n') | 35 else if (ch == '\n') |
40 return '\\n'; | 36 return '\\n'; |
41 else if (ch == '\r') | 37 else if (ch == '\r') |
42 return '\\r'; | 38 return '\\r'; |
43 else if (ch == '\t') | 39 else if (ch == '\t') |
44 return '\\t'; | 40 return '\\t'; |
45 else | 41 else |
46 return ch; | 42 return ch; |
47 } | 43 } |
48 | 44 |
49 /** Indent each line in [str] by two spaces. */ | 45 /// Indent each line in [str] by two spaces. |
50 String indent(String str) => | 46 String indent(String str) => |
51 str.replaceAll(new RegExp("^", multiLine: true), " "); | 47 str.replaceAll(new RegExp("^", multiLine: true), " "); |
52 | 48 |
53 /** A pair of values. */ | 49 /// A pair of values. |
54 class Pair<E, F> { | 50 class Pair<E, F> { |
55 E first; | 51 final E first; |
56 F last; | 52 final F last; |
57 | 53 |
58 Pair(this.first, this.last); | 54 Pair(this.first, this.last); |
59 | 55 |
60 String toString() => '($first, $last)'; | 56 String toString() => '($first, $last)'; |
61 | 57 |
62 bool operator ==(other) { | 58 bool operator ==(other) { |
63 if (other is! Pair) return false; | 59 if (other is! Pair) return false; |
64 return other.first == first && other.last == last; | 60 return other.first == first && other.last == last; |
65 } | 61 } |
66 | 62 |
67 int get hashCode => first.hashCode ^ last.hashCode; | 63 int get hashCode => first.hashCode ^ last.hashCode; |
68 } | 64 } |
69 | 65 |
OLD | NEW |