OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library unittest.pretty_print; | |
6 | |
7 import '../matcher.dart'; | |
8 import 'utils.dart'; | |
9 | |
10 /** | |
11 * Returns a pretty-printed representation of [object]. | |
12 * | |
13 * If [maxLineLength] is passed, this will attempt to ensure that each line is | |
14 * no longer than [maxLineLength] characters long. This isn't guaranteed, since | |
15 * individual objects may have string representations that are too long, but | |
16 * most lines will be less than [maxLineLength] long. | |
17 * | |
18 * If [maxItems] is passed, [Iterable]s and [Map]s will only print their first | |
19 * [maxItems] members or key/value pairs, respectively. | |
20 */ | |
21 String prettyPrint(object, {int maxLineLength, int maxItems}) { | |
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 | |
30 // Avoid looping infinitely on recursively-nested data structures. | |
31 if (seen.contains(object)) return "(recursive)"; | |
32 seen = seen.union(new Set.from([object])); | |
33 String pp(child) => _prettyPrint(child, indent + 2, seen, false); | |
34 | |
35 if (object is Iterable) { | |
36 // Print the type name for non-List iterables. | |
37 var type = object is List ? "" : typeName(object) + ":"; | |
38 | |
39 // Truncate the list of strings if it's longer than [maxItems]. | |
40 var strings = object.map(pp).toList(); | |
41 if (maxItems != null && strings.length > maxItems) { | |
42 strings.replaceRange(maxItems - 1, strings.length, ['...']); | |
43 } | |
44 | |
45 // If the printed string is short and doesn't contain a newline, print it | |
46 // as a single line. | |
47 var singleLine = "$type[${strings.join(', ')}]"; | |
48 if ((maxLineLength == null || | |
49 singleLine.length + indent <= maxLineLength) && | |
50 !singleLine.contains("\n")) { | |
51 return singleLine; | |
52 } | |
53 | |
54 // Otherwise, print each member on its own line. | |
55 return "$type[\n" + strings.map((string) { | |
56 return _indent(indent + 2) + string; | |
57 }).join(",\n") + "\n" + _indent(indent) + "]"; | |
58 } else if (object is Map) { | |
59 // Convert the contents of the map to string representations. | |
60 var strings = object.keys.map((key) { | |
61 return '${pp(key)}: ${pp(object[key])}'; | |
62 }).toList(); | |
63 | |
64 // Truncate the list of strings if it's longer than [maxItems]. | |
65 if (maxItems != null && strings.length > maxItems) { | |
66 strings.replaceRange(maxItems - 1, strings.length, ['...']); | |
67 } | |
68 | |
69 // If the printed string is short and doesn't contain a newline, print it | |
70 // as a single line. | |
71 var singleLine = "{${strings.join(", ")}}"; | |
72 if ((maxLineLength == null || | |
73 singleLine.length + indent <= maxLineLength) && | |
74 !singleLine.contains("\n")) { | |
75 return singleLine; | |
76 } | |
77 | |
78 // Otherwise, print each key/value pair on its own line. | |
79 return "{\n" + strings.map((string) { | |
80 return _indent(indent + 2) + string; | |
81 }).join(",\n") + "\n" + _indent(indent) + "}"; | |
82 } else if (object is String) { | |
83 // Escape strings and print each line on its own line. | |
84 var lines = object.split("\n"); | |
85 return "'" + lines.map(escapeString) | |
86 .join("\\n'\n${_indent(indent + 2)}'") + "'"; | |
87 } else { | |
88 var value = object.toString().replaceAll("\n", _indent(indent) + "\n"); | |
89 var defaultToString = value.startsWith("Instance of "); | |
90 | |
91 // If this is the top-level call to [prettyPrint], wrap the value on angle | |
92 // brackets to set it apart visually. | |
93 if (top) value = "<$value>"; | |
94 | |
95 // Print the type of objects with custom [toString] methods. Primitive | |
96 // objects and objects that don't implement a custom [toString] don't need | |
97 // to have their types printed. | |
98 if (object is num || object is bool || object is Function || | |
99 object == null || defaultToString) { | |
100 return value; | |
101 } else { | |
102 return "${typeName(object)}:$value"; | |
103 } | |
104 } | |
105 } | |
106 | |
107 return _prettyPrint(object, 0, new Set(), true); | |
108 } | |
109 | |
110 String _indent(int length) => new List.filled(length, ' ').join(''); | |
OLD | NEW |