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 /** Escape the control characters in [string] so that they are visible. */ |
| 71 _addEscapedString(String string) { |
| 72 add("'"); |
| 73 for (var i = 0; i < string.length; i++) { |
| 74 add(_escape(string[i])); |
| 75 } |
| 76 add("'"); |
| 77 } |
| 78 |
| 79 /** Return the escaped form of a character [ch]. */ |
| 80 _escape(ch) { |
| 81 if (ch == "'") |
| 82 return "\'"; |
| 83 else if (ch == '\n') |
| 84 return '\\n'; |
| 85 else if (ch == '\r') |
| 86 return '\\r'; |
| 87 else if (ch == '\t') |
| 88 return '\\t'; |
| 89 else |
| 90 return ch; |
| 91 } |
| 92 } |
OLD | NEW |