Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(325)

Side by Side Diff: pkg/matcher/lib/src/pretty_print.dart

Issue 313563002: pkg/matcher: Reverting 36881,36896 while investigating dart2js checked crash (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « pkg/matcher/lib/src/operator_matchers.dart ('k') | pkg/matcher/lib/src/string_matchers.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 import 'utils.dart';
9 10
10 /// Returns a pretty-printed representation of [object]. 11 /**
11 /// 12 * Returns a pretty-printed representation of [object].
12 /// If [maxLineLength] is passed, this will attempt to ensure that each line is 13 *
13 /// no longer than [maxLineLength] characters long. This isn't guaranteed, since 14 * If [maxLineLength] is passed, this will attempt to ensure that each line is
14 /// individual objects may have string representations that are too long, but 15 * no longer than [maxLineLength] characters long. This isn't guaranteed, since
15 /// most lines will be less than [maxLineLength] long. 16 * individual objects may have string representations that are too long, but
16 /// 17 * most lines will be less than [maxLineLength] long.
17 /// If [maxItems] is passed, [Iterable]s and [Map]s will only print their first 18 *
18 /// [maxItems] members or key/value pairs, respectively. 19 * If [maxItems] is passed, [Iterable]s and [Map]s will only print their first
20 * [maxItems] members or key/value pairs, respectively.
21 */
19 String prettyPrint(object, {int maxLineLength, int maxItems}) { 22 String prettyPrint(object, {int maxLineLength, int maxItems}) {
20 String _prettyPrint(object, int indent, Set seen, bool top) { 23 String _prettyPrint(object, int indent, Set seen, bool top) {
21 // If the object is a matcher, use its description. 24 // If the object is a matcher, use its description.
22 if (object is Matcher) { 25 if (object is Matcher) {
23 var description = new StringDescription(); 26 var description = new StringDescription();
24 object.describe(description); 27 object.describe(description);
25 return "<$description>"; 28 return "<$description>";
26 } 29 }
27 30
28 // Avoid looping infinitely on recursively-nested data structures. 31 // Avoid looping infinitely on recursively-nested data structures.
29 if (seen.contains(object)) return "(recursive)"; 32 if (seen.contains(object)) return "(recursive)";
30 seen = seen.union(new Set.from([object])); 33 seen = seen.union(new Set.from([object]));
31 String pp(child) => _prettyPrint(child, indent + 2, seen, false); 34 String pp(child) => _prettyPrint(child, indent + 2, seen, false);
32 35
33 if (object is Iterable) { 36 if (object is Iterable) {
34 // Print the type name for non-List iterables. 37 // Print the type name for non-List iterables.
35 var type = object is List ? "" : _typeName(object) + ":"; 38 var type = object is List ? "" : typeName(object) + ":";
36 39
37 // Truncate the list of strings if it's longer than [maxItems]. 40 // Truncate the list of strings if it's longer than [maxItems].
38 var strings = object.map(pp).toList(); 41 var strings = object.map(pp).toList();
39 if (maxItems != null && strings.length > maxItems) { 42 if (maxItems != null && strings.length > maxItems) {
40 strings.replaceRange(maxItems - 1, strings.length, ['...']); 43 strings.replaceRange(maxItems - 1, strings.length, ['...']);
41 } 44 }
42 45
43 // If the printed string is short and doesn't contain a newline, print it 46 // If the printed string is short and doesn't contain a newline, print it
44 // as a single line. 47 // as a single line.
45 var singleLine = "$type[${strings.join(', ')}]"; 48 var singleLine = "$type[${strings.join(', ')}]";
(...skipping 27 matching lines...) Expand all
73 return singleLine; 76 return singleLine;
74 } 77 }
75 78
76 // Otherwise, print each key/value pair on its own line. 79 // Otherwise, print each key/value pair on its own line.
77 return "{\n" + strings.map((string) { 80 return "{\n" + strings.map((string) {
78 return _indent(indent + 2) + string; 81 return _indent(indent + 2) + string;
79 }).join(",\n") + "\n" + _indent(indent) + "}"; 82 }).join(",\n") + "\n" + _indent(indent) + "}";
80 } else if (object is String) { 83 } else if (object is String) {
81 // Escape strings and print each line on its own line. 84 // Escape strings and print each line on its own line.
82 var lines = object.split("\n"); 85 var lines = object.split("\n");
83 return "'" + lines.map(_escapeString) 86 return "'" + lines.map(escapeString)
84 .join("\\n'\n${_indent(indent + 2)}'") + "'"; 87 .join("\\n'\n${_indent(indent + 2)}'") + "'";
85 } else { 88 } else {
86 var value = object.toString().replaceAll("\n", _indent(indent) + "\n"); 89 var value = object.toString().replaceAll("\n", _indent(indent) + "\n");
87 var defaultToString = value.startsWith("Instance of "); 90 var defaultToString = value.startsWith("Instance of ");
88 91
89 // If this is the top-level call to [prettyPrint], wrap the value on angle 92 // If this is the top-level call to [prettyPrint], wrap the value on angle
90 // brackets to set it apart visually. 93 // brackets to set it apart visually.
91 if (top) value = "<$value>"; 94 if (top) value = "<$value>";
92 95
93 // Print the type of objects with custom [toString] methods. Primitive 96 // Print the type of objects with custom [toString] methods. Primitive
94 // objects and objects that don't implement a custom [toString] don't need 97 // objects and objects that don't implement a custom [toString] don't need
95 // to have their types printed. 98 // to have their types printed.
96 if (object is num || object is bool || object is Function || 99 if (object is num || object is bool || object is Function ||
97 object == null || defaultToString) { 100 object == null || defaultToString) {
98 return value; 101 return value;
99 } else { 102 } else {
100 return "${_typeName(object)}:$value"; 103 return "${typeName(object)}:$value";
101 } 104 }
102 } 105 }
103 } 106 }
104 107
105 return _prettyPrint(object, 0, new Set(), true); 108 return _prettyPrint(object, 0, new Set(), true);
106 } 109 }
107 110
108 String _indent(int length) => new List.filled(length, ' ').join(''); 111 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
OLDNEW
« no previous file with comments | « pkg/matcher/lib/src/operator_matchers.dart ('k') | pkg/matcher/lib/src/string_matchers.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698