| 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 |  | 
| 10 /// Returns a pretty-printed representation of [object]. |  | 
| 11 /// |  | 
| 12 /// 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 /// individual objects may have string representations that are too long, but |  | 
| 15 /// most lines will be less than [maxLineLength] long. |  | 
| 16 /// |  | 
| 17 /// If [maxItems] is passed, [Iterable]s and [Map]s will only print their first |  | 
| 18 /// [maxItems] members or key/value pairs, respectively. |  | 
| 19 String prettyPrint(object, {int maxLineLength, int maxItems}) { |  | 
| 20   String _prettyPrint(object, int indent, Set seen, bool top) { |  | 
| 21     // If the object is a matcher, use its description. |  | 
| 22     if (object is Matcher) { |  | 
| 23       var description = new StringDescription(); |  | 
| 24       object.describe(description); |  | 
| 25       return "<$description>"; |  | 
| 26     } |  | 
| 27 |  | 
| 28     // Avoid looping infinitely on recursively-nested data structures. |  | 
| 29     if (seen.contains(object)) return "(recursive)"; |  | 
| 30     seen = seen.union(new Set.from([object])); |  | 
| 31     String pp(child) => _prettyPrint(child, indent + 2, seen, false); |  | 
| 32 |  | 
| 33     if (object is Iterable) { |  | 
| 34       // Print the type name for non-List iterables. |  | 
| 35       var type = object is List ? "" : _typeName(object) + ":"; |  | 
| 36 |  | 
| 37       // Truncate the list of strings if it's longer than [maxItems]. |  | 
| 38       var strings = object.map(pp).toList(); |  | 
| 39       if (maxItems != null && strings.length > maxItems) { |  | 
| 40         strings.replaceRange(maxItems - 1, strings.length, ['...']); |  | 
| 41       } |  | 
| 42 |  | 
| 43       // If the printed string is short and doesn't contain a newline, print it |  | 
| 44       // as a single line. |  | 
| 45       var singleLine = "$type[${strings.join(', ')}]"; |  | 
| 46       if ((maxLineLength == null || |  | 
| 47               singleLine.length + indent <= maxLineLength) && |  | 
| 48           !singleLine.contains("\n")) { |  | 
| 49         return singleLine; |  | 
| 50       } |  | 
| 51 |  | 
| 52       // Otherwise, print each member on its own line. |  | 
| 53       return "$type[\n" + strings.map((string) { |  | 
| 54         return _indent(indent + 2) + string; |  | 
| 55       }).join(",\n") + "\n" + _indent(indent) + "]"; |  | 
| 56     } else if (object is Map) { |  | 
| 57       // Convert the contents of the map to string representations. |  | 
| 58       var strings = object.keys.map((key) { |  | 
| 59         return '${pp(key)}: ${pp(object[key])}'; |  | 
| 60       }).toList(); |  | 
| 61 |  | 
| 62       // Truncate the list of strings if it's longer than [maxItems]. |  | 
| 63       if (maxItems != null && strings.length > maxItems) { |  | 
| 64         strings.replaceRange(maxItems - 1, strings.length, ['...']); |  | 
| 65       } |  | 
| 66 |  | 
| 67       // If the printed string is short and doesn't contain a newline, print it |  | 
| 68       // as a single line. |  | 
| 69       var singleLine = "{${strings.join(", ")}}"; |  | 
| 70       if ((maxLineLength == null || |  | 
| 71               singleLine.length + indent <= maxLineLength) && |  | 
| 72           !singleLine.contains("\n")) { |  | 
| 73         return singleLine; |  | 
| 74       } |  | 
| 75 |  | 
| 76       // Otherwise, print each key/value pair on its own line. |  | 
| 77       return "{\n" + strings.map((string) { |  | 
| 78         return _indent(indent + 2) + string; |  | 
| 79       }).join(",\n") + "\n" + _indent(indent) + "}"; |  | 
| 80     } else if (object is String) { |  | 
| 81       // Escape strings and print each line on its own line. |  | 
| 82       var lines = object.split("\n"); |  | 
| 83       return "'" + lines.map(_escapeString) |  | 
| 84           .join("\\n'\n${_indent(indent + 2)}'") + "'"; |  | 
| 85     } else { |  | 
| 86       var value = object.toString().replaceAll("\n", _indent(indent) + "\n"); |  | 
| 87       var defaultToString = value.startsWith("Instance of "); |  | 
| 88 |  | 
| 89       // If this is the top-level call to [prettyPrint], wrap the value on angle |  | 
| 90       // brackets to set it apart visually. |  | 
| 91       if (top) value = "<$value>"; |  | 
| 92 |  | 
| 93       // Print the type of objects with custom [toString] methods. Primitive |  | 
| 94       // objects and objects that don't implement a custom [toString] don't need |  | 
| 95       // to have their types printed. |  | 
| 96       if (object is num || object is bool || object is Function || |  | 
| 97           object == null || defaultToString) { |  | 
| 98         return value; |  | 
| 99       } else { |  | 
| 100         return "${_typeName(object)}:$value"; |  | 
| 101       } |  | 
| 102     } |  | 
| 103   } |  | 
| 104 |  | 
| 105   return _prettyPrint(object, 0, new Set(), true); |  | 
| 106 } |  | 
| 107 |  | 
| 108 String _indent(int length) => new List.filled(length, ' ').join(''); |  | 
| 109 |  | 
| 110 /// Returns the name of the type of [x], or "Unknown" if the type name can't be |  | 
| 111 /// determined. |  | 
| 112 String _typeName(x) { |  | 
| 113   // dart2js blows up on some objects (e.g. window.navigator). |  | 
| 114   // So we play safe here. |  | 
| 115   try { |  | 
| 116     if (x == null) return "null"; |  | 
| 117     var type = x.runtimeType.toString(); |  | 
| 118     // TODO(nweiz): if the object's type is private, find a public superclass to |  | 
| 119     // display once there's a portable API to do that. |  | 
| 120     return type.startsWith("_") ? "?" : type; |  | 
| 121   } catch (e) { |  | 
| 122     return "?"; |  | 
| 123   } |  | 
| 124 } |  | 
| 125 |  | 
| 126 /// Returns [source] with any control characters replaced by their escape |  | 
| 127 /// sequences. |  | 
| 128 /// |  | 
| 129 /// This doesn't add quotes to the string, but it does escape single quote |  | 
| 130 /// characters so that single quotes can be applied externally. |  | 
| 131 String _escapeString(String source) => |  | 
| 132   source.split("").map(_escapeChar).join(""); |  | 
| 133 |  | 
| 134 /// Return the escaped form of a character [ch]. |  | 
| 135 String _escapeChar(String ch) { |  | 
| 136   if (ch == "'") |  | 
| 137     return "\\'"; |  | 
| 138   else if (ch == '\n') |  | 
| 139     return '\\n'; |  | 
| 140   else if (ch == '\r') |  | 
| 141     return '\\r'; |  | 
| 142   else if (ch == '\t') |  | 
| 143     return '\\t'; |  | 
| 144   else |  | 
| 145     return ch; |  | 
| 146 } |  | 
| 147 |  | 
| OLD | NEW | 
|---|