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