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.pretty_print; | 5 library unittest.pretty_print; |
6 | 6 |
7 import '../matcher.dart'; | |
Siggi Cherem (dart-lang)
2014/02/28 02:41:04
use package: ?
Bob Nystrom
2014/02/28 17:31:24
Do you think that's better? It seems a bit weird t
Siggi Cherem (dart-lang)
2014/02/28 17:37:19
Not really.
I think in general is a bad idea to u
| |
7 import 'utils.dart'; | 8 import 'utils.dart'; |
8 | 9 |
9 /** | 10 /** |
10 * Returns a pretty-printed representation of [object]. | 11 * Returns a pretty-printed representation of [object]. |
11 * | 12 * |
12 * If [maxLineLength] is passed, this will attempt to ensure that each line is | 13 * If [maxLineLength] is passed, this will attempt to ensure that each line is |
13 * no longer than [maxLineLength] characters long. This isn't guaranteed, since | 14 * no longer than [maxLineLength] characters long. This isn't guaranteed, since |
14 * individual objects may have string representations that are too long, but | 15 * individual objects may have string representations that are too long, but |
15 * most lines will be less than [maxLineLength] long. | 16 * most lines will be less than [maxLineLength] long. |
16 * | 17 * |
17 * If [maxItems] is passed, [Iterable]s and [Map]s will only print their first | 18 * If [maxItems] is passed, [Iterable]s and [Map]s will only print their first |
18 * [maxItems] members or key/value pairs, respectively. | 19 * [maxItems] members or key/value pairs, respectively. |
19 */ | 20 */ |
20 String prettyPrint(object, {int maxLineLength, int maxItems}) { | 21 String prettyPrint(object, {int maxLineLength, int maxItems}) { |
21 String _prettyPrint(object, int indent, Set seen, bool top) { | 22 String _prettyPrint(object, int indent, Set seen, bool top) { |
23 // If the object is a matcher, use its description. | |
24 if (object is Matcher) { | |
25 var description = new StringDescription(); | |
26 object.describe(description); | |
27 return "<$description>"; | |
28 } | |
29 | |
22 // Avoid looping infinitely on recursively-nested data structures. | 30 // Avoid looping infinitely on recursively-nested data structures. |
23 if (seen.contains(object)) return "(recursive)"; | 31 if (seen.contains(object)) return "(recursive)"; |
24 seen = seen.union(new Set.from([object])); | 32 seen = seen.union(new Set.from([object])); |
25 String pp(child) => _prettyPrint(child, indent + 2, seen, false); | 33 String pp(child) => _prettyPrint(child, indent + 2, seen, false); |
26 | 34 |
27 if (object is Iterable) { | 35 if (object is Iterable) { |
28 // Print the type name for non-List iterables. | 36 // Print the type name for non-List iterables. |
29 var type = object is List ? "" : typeName(object) + ":"; | 37 var type = object is List ? "" : typeName(object) + ":"; |
30 | 38 |
31 // Truncate the list of strings if it's longer than [maxItems]. | 39 // Truncate the list of strings if it's longer than [maxItems]. |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
93 } else { | 101 } else { |
94 return "${typeName(object)}:$value"; | 102 return "${typeName(object)}:$value"; |
95 } | 103 } |
96 } | 104 } |
97 } | 105 } |
98 | 106 |
99 return _prettyPrint(object, 0, new Set(), true); | 107 return _prettyPrint(object, 0, new Set(), true); |
100 } | 108 } |
101 | 109 |
102 String _indent(int length) => new List.filled(length, ' ').join(''); | 110 String _indent(int length) => new List.filled(length, ' ').join(''); |
OLD | NEW |