OLD | NEW |
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 library matcher.description; | 5 library matcher.description; |
6 | 6 |
7 import 'interfaces.dart'; | 7 import 'interfaces.dart'; |
8 import 'pretty_print.dart'; | 8 import 'pretty_print.dart'; |
9 import 'utils.dart'; | |
10 | 9 |
11 /** | 10 /// The default implementation of [Description]. This should rarely need |
12 * The default implementation of IDescription. This should rarely need | 11 /// substitution, although conceivably it is a place where other languages |
13 * substitution, although conceivably it is a place where other languages | 12 /// could be supported. |
14 * could be supported. | |
15 */ | |
16 class StringDescription implements Description { | 13 class StringDescription implements Description { |
17 var _out; | 14 final StringBuffer _out = new StringBuffer(); |
18 | 15 |
19 /** Initialize the description with initial contents [init]. */ | 16 /// Initialize the description with initial contents [init]. |
20 StringDescription([String init = '']) { | 17 StringDescription([String init = '']) { |
21 _out = init; | 18 _out.write(init); |
22 } | 19 } |
23 | 20 |
24 int get length => _out.length; | 21 int get length => _out.length; |
25 | 22 |
26 /** Get the description as a string. */ | 23 /// Get the description as a string. |
27 String toString() => _out; | 24 String toString() => _out.toString(); |
28 | 25 |
29 /** Append [text] to the description. */ | 26 /// Append [text] to the description. |
30 Description add(text) { | 27 Description add(String text) { |
31 _out = '${_out}${text}'; | 28 _out.write(text); |
32 return this; | 29 return this; |
33 } | 30 } |
34 | 31 |
35 /** Change the value of the description. */ | 32 /// Change the value of the description. |
36 Description replace(String text) { | 33 Description replace(String text) { |
37 _out = text; | 34 _out.clear(); |
38 return this; | 35 return add(text); |
39 } | 36 } |
40 | 37 |
41 /** | 38 /// Appends a description of [value]. If it is an IMatcher use its |
42 * Appends a description of [value]. If it is an IMatcher use its | 39 /// describe method; if it is a string use its literal value after |
43 * describe method; if it is a string use its literal value after | 40 /// escaping any embedded control characters; otherwise use its |
44 * escaping any embedded control characters; otherwise use its | 41 /// toString() value and wrap it in angular "quotes". |
45 * toString() value and wrap it in angular "quotes". | |
46 */ | |
47 Description addDescriptionOf(value) { | 42 Description addDescriptionOf(value) { |
48 if (value is Matcher) { | 43 if (value is Matcher) { |
49 value.describe(this); | 44 value.describe(this); |
50 } else { | 45 } else { |
51 add(prettyPrint(value, maxLineLength: 80, maxItems: 25)); | 46 add(prettyPrint(value, maxLineLength: 80, maxItems: 25)); |
52 } | 47 } |
53 return this; | 48 return this; |
54 } | 49 } |
55 | 50 |
56 /** | 51 /// Append an [Iterable] [list] of objects to the description, using the |
57 * Append an [Iterable] [list] of objects to the description, using the | 52 /// specified [separator] and framing the list with [start] |
58 * specified [separator] and framing the list with [start] | 53 /// and [end]. |
59 * and [end]. | |
60 */ | |
61 Description addAll(String start, String separator, String end, | 54 Description addAll(String start, String separator, String end, |
62 Iterable list) { | 55 Iterable list) { |
63 var separate = false; | 56 var separate = false; |
64 add(start); | 57 add(start); |
65 for (var item in list) { | 58 for (var item in list) { |
66 if (separate) { | 59 if (separate) { |
67 add(separator); | 60 add(separator); |
68 } | 61 } |
69 addDescriptionOf(item); | 62 addDescriptionOf(item); |
70 separate = true; | 63 separate = true; |
71 } | 64 } |
72 add(end); | 65 add(end); |
73 return this; | 66 return this; |
74 } | 67 } |
75 | |
76 /** Escape the control characters in [string] so that they are visible. */ | |
77 _addEscapedString(String string) { | |
78 add("'"); | |
79 add(escapeString(string)); | |
80 add("'"); | |
81 } | |
82 } | 68 } |
OLD | NEW |