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(""); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 bf.write("s"); | 49 bf.write("s"); |
50 bf.write("t"); | 50 bf.write("t"); |
51 bf.write("u"); | 51 bf.write("u"); |
52 bf.write("v"); | 52 bf.write("v"); |
53 bf.write("w"); | 53 bf.write("w"); |
54 bf.write("x"); | 54 bf.write("x"); |
55 bf.write("y"); | 55 bf.write("y"); |
56 bf.write("z"); | 56 bf.write("z"); |
57 bf.write("\n"); | 57 bf.write("\n"); |
58 bf.write("thequickbrownfoxjumpsoverthelazydog"); | 58 bf.write("thequickbrownfoxjumpsoverthelazydog"); |
59 Expect.equals("abcdefghijklmnopqrstuvwxyz\n" | 59 Expect.equals( |
60 "thequickbrownfoxjumpsoverthelazydog", | 60 "abcdefghijklmnopqrstuvwxyz\n" |
61 bf.toString()); | 61 "thequickbrownfoxjumpsoverthelazydog", |
| 62 bf.toString()); |
62 | 63 |
63 bf = new StringBuffer(""); | 64 bf = new StringBuffer(""); |
64 for (int i = 0; i < 100000; i++) { | 65 for (int i = 0; i < 100000; i++) { |
65 bf.write(''); | 66 bf.write(''); |
66 bf.write(""); | 67 bf.write(""); |
67 } | 68 } |
68 Expect.equals("", bf.toString()); | 69 Expect.equals("", bf.toString()); |
69 } | 70 } |
70 | 71 |
71 void testLength() { | 72 void testLength() { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 | 116 |
116 void testWriteAll2() { | 117 void testWriteAll2() { |
117 // Passing `null` for separator is an error that is checked when the iterable | 118 // Passing `null` for separator is an error that is checked when the iterable |
118 // is not empty. This is not specified in the documentation but we want | 119 // is not empty. This is not specified in the documentation but we want |
119 // implementations to be consistent. | 120 // implementations to be consistent. |
120 StringBuffer bf1 = new StringBuffer(""); | 121 StringBuffer bf1 = new StringBuffer(""); |
121 bf1.writeAll([], null); | 122 bf1.writeAll([], null); |
122 Expect.equals("", bf1.toString()); | 123 Expect.equals("", bf1.toString()); |
123 | 124 |
124 StringBuffer bf2 = new StringBuffer(""); | 125 StringBuffer bf2 = new StringBuffer(""); |
125 Expect.throws(() { bf2.writeAll([1], null); }); | 126 Expect.throws(() { |
| 127 bf2.writeAll([1], null); |
| 128 }); |
126 } | 129 } |
127 | 130 |
128 void testWriteln() { | 131 void testWriteln() { |
129 StringBuffer bf1 = new StringBuffer(""); | 132 StringBuffer bf1 = new StringBuffer(""); |
130 bf1.writeln("Hello"); | 133 bf1.writeln("Hello"); |
131 Expect.equals("Hello\n", bf1.toString()); | 134 Expect.equals("Hello\n", bf1.toString()); |
132 | 135 |
133 StringBuffer bf2 = new StringBuffer(""); | 136 StringBuffer bf2 = new StringBuffer(""); |
134 bf2.writeln(); | 137 bf2.writeln(); |
135 Expect.equals("\n", bf2.toString()); | 138 Expect.equals("\n", bf2.toString()); |
(...skipping 29 matching lines...) Expand all Loading... |
165 | 168 |
166 bf = new StringBuffer("foo"); | 169 bf = new StringBuffer("foo"); |
167 bf.write("bar"); | 170 bf.write("bar"); |
168 Expect.equals("foobar", bf.toString()); | 171 Expect.equals("foobar", bf.toString()); |
169 } | 172 } |
170 | 173 |
171 void testChaining() { | 174 void testChaining() { |
172 StringBuffer bf = new StringBuffer(""); | 175 StringBuffer bf = new StringBuffer(""); |
173 StringBuffer bf2 = new StringBuffer(""); | 176 StringBuffer bf2 = new StringBuffer(""); |
174 bf2.write("bf2"); | 177 bf2.write("bf2"); |
175 bf..write("foo") | 178 bf..write("foo")..write("bar")..write(bf2)..write(bf2)..write("toto"); |
176 ..write("bar") | |
177 ..write(bf2) | |
178 ..write(bf2) | |
179 ..write("toto"); | |
180 Expect.equals("foobarbf2bf2toto", bf.toString()); | 179 Expect.equals("foobarbf2bf2toto", bf.toString()); |
181 } | 180 } |
182 | 181 |
183 void testWriteCharCode() { | 182 void testWriteCharCode() { |
184 StringBuffer bf1 = new StringBuffer(); | 183 StringBuffer bf1 = new StringBuffer(); |
185 StringBuffer bf2 = new StringBuffer(); | 184 StringBuffer bf2 = new StringBuffer(); |
186 bf1.write("a"); | 185 bf1.write("a"); |
187 bf2.writeCharCode(0x61); // a | 186 bf2.writeCharCode(0x61); // a |
188 bf1.write("b"); | 187 bf1.write("b"); |
189 bf2.writeCharCode(0x62); // b | 188 bf2.writeCharCode(0x62); // b |
190 bf1.write("c"); | 189 bf1.write("c"); |
191 bf2.writeCharCode(0x63); // c | 190 bf2.writeCharCode(0x63); // c |
192 bf1.write(new String.fromCharCode(0xD823)); | 191 bf1.write(new String.fromCharCode(0xD823)); |
193 bf2.writeCharCode(0xD823); | 192 bf2.writeCharCode(0xD823); |
194 bf1.write(new String.fromCharCode(0xDC23)); | 193 bf1.write(new String.fromCharCode(0xDC23)); |
195 bf2.writeCharCode(0xDC23); | 194 bf2.writeCharCode(0xDC23); |
196 bf1.write("\u{1d49e}"); | 195 bf1.write("\u{1d49e}"); |
197 bf2.writeCharCode(0x1d49e); | 196 bf2.writeCharCode(0x1d49e); |
198 bf1.write("\x00"); | 197 bf1.write("\x00"); |
199 bf2.writeCharCode(0); | 198 bf2.writeCharCode(0); |
200 Expect.equals(bf1.toString(), bf2.toString()); | 199 Expect.equals(bf1.toString(), bf2.toString()); |
201 Expect.equals("abc\u{18c23}\u{1d49e}\x00", bf2.toString()); | 200 Expect.equals("abc\u{18c23}\u{1d49e}\x00", bf2.toString()); |
202 | 201 |
203 // Mixing strings and char-codes. | 202 // Mixing strings and char-codes. |
204 bf1.clear(); | 203 bf1.clear(); |
205 bf1.write("abcde"); | 204 bf1.write("abcde"); |
206 bf1.writeCharCode(0x61); | 205 bf1.writeCharCode(0x61); |
207 bf1.writeCharCode(0x62); | 206 bf1.writeCharCode(0x62); |
208 bf1.writeCharCode(0x63); | 207 bf1.writeCharCode(0x63); |
209 bf1.write("d"); | 208 bf1.write("d"); |
210 bf1.writeCharCode(0x65); | 209 bf1.writeCharCode(0x65); |
211 Expect.equals("abcdeabcde", bf1.toString()); | 210 Expect.equals("abcdeabcde", bf1.toString()); |
212 | 211 |
213 // Out-of-range character codes are not allowed. | 212 // Out-of-range character codes are not allowed. |
214 Expect.throws(() { bf2.writeCharCode(-1); }); | 213 Expect.throws(() { |
215 Expect.throws(() { bf2.writeCharCode(0x110000); }); | 214 bf2.writeCharCode(-1); |
| 215 }); |
| 216 Expect.throws(() { |
| 217 bf2.writeCharCode(0x110000); |
| 218 }); |
216 } | 219 } |
217 | 220 |
218 void testBufferLength(int length, StringBuffer bf) { | 221 void testBufferLength(int length, StringBuffer bf) { |
219 Expect.equals(length, bf.length); | 222 Expect.equals(length, bf.length); |
220 (length == 0 ? Expect.isTrue : Expect.isFalse)(bf.isEmpty); | 223 (length == 0 ? Expect.isTrue : Expect.isFalse)(bf.isEmpty); |
221 (length != 0 ? Expect.isTrue : Expect.isFalse)(bf.isNotEmpty); | 224 (length != 0 ? Expect.isTrue : Expect.isFalse)(bf.isNotEmpty); |
222 } | 225 } |
223 | 226 |
224 void main() { | 227 void main() { |
225 testToString(); | 228 testToString(); |
226 testConstructor(); | 229 testConstructor(); |
227 testLength(); | 230 testLength(); |
228 testIsEmpty(); | 231 testIsEmpty(); |
229 testWrite(); | 232 testWrite(); |
230 testWriteCharCode(); | 233 testWriteCharCode(); |
231 testWriteAll(); | 234 testWriteAll(); |
232 testWriteAll2(); | 235 testWriteAll2(); |
233 testWriteln(); | 236 testWriteln(); |
234 testClear(); | 237 testClear(); |
235 testChaining(); | 238 testChaining(); |
236 } | 239 } |
OLD | NEW |