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 matcher.pretty_print; |
| 6 |
| 7 import 'description.dart'; |
| 8 import 'interfaces.dart'; |
| 9 import 'util.dart'; |
| 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 String prettyPrint(object, {int maxLineLength, int maxItems}) { |
| 21 String _prettyPrint(object, int indent, Set seen, bool top) { |
| 22 // If the object is a matcher, use its description. |
| 23 if (object is Matcher) { |
| 24 var description = new StringDescription(); |
| 25 object.describe(description); |
| 26 return "<$description>"; |
| 27 } |
| 28 |
| 29 // Avoid looping infinitely on recursively-nested data structures. |
| 30 if (seen.contains(object)) return "(recursive)"; |
| 31 seen = seen.union(new Set.from([object])); |
| 32 String pp(child) => _prettyPrint(child, indent + 2, seen, false); |
| 33 |
| 34 if (object is Iterable) { |
| 35 // Print the type name for non-List iterables. |
| 36 var type = object is List ? "" : _typeName(object) + ":"; |
| 37 |
| 38 // Truncate the list of strings if it's longer than [maxItems]. |
| 39 var strings = object.map(pp).toList(); |
| 40 if (maxItems != null && strings.length > maxItems) { |
| 41 strings.replaceRange(maxItems - 1, strings.length, ['...']); |
| 42 } |
| 43 |
| 44 // If the printed string is short and doesn't contain a newline, print it |
| 45 // as a single line. |
| 46 var singleLine = "$type[${strings.join(', ')}]"; |
| 47 if ((maxLineLength == null || |
| 48 singleLine.length + indent <= maxLineLength) && |
| 49 !singleLine.contains("\n")) { |
| 50 return singleLine; |
| 51 } |
| 52 |
| 53 // Otherwise, print each member on its own line. |
| 54 return "$type[\n" + strings.map((string) { |
| 55 return _indent(indent + 2) + string; |
| 56 }).join(",\n") + "\n" + _indent(indent) + "]"; |
| 57 } else if (object is Map) { |
| 58 // Convert the contents of the map to string representations. |
| 59 var strings = object.keys.map((key) { |
| 60 return '${pp(key)}: ${pp(object[key])}'; |
| 61 }).toList(); |
| 62 |
| 63 // Truncate the list of strings if it's longer than [maxItems]. |
| 64 if (maxItems != null && strings.length > maxItems) { |
| 65 strings.replaceRange(maxItems - 1, strings.length, ['...']); |
| 66 } |
| 67 |
| 68 // If the printed string is short and doesn't contain a newline, print it |
| 69 // as a single line. |
| 70 var singleLine = "{${strings.join(", ")}}"; |
| 71 if ((maxLineLength == null || |
| 72 singleLine.length + indent <= maxLineLength) && |
| 73 !singleLine.contains("\n")) { |
| 74 return singleLine; |
| 75 } |
| 76 |
| 77 // Otherwise, print each key/value pair on its own line. |
| 78 return "{\n" + strings.map((string) { |
| 79 return _indent(indent + 2) + string; |
| 80 }).join(",\n") + "\n" + _indent(indent) + "}"; |
| 81 } else if (object is String) { |
| 82 // Escape strings and print each line on its own line. |
| 83 var lines = object.split("\n"); |
| 84 return "'" + |
| 85 lines.map(_escapeString).join("\\n'\n${_indent(indent + 2)}'") + |
| 86 "'"; |
| 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 || |
| 99 object is bool || |
| 100 object is Function || |
| 101 object == null || |
| 102 defaultToString) { |
| 103 return value; |
| 104 } else { |
| 105 return "${_typeName(object)}:$value"; |
| 106 } |
| 107 } |
| 108 } |
| 109 |
| 110 return _prettyPrint(object, 0, new Set(), true); |
| 111 } |
| 112 |
| 113 String _indent(int length) => new List.filled(length, ' ').join(''); |
| 114 |
| 115 /// Returns the name of the type of [x], or "Unknown" if the type name can't be |
| 116 /// determined. |
| 117 String _typeName(x) { |
| 118 // dart2js blows up on some objects (e.g. window.navigator). |
| 119 // So we play safe here. |
| 120 try { |
| 121 if (x == null) return "null"; |
| 122 var type = x.runtimeType.toString(); |
| 123 // TODO(nweiz): if the object's type is private, find a public superclass to |
| 124 // display once there's a portable API to do that. |
| 125 return type.startsWith("_") ? "?" : type; |
| 126 } catch (e) { |
| 127 return "?"; |
| 128 } |
| 129 } |
| 130 |
| 131 /// Returns [source] with any control characters replaced by their escape |
| 132 /// sequences. |
| 133 /// |
| 134 /// This doesn't add quotes to the string, but it does escape single quote |
| 135 /// characters so that single quotes can be applied externally. |
| 136 String _escapeString(String source) => escape(source).replaceAll("'", r"\'"); |
OLD | NEW |