OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 import "package:expect/expect.dart"; | 5 import "package:expect/expect.dart"; |
6 | 6 |
7 // TODO(srdjan): Move StringBuffer to visible names. | 7 // TODO(srdjan): Move StringBuffer to visible names. |
8 | 8 |
9 void testConstructor() { | 9 void testConstructor() { |
10 StringBuffer bf = new StringBuffer(""); | 10 StringBuffer bf = new StringBuffer(""); |
11 Expect.equals(true, bf.isEmpty); | 11 testBufferLength(0, bf); |
12 | 12 |
13 bf = new StringBuffer("abc"); | 13 bf = new StringBuffer("abc"); |
14 Expect.equals(3, bf.length); | 14 testBufferLength(3, bf); |
15 Expect.equals("abc", bf.toString()); | 15 Expect.equals("abc", bf.toString()); |
16 | 16 |
17 bf = new StringBuffer("\x00"); | 17 bf = new StringBuffer("\x00"); |
18 } | 18 } |
19 | 19 |
20 void testWrite() { | 20 void testWrite() { |
21 StringBuffer bf = new StringBuffer(""); | 21 StringBuffer bf = new StringBuffer(""); |
22 Expect.equals(true, bf.isEmpty); | 22 Expect.equals(true, bf.isEmpty); |
23 | 23 |
24 bf.write("a"); | 24 bf.write("a"); |
25 Expect.equals(1, bf.length); | 25 testBufferLength(1, bf); |
26 Expect.equals("a", bf.toString()); | 26 Expect.equals("a", bf.toString()); |
27 | 27 |
28 bf = new StringBuffer(""); | 28 bf = new StringBuffer(""); |
29 bf.write("a"); | 29 bf.write("a"); |
30 bf.write("b"); | 30 bf.write("b"); |
31 Expect.equals("ab", bf.toString()); | 31 Expect.equals("ab", bf.toString()); |
32 | 32 |
33 bf = new StringBuffer("abc"); | 33 bf = new StringBuffer("abc"); |
34 bf.write("d"); | 34 bf.write("d"); |
35 bf.write("e"); | 35 bf.write("e"); |
(...skipping 27 matching lines...) Expand all Loading... |
63 bf = new StringBuffer(""); | 63 bf = new StringBuffer(""); |
64 for (int i = 0; i < 100000; i++) { | 64 for (int i = 0; i < 100000; i++) { |
65 bf.write(''); | 65 bf.write(''); |
66 bf.write(""); | 66 bf.write(""); |
67 } | 67 } |
68 Expect.equals("", bf.toString()); | 68 Expect.equals("", bf.toString()); |
69 } | 69 } |
70 | 70 |
71 void testLength() { | 71 void testLength() { |
72 StringBuffer bf = new StringBuffer(""); | 72 StringBuffer bf = new StringBuffer(""); |
73 Expect.equals(0, bf.length); | 73 testBufferLength(0, bf); |
74 bf.write("foo"); | 74 bf.write("foo"); |
75 Expect.equals(3, bf.length); | 75 testBufferLength(3, bf); |
76 bf.write("bar"); | 76 bf.write("bar"); |
77 Expect.equals(6, bf.length); | 77 testBufferLength(6, bf); |
78 bf.write(""); | 78 bf.write(""); |
79 Expect.equals(6, bf.length); | 79 testBufferLength(6, bf); |
80 } | 80 } |
81 | 81 |
82 void testIsEmpty() { | 82 void testIsEmpty() { |
83 StringBuffer bf = new StringBuffer(""); | 83 StringBuffer bf = new StringBuffer(""); |
84 Expect.equals(true, bf.isEmpty); | 84 Expect.equals(true, bf.isEmpty); |
85 bf.write("foo"); | 85 bf.write("foo"); |
86 Expect.equals(false, bf.isEmpty); | 86 Expect.equals(false, bf.isEmpty); |
87 } | 87 } |
88 | 88 |
89 void testWriteAll() { | 89 void testWriteAll() { |
90 StringBuffer bf = new StringBuffer(""); | 90 StringBuffer bf = new StringBuffer(""); |
91 bf.writeAll(["foo", "bar", "a", "b", "c"]); | 91 bf.writeAll(["foo", "bar", "a", "b", "c"]); |
92 Expect.equals("foobarabc", bf.toString()); | 92 Expect.equals("foobarabc", bf.toString()); |
93 | 93 |
94 bf.writeAll([]); | 94 bf.writeAll([]); |
95 Expect.equals("foobarabc", bf.toString()); | 95 Expect.equals("foobarabc", bf.toString()); |
96 | 96 |
97 bf.writeAll(["", "", ""]); | 97 bf.writeAll(["", "", ""]); |
98 Expect.equals("foobarabc", bf.toString()); | 98 Expect.equals("foobarabc", bf.toString()); |
99 } | 99 } |
100 | 100 |
101 void testClear() { | 101 void testClear() { |
102 StringBuffer bf = new StringBuffer(""); | 102 StringBuffer bf = new StringBuffer(""); |
103 bf.write("foo"); | 103 bf.write("foo"); |
104 bf.clear(); | 104 bf.clear(); |
105 Expect.equals("", bf.toString()); | 105 Expect.equals("", bf.toString()); |
106 Expect.equals(0, bf.length); | 106 testBufferLength(0, bf); |
107 | 107 |
108 bf.write("bar"); | 108 bf.write("bar"); |
109 Expect.equals("bar", bf.toString()); | 109 Expect.equals("bar", bf.toString()); |
110 Expect.equals(3, bf.length); | 110 testBufferLength(3, bf); |
111 bf.clear(); | 111 bf.clear(); |
112 Expect.equals("", bf.toString()); | 112 Expect.equals("", bf.toString()); |
113 Expect.equals(0, bf.length); | 113 testBufferLength(0, bf); |
114 } | 114 } |
115 | 115 |
116 void testToString() { | 116 void testToString() { |
117 StringBuffer bf = new StringBuffer(""); | 117 StringBuffer bf = new StringBuffer(""); |
118 Expect.equals("", bf.toString()); | 118 Expect.equals("", bf.toString()); |
119 | 119 |
120 bf = new StringBuffer("foo"); | 120 bf = new StringBuffer("foo"); |
121 Expect.equals("foo", bf.toString()); | 121 Expect.equals("foo", bf.toString()); |
122 | 122 |
123 bf = new StringBuffer("foo"); | 123 bf = new StringBuffer("foo"); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 bf1.writeCharCode(0x63); | 165 bf1.writeCharCode(0x63); |
166 bf1.write("d"); | 166 bf1.write("d"); |
167 bf1.writeCharCode(0x65); | 167 bf1.writeCharCode(0x65); |
168 Expect.equals("abcdeabcde", bf1.toString()); | 168 Expect.equals("abcdeabcde", bf1.toString()); |
169 | 169 |
170 // Out-of-range character codes are not allowed. | 170 // Out-of-range character codes are not allowed. |
171 Expect.throws(() { bf2.writeCharCode(-1); }); | 171 Expect.throws(() { bf2.writeCharCode(-1); }); |
172 Expect.throws(() { bf2.writeCharCode(0x110000); }); | 172 Expect.throws(() { bf2.writeCharCode(0x110000); }); |
173 } | 173 } |
174 | 174 |
| 175 void testBufferLength(int length, StringBuffer bf) { |
| 176 Expect.equals(length, bf.length); |
| 177 (length == 0 ? Expect.isTrue : Expect.isFalse)(bf.isEmpty); |
| 178 (length != 0 ? Expect.isTrue : Expect.isFalse)(bf.isNotEmpty); |
| 179 } |
| 180 |
175 void main() { | 181 void main() { |
176 testToString(); | 182 testToString(); |
177 testConstructor(); | 183 testConstructor(); |
178 testLength(); | 184 testLength(); |
179 testIsEmpty(); | 185 testIsEmpty(); |
180 testWrite(); | 186 testWrite(); |
181 testWriteCharCode(); | 187 testWriteCharCode(); |
182 testWriteAll(); | 188 testWriteAll(); |
183 testClear(); | 189 testClear(); |
184 testChaining(); | 190 testChaining(); |
185 } | 191 } |
OLD | NEW |