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 /** Initialize the description with initial contents [init]. */ | |
14 StringDescription([String init = '']) { | |
15 _out = init; | |
16 } | |
17 | |
18 /** Get the description as a string. */ | |
19 String toString() => _out; | |
20 | |
21 /** Append some plain [text] to the description. */ | |
22 Description add(String text) { | |
23 _out = '${_out}${text}'; | |
24 return this; | |
25 } | |
26 | |
27 /** Change the value of the description. */ | |
28 Description replace(String text) { | |
29 _out = text; | |
30 return this; | |
31 } | |
32 | |
33 /** | |
34 * Appends a description of [value]. If it is an IMatcher use its | |
35 * describe method; if it is a string use its literal value after | |
36 * escaping any embedded control characters; otherwise use its | |
37 * toString() value and wrap it in angular "quotes". | |
38 */ | |
39 Description addDescriptionOf(value) { | |
40 if (value is Matcher) { | |
41 value.describe(this); | |
42 } else if (value is String) { | |
43 _addEscapedString(value); | |
44 } else { | |
45 String description = (value == null) ? "null" : value.toString(); | |
46 if (description.startsWith('<') && description.endsWith('>')) { | |
47 add(description); | |
48 } else { | |
49 add('<'); | |
50 add(description); | |
51 add('>'); | |
52 } | |
53 } | |
54 return this; | |
55 } | |
56 | |
57 /** | |
58 * Append an [Iterable] [list] of objects to the description, using the | |
59 * specified [separator] and framing the list with [start] | |
60 * and [end]. | |
61 */ | |
62 Description addAll(String start, String separator, String end, | |
63 Iterable list) { | |
64 var separate = false; | |
65 add(start); | |
66 for (var item in list) { | |
67 if (separate) { | |
68 add(separator); | |
69 } | |
70 addDescriptionOf(item); | |
71 separate = true; | |
72 } | |
73 add(end); | |
74 return this; | |
75 } | |
76 | |
77 /** Escape the control characters in [string] so that they are visible. */ | |
78 _addEscapedString(String string) { | |
79 add("'"); | |
80 for (var i = 0; i < string.length; i++) { | |
81 add(_escape(string[i])); | |
82 } | |
83 add("'"); | |
84 } | |
85 | |
86 /** Return the escaped form of a character [ch]. */ | |
87 _escape(ch) { | |
88 if (ch == "'") | |
89 return "\'"; | |
90 else if (ch == '\n') | |
91 return '\\n'; | |
92 else if (ch == '\r') | |
93 return '\\r'; | |
94 else if (ch == '\t') | |
95 return '\\t'; | |
96 else | |
97 return ch; | |
98 } | |
99 } | |
OLD | NEW |