Chromium Code Reviews

Side by Side Diff: pkg/unittest/lib/src/description.dart

Issue 14600029: Add pretty-printing to unittest and more thoroughly print values. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Code review changes. Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 part of matcher; 5 part of matcher;
6 6
7 /** 7 /**
8 * The default implementation of IDescription. This should rarely need 8 * The default implementation of IDescription. This should rarely need
9 * substitution, although conceivably it is a place where other languages 9 * substitution, although conceivably it is a place where other languages
10 * could be supported. 10 * could be supported.
(...skipping 23 matching lines...)
34 34
35 /** 35 /**
36 * Appends a description of [value]. If it is an IMatcher use its 36 * Appends a description of [value]. If it is an IMatcher use its
37 * describe method; if it is a string use its literal value after 37 * describe method; if it is a string use its literal value after
38 * escaping any embedded control characters; otherwise use its 38 * escaping any embedded control characters; otherwise use its
39 * toString() value and wrap it in angular "quotes". 39 * toString() value and wrap it in angular "quotes".
40 */ 40 */
41 Description addDescriptionOf(value) { 41 Description addDescriptionOf(value) {
42 if (value is Matcher) { 42 if (value is Matcher) {
43 value.describe(this); 43 value.describe(this);
44 } else if (value is String) {
45 _addEscapedString(value);
46 } else { 44 } else {
47 String description = (value == null) ? "null" : value.toString(); 45 add(prettyPrint(value, maxLineLength: 80, maxItems: 25));
48 if (description.startsWith('<') && description.endsWith('>')) {
49 add(description);
50 } else if (description.startsWith("Instance of")) {
51 add('<');
52 add(description);
53 add(':');
54 add(value.hashCode.toString());
55 add('>');
56 } else {
57 add('<');
58 add(description);
59 add('>');
60 }
61 } 46 }
62 return this; 47 return this;
63 } 48 }
64 49
65 /** 50 /**
66 * Append an [Iterable] [list] of objects to the description, using the 51 * Append an [Iterable] [list] of objects to the description, using the
67 * specified [separator] and framing the list with [start] 52 * specified [separator] and framing the list with [start]
68 * and [end]. 53 * and [end].
69 */ 54 */
70 Description addAll(String start, String separator, String end, 55 Description addAll(String start, String separator, String end,
71 Iterable list) { 56 Iterable list) {
72 var separate = false; 57 var separate = false;
73 add(start); 58 add(start);
74 for (var item in list) { 59 for (var item in list) {
75 if (separate) { 60 if (separate) {
76 add(separator); 61 add(separator);
77 } 62 }
78 addDescriptionOf(item); 63 addDescriptionOf(item);
79 separate = true; 64 separate = true;
80 } 65 }
81 add(end); 66 add(end);
82 return this; 67 return this;
83 } 68 }
84 69
85 /** Escape the control characters in [string] so that they are visible. */ 70 /** Escape the control characters in [string] so that they are visible. */
86 _addEscapedString(String string) { 71 _addEscapedString(String string) {
87 add("'"); 72 add("'");
88 for (var i = 0; i < string.length; i++) { 73 add(escapeString(string));
89 add(_escape(string[i]));
90 }
91 add("'"); 74 add("'");
92 } 75 }
93
94 /** Return the escaped form of a character [ch]. */
95 _escape(ch) {
96 if (ch == "'")
97 return "\'";
98 else if (ch == '\n')
99 return '\\n';
100 else if (ch == '\r')
101 return '\\r';
102 else if (ch == '\t')
103 return '\\t';
104 else
105 return ch;
106 }
107 } 76 }
OLDNEW

Powered by Google App Engine