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 matcher.pretty_print; | 5 library matcher.pretty_print; |
6 | 6 |
7 import 'description.dart'; | 7 import 'description.dart'; |
8 import 'interfaces.dart'; | 8 import 'interfaces.dart'; |
9 | 9 |
10 /// Returns a pretty-printed representation of [object]. | 10 /// Returns a pretty-printed representation of [object]. |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 return singleLine; | 73 return singleLine; |
74 } | 74 } |
75 | 75 |
76 // Otherwise, print each key/value pair on its own line. | 76 // Otherwise, print each key/value pair on its own line. |
77 return "{\n" + strings.map((string) { | 77 return "{\n" + strings.map((string) { |
78 return _indent(indent + 2) + string; | 78 return _indent(indent + 2) + string; |
79 }).join(",\n") + "\n" + _indent(indent) + "}"; | 79 }).join(",\n") + "\n" + _indent(indent) + "}"; |
80 } else if (object is String) { | 80 } else if (object is String) { |
81 // Escape strings and print each line on its own line. | 81 // Escape strings and print each line on its own line. |
82 var lines = object.split("\n"); | 82 var lines = object.split("\n"); |
83 return "'" + lines.map(_escapeString) | 83 return "'" + |
84 .join("\\n'\n${_indent(indent + 2)}'") + "'"; | 84 lines.map(_escapeString).join("\\n'\n${_indent(indent + 2)}'") + |
| 85 "'"; |
85 } else { | 86 } else { |
86 var value = object.toString().replaceAll("\n", _indent(indent) + "\n"); | 87 var value = object.toString().replaceAll("\n", _indent(indent) + "\n"); |
87 var defaultToString = value.startsWith("Instance of "); | 88 var defaultToString = value.startsWith("Instance of "); |
88 | 89 |
89 // If this is the top-level call to [prettyPrint], wrap the value on angle | 90 // If this is the top-level call to [prettyPrint], wrap the value on angle |
90 // brackets to set it apart visually. | 91 // brackets to set it apart visually. |
91 if (top) value = "<$value>"; | 92 if (top) value = "<$value>"; |
92 | 93 |
93 // Print the type of objects with custom [toString] methods. Primitive | 94 // 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 // objects and objects that don't implement a custom [toString] don't need |
95 // to have their types printed. | 96 // to have their types printed. |
96 if (object is num || object is bool || object is Function || | 97 if (object is num || |
97 object == null || defaultToString) { | 98 object is bool || |
| 99 object is Function || |
| 100 object == null || |
| 101 defaultToString) { |
98 return value; | 102 return value; |
99 } else { | 103 } else { |
100 return "${_typeName(object)}:$value"; | 104 return "${_typeName(object)}:$value"; |
101 } | 105 } |
102 } | 106 } |
103 } | 107 } |
104 | 108 |
105 return _prettyPrint(object, 0, new Set(), true); | 109 return _prettyPrint(object, 0, new Set(), true); |
106 } | 110 } |
107 | 111 |
(...skipping 14 matching lines...) Expand all Loading... |
122 return "?"; | 126 return "?"; |
123 } | 127 } |
124 } | 128 } |
125 | 129 |
126 /// Returns [source] with any control characters replaced by their escape | 130 /// Returns [source] with any control characters replaced by their escape |
127 /// sequences. | 131 /// sequences. |
128 /// | 132 /// |
129 /// This doesn't add quotes to the string, but it does escape single quote | 133 /// This doesn't add quotes to the string, but it does escape single quote |
130 /// characters so that single quotes can be applied externally. | 134 /// characters so that single quotes can be applied externally. |
131 String _escapeString(String source) => | 135 String _escapeString(String source) => |
132 source.split("").map(_escapeChar).join(""); | 136 source.split("").map(_escapeChar).join(""); |
133 | 137 |
134 /// Return the escaped form of a character [ch]. | 138 /// Return the escaped form of a character [ch]. |
135 String _escapeChar(String ch) { | 139 String _escapeChar(String ch) { |
136 if (ch == "'") | 140 switch (ch) { |
137 return "\\'"; | 141 case "'": |
138 else if (ch == '\n') | 142 return "\\'"; |
139 return '\\n'; | 143 case '\n': |
140 else if (ch == '\r') | 144 return '\\n'; |
141 return '\\r'; | 145 case '\r': |
142 else if (ch == '\t') | 146 return '\\r'; |
143 return '\\t'; | 147 case '\t': |
144 else | 148 return '\\t'; |
145 return ch; | 149 default: |
| 150 return ch; |
| 151 } |
146 } | 152 } |
147 | |
OLD | NEW |