Line data Source code
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 : import 'interfaces.dart';
6 : import 'pretty_print.dart';
7 :
8 : /// The default implementation of [Description]. This should rarely need
9 : /// substitution, although conceivably it is a place where other languages
10 : /// could be supported.
11 : class StringDescription implements Description {
12 : final StringBuffer _out = new StringBuffer();
13 :
14 : /// Initialize the description with initial contents [init].
15 1 : StringDescription([String init = '']) {
16 2 : _out.write(init);
17 : }
18 :
19 0 : int get length => _out.length;
20 :
21 : /// Get the description as a string.
22 0 : String toString() => _out.toString();
23 :
24 : /// Append [text] to the description.
25 : Description add(String text) {
26 0 : _out.write(text);
27 : return this;
28 : }
29 :
30 : /// Change the value of the description.
31 : Description replace(String text) {
32 0 : _out.clear();
33 0 : return add(text);
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 : Description addDescriptionOf(value) {
41 0 : if (value is Matcher) {
42 0 : value.describe(this);
43 : } else {
44 0 : add(prettyPrint(value, maxLineLength: 80, maxItems: 25));
45 : }
46 : return this;
47 : }
48 :
49 : /// Append an [Iterable] [list] of objects to the description, using the
50 : /// specified [separator] and framing the list with [start]
51 : /// and [end].
52 : Description addAll(
53 : String start, String separator, String end, Iterable list) {
54 : var separate = false;
55 0 : add(start);
56 0 : for (var item in list) {
57 : if (separate) {
58 0 : add(separator);
59 : }
60 0 : addDescriptionOf(item);
61 : separate = true;
62 : }
63 0 : add(end);
64 : return this;
65 : }
66 : }
|