Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 /** | |
| 5 * The default implementation of IDescription. This should rarely need | |
| 6 * substitution, although conceivably it is a place where other languages | |
| 7 * could be supported. | |
| 8 */ | |
| 9 | |
| 10 class StringDescription implements Description { | |
| 11 var _out = ''; | |
| 12 | |
| 13 /** | |
| 14 * Get the description as a string. | |
| 15 */ | |
| 16 String toString() => _out; | |
| 17 | |
| 18 /** | |
| 19 * Append some plain [text] to the description. | |
| 20 */ | |
| 21 Description add(String text) { | |
| 22 _out = '${_out}${text}'; | |
| 23 return this; | |
| 24 } | |
| 25 | |
| 26 /** | |
| 27 * Appends a description of [value]. If it is an IMatcher use its | |
| 28 * describe method; if it is a string use its literal value after | |
| 29 * escaping any embedded control characters; otherwise use its | |
| 30 * toString() value and wrap it in angular "quotes". | |
| 31 */ | |
| 32 Description addDescriptionOf(value) { | |
| 33 if (value is Matcher) { | |
| 34 value.describe(this); | |
| 35 } else if (value is String) { | |
| 36 _addEscapedString(value); | |
| 37 } else { | |
| 38 String description = (value == null) ? "null" : value.toString(); | |
| 39 if (description.startsWith('<') && description.endsWith('>')) { | |
| 40 add(description); | |
| 41 } else { | |
| 42 add('<'); | |
| 43 add(description); | |
| 44 add('>'); | |
| 45 } | |
| 46 } | |
| 47 return this; | |
| 48 } | |
| 49 | |
| 50 /** | |
| 51 * Append an [Iterable] [list] of objects to the description, using the | |
| 52 * specified [separator] and framing the list with [start] | |
| 53 * and [end]. | |
| 54 */ | |
| 55 Description addAll(String start, String separator, String end, | |
| 56 Iterable list) { | |
| 57 var separate = false; | |
| 58 add(start); | |
| 59 for (var item in list) { | |
| 60 if (separate) { | |
| 61 add(separator); | |
| 62 } | |
| 63 addDescriptionOf(item); | |
| 64 separate = true; | |
| 65 } | |
| 66 add(end); | |
| 67 return this; | |
| 68 } | |
| 69 | |
| 70 /** | |
| 71 * Escape the control characters in [string] so that they are visible. | |
| 72 */ | |
| 73 _addEscapedString(String string) { | |
| 74 add("'"); | |
| 75 for (var i = 0; i < string.length; i++) { | |
| 76 add(_escape(string[i])); | |
| 77 } | |
| 78 add("'"); | |
| 79 } | |
| 80 | |
| 81 /** | |
| 82 * Return the escaped form of a character [ch]. | |
| 83 */ | |
|
Siggi Cherem (dart-lang)
2012/06/06 00:26:08
style nit: for comments that fit in a single line,
gram
2012/06/06 16:23:56
Done.
| |
| 84 _escape(ch) { | |
| 85 if (ch == "'") | |
| 86 return "\'"; | |
| 87 else if (ch == '\n') | |
| 88 return '\\n'; | |
| 89 else if (ch == '\r') | |
| 90 return '\\r'; | |
| 91 else if (ch == '\t') | |
| 92 return '\\t'; | |
| 93 else | |
| 94 return ch; | |
| 95 } | |
| 96 } | |
| OLD | NEW |