Chromium Code Reviews| Index: pkg/unittest/lib/src/pretty_print.dart |
| diff --git a/pkg/unittest/lib/src/pretty_print.dart b/pkg/unittest/lib/src/pretty_print.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..cad27e482786ce721113a746f354b5076ddc8c17 |
| --- /dev/null |
| +++ b/pkg/unittest/lib/src/pretty_print.dart |
| @@ -0,0 +1,87 @@ |
| +// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +library pretty_print; |
| + |
| +import 'utils.dart'; |
| + |
| +/** |
| + * Returns a pretty-printed representation of [object]. |
| + * |
| + * If [maxLineLength] is passed, this will attempt to ensure that each line is |
| + * no longer than [maxLineLength] characters long. This isn't guaranteed, since |
| + * individual objects may have string representations that are too long, but |
| + * most lines will be less than [maxLineLength] long. |
| + * |
| + * If [maxItems] is passed, [Iterable]s and [Map]s will only print their first |
| + * [maxItems] members or key/value pairs, respectively. |
| + */ |
| +String prettyPrint(object, {int maxLineLength, int maxItems}) { |
| + String _prettyPrint(object, int indent, Set seen, bool top) { |
| + if (seen.contains(object)) return "(recursive)"; |
| + seen = seen.union(new Set.from([object])); |
| + String pp(child) => _prettyPrint(child, indent + 2, seen, false); |
| + |
| + if (object is Iterable) { |
| + var typeName = object is List ? "" : typeName(object) + ":"; |
| + |
|
gram
2013/05/17 01:00:30
Please add a short comment at the start of each of
nweiz
2013/05/17 19:50:56
Done.
|
| + var strings = object.map(pp).toList(); |
| + if (maxItems != null && strings.length > maxItems) { |
| + strings.replaceRange(maxItems - 1, strings.length, ['...']); |
| + } |
| + |
| + var singleLine = "$typeName[${strings.join(', ')}]"; |
| + if ((maxLineLength == null || |
| + singleLine.length + indent <= maxLineLength) && |
| + !singleLine.contains("\n")) { |
| + return singleLine; |
| + } |
| + |
| + return "$typeName[\n" + strings.map((string) { |
| + return _indent(indent + 2) + string; |
| + }).join(",\n") + "\n" + _indent(indent) + "]"; |
| + } else if (object is Map) { |
| + var strings = object.keys.map((key) { |
| + return '${pp(key)}: ${pp(object[key])}'; |
| + }).toList(); |
| + if (maxItems != null && strings.length > maxItems) { |
| + strings.replaceRange(maxItems - 1, strings.length, ['...']); |
| + } |
| + |
| + var singleLine = "{${strings.join(", ")}}"; |
| + if ((maxLineLength == null || |
| + singleLine.length + indent <= maxLineLength) && |
| + !singleLine.contains("\n")) { |
| + return singleLine; |
| + } |
| + |
| + return "{\n" + strings.map((string) { |
| + return _indent(indent + 2) + string; |
| + }).join(",\n") + "\n" + _indent(indent) + "}"; |
| + } else if (object is String) { |
| + var lines = object.split("\n"); |
| + return "'" + lines.map(escapeString) |
| + .join("\\n'\n${_indent(indent + 2)}'") + "'"; |
| + } else { |
| + var value = object.toString().replaceAll("\n", _indent(indent) + "\n"); |
| + var defaultToString = value.startsWith("Instance of "); |
| + if (top) value = "<$value>"; |
| + |
| + if (object is num || object is bool || object is Function || |
| + object == null || defaultToString) { |
| + return value; |
| + } else { |
| + var type = typeName(object); |
| + // TODO(nweiz): find a public superclass of the object's type to |
| + // display once there's a portable API to do that. |
| + if (type.startsWith("_")) return value; |
| + return "$type:$value"; |
| + } |
| + } |
| + } |
| + |
| + return _prettyPrint(object, 0, new Set(), true); |
| +} |
| + |
| +String _indent(int length) => new List.filled(length, ' ').join(''); |