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; | |
6 | |
7 import 'description.dart'; | 5 import 'description.dart'; |
8 import 'interfaces.dart'; | 6 import 'interfaces.dart'; |
9 import 'util.dart'; | 7 import 'util.dart'; |
10 | 8 |
11 /// Returns a pretty-printed representation of [object]. | 9 /// Returns a pretty-printed representation of [object]. |
12 /// | 10 /// |
13 /// If [maxLineLength] is passed, this will attempt to ensure that each line is | 11 /// 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 | 12 /// no longer than [maxLineLength] characters long. This isn't guaranteed, since |
15 /// individual objects may have string representations that are too long, but | 13 /// individual objects may have string representations that are too long, but |
16 /// most lines will be less than [maxLineLength] long. | 14 /// most lines will be less than [maxLineLength] long. |
(...skipping 27 matching lines...) Expand all Loading... |
44 // If the printed string is short and doesn't contain a newline, print it | 42 // If the printed string is short and doesn't contain a newline, print it |
45 // as a single line. | 43 // as a single line. |
46 var singleLine = "$type[${strings.join(', ')}]"; | 44 var singleLine = "$type[${strings.join(', ')}]"; |
47 if ((maxLineLength == null || | 45 if ((maxLineLength == null || |
48 singleLine.length + indent <= maxLineLength) && | 46 singleLine.length + indent <= maxLineLength) && |
49 !singleLine.contains("\n")) { | 47 !singleLine.contains("\n")) { |
50 return singleLine; | 48 return singleLine; |
51 } | 49 } |
52 | 50 |
53 // Otherwise, print each member on its own line. | 51 // Otherwise, print each member on its own line. |
54 return "$type[\n" + strings.map((string) { | 52 return "$type[\n" + |
55 return _indent(indent + 2) + string; | 53 strings.map((string) { |
56 }).join(",\n") + "\n" + _indent(indent) + "]"; | 54 return _indent(indent + 2) + string; |
| 55 }).join(",\n") + |
| 56 "\n" + |
| 57 _indent(indent) + |
| 58 "]"; |
57 } else if (object is Map) { | 59 } else if (object is Map) { |
58 // Convert the contents of the map to string representations. | 60 // Convert the contents of the map to string representations. |
59 var strings = object.keys.map((key) { | 61 var strings = object.keys.map((key) { |
60 return '${pp(key)}: ${pp(object[key])}'; | 62 return '${pp(key)}: ${pp(object[key])}'; |
61 }).toList(); | 63 }).toList(); |
62 | 64 |
63 // Truncate the list of strings if it's longer than [maxItems]. | 65 // Truncate the list of strings if it's longer than [maxItems]. |
64 if (maxItems != null && strings.length > maxItems) { | 66 if (maxItems != null && strings.length > maxItems) { |
65 strings.replaceRange(maxItems - 1, strings.length, ['...']); | 67 strings.replaceRange(maxItems - 1, strings.length, ['...']); |
66 } | 68 } |
67 | 69 |
68 // If the printed string is short and doesn't contain a newline, print it | 70 // If the printed string is short and doesn't contain a newline, print it |
69 // as a single line. | 71 // as a single line. |
70 var singleLine = "{${strings.join(", ")}}"; | 72 var singleLine = "{${strings.join(", ")}}"; |
71 if ((maxLineLength == null || | 73 if ((maxLineLength == null || |
72 singleLine.length + indent <= maxLineLength) && | 74 singleLine.length + indent <= maxLineLength) && |
73 !singleLine.contains("\n")) { | 75 !singleLine.contains("\n")) { |
74 return singleLine; | 76 return singleLine; |
75 } | 77 } |
76 | 78 |
77 // Otherwise, print each key/value pair on its own line. | 79 // Otherwise, print each key/value pair on its own line. |
78 return "{\n" + strings.map((string) { | 80 return "{\n" + |
79 return _indent(indent + 2) + string; | 81 strings.map((string) { |
80 }).join(",\n") + "\n" + _indent(indent) + "}"; | 82 return _indent(indent + 2) + string; |
| 83 }).join(",\n") + |
| 84 "\n" + |
| 85 _indent(indent) + |
| 86 "}"; |
81 } else if (object is String) { | 87 } else if (object is String) { |
82 // Escape strings and print each line on its own line. | 88 // Escape strings and print each line on its own line. |
83 var lines = object.split("\n"); | 89 var lines = object.split("\n"); |
84 return "'" + | 90 return "'" + |
85 lines.map(_escapeString).join("\\n'\n${_indent(indent + 2)}'") + | 91 lines.map(_escapeString).join("\\n'\n${_indent(indent + 2)}'") + |
86 "'"; | 92 "'"; |
87 } else { | 93 } else { |
88 var value = object.toString().replaceAll("\n", _indent(indent) + "\n"); | 94 var value = object.toString().replaceAll("\n", _indent(indent) + "\n"); |
89 var defaultToString = value.startsWith("Instance of "); | 95 var defaultToString = value.startsWith("Instance of "); |
90 | 96 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 return "?"; | 133 return "?"; |
128 } | 134 } |
129 } | 135 } |
130 | 136 |
131 /// Returns [source] with any control characters replaced by their escape | 137 /// Returns [source] with any control characters replaced by their escape |
132 /// sequences. | 138 /// sequences. |
133 /// | 139 /// |
134 /// This doesn't add quotes to the string, but it does escape single quote | 140 /// This doesn't add quotes to the string, but it does escape single quote |
135 /// characters so that single quotes can be applied externally. | 141 /// characters so that single quotes can be applied externally. |
136 String _escapeString(String source) => escape(source).replaceAll("'", r"\'"); | 142 String _escapeString(String source) => escape(source).replaceAll("'", r"\'"); |
OLD | NEW |