OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 import "package:expect/expect.dart"; | |
6 | |
7 // TODO(srdjan): Move StringBuffer to visible names. | |
8 | |
9 void testConstructor() { | |
10 StringBuffer bf = new StringBuffer(""); | |
11 testBufferLength(0, bf); | |
12 | |
13 bf = new StringBuffer("abc"); | |
14 testBufferLength(3, bf); | |
15 Expect.equals("abc", bf.toString()); | |
16 | |
17 bf = new StringBuffer("\x00"); | |
18 } | |
19 | |
20 void testWrite() { | |
21 StringBuffer bf = new StringBuffer(""); | |
22 Expect.equals(true, bf.isEmpty); | |
23 | |
24 bf.write("a"); | |
25 testBufferLength(1, bf); | |
26 Expect.equals("a", bf.toString()); | |
27 | |
28 bf = new StringBuffer(""); | |
29 bf.write("a"); | |
30 bf.write("b"); | |
31 Expect.equals("ab", bf.toString()); | |
32 | |
33 bf = new StringBuffer("abc"); | |
34 bf.write("d"); | |
35 bf.write("e"); | |
36 bf.write("f"); | |
37 bf.write("g"); | |
38 bf.write("h"); | |
39 bf.write("i"); | |
40 bf.write("j"); | |
41 bf.write("k"); | |
42 bf.write("l"); | |
43 bf.write("m"); | |
44 bf.write("n"); | |
45 bf.write("o"); | |
46 bf.write("p"); | |
47 bf.write("q"); | |
48 bf.write("r"); | |
49 bf.write("s"); | |
50 bf.write("t"); | |
51 bf.write("u"); | |
52 bf.write("v"); | |
53 bf.write("w"); | |
54 bf.write("x"); | |
55 bf.write("y"); | |
56 bf.write("z"); | |
57 bf.write("\n"); | |
58 bf.write("thequickbrownfoxjumpsoverthelazydog"); | |
59 Expect.equals( | |
60 "abcdefghijklmnopqrstuvwxyz\n" | |
61 "thequickbrownfoxjumpsoverthelazydog", | |
62 bf.toString()); | |
63 | |
64 bf = new StringBuffer(""); | |
65 for (int i = 0; i < 100000; i++) { | |
66 bf.write(''); | |
67 bf.write(""); | |
68 } | |
69 Expect.equals("", bf.toString()); | |
70 } | |
71 | |
72 void testLength() { | |
73 StringBuffer bf = new StringBuffer(""); | |
74 testBufferLength(0, bf); | |
75 bf.write("foo"); | |
76 testBufferLength(3, bf); | |
77 bf.write("bar"); | |
78 testBufferLength(6, bf); | |
79 bf.write(""); | |
80 testBufferLength(6, bf); | |
81 } | |
82 | |
83 void testIsEmpty() { | |
84 StringBuffer bf = new StringBuffer(""); | |
85 Expect.equals(true, bf.isEmpty); | |
86 bf.write("foo"); | |
87 Expect.equals(false, bf.isEmpty); | |
88 } | |
89 | |
90 void testWriteAll() { | |
91 StringBuffer bf = new StringBuffer(""); | |
92 bf.writeAll(["foo", "bar", "a", "b", "c"]); | |
93 Expect.equals("foobarabc", bf.toString()); | |
94 | |
95 bf.writeAll([]); | |
96 Expect.equals("foobarabc", bf.toString()); | |
97 | |
98 bf.writeAll(["", "", ""]); | |
99 Expect.equals("foobarabc", bf.toString()); | |
100 | |
101 bf.writeAll(["", "", ""], ""); | |
102 Expect.equals("foobarabc", bf.toString()); | |
103 | |
104 StringBuffer bf2 = new StringBuffer(""); | |
105 bf2.writeAll([], "s"); | |
106 Expect.equals("", bf2.toString()); | |
107 | |
108 StringBuffer bf3 = new StringBuffer(""); | |
109 bf3.writeAll(["a"], "s"); | |
110 Expect.equals("a", bf3.toString()); | |
111 | |
112 StringBuffer bf4 = new StringBuffer(""); | |
113 bf4.writeAll(["a", "b"], "s"); | |
114 Expect.equals("asb", bf4.toString()); | |
115 } | |
116 | |
117 void testWriteAll2() { | |
118 // Passing `null` for separator is an error that is checked when the iterable | |
119 // is not empty. This is not specified in the documentation but we want | |
120 // implementations to be consistent. | |
121 StringBuffer bf1 = new StringBuffer(""); | |
122 bf1.writeAll([], null); | |
123 Expect.equals("", bf1.toString()); | |
124 | |
125 StringBuffer bf2 = new StringBuffer(""); | |
126 Expect.throws(() { | |
127 bf2.writeAll([1], null); | |
128 }); | |
129 } | |
130 | |
131 void testWriteln() { | |
132 StringBuffer bf1 = new StringBuffer(""); | |
133 bf1.writeln("Hello"); | |
134 Expect.equals("Hello\n", bf1.toString()); | |
135 | |
136 StringBuffer bf2 = new StringBuffer(""); | |
137 bf2.writeln(); | |
138 Expect.equals("\n", bf2.toString()); | |
139 | |
140 StringBuffer bf3 = new StringBuffer(""); | |
141 bf3.writeln("\n"); | |
142 bf3.writeln(null); | |
143 bf3.writeln(1); | |
144 Expect.equals("\n\nnull\n1\n", bf3.toString()); | |
145 } | |
146 | |
147 void testClear() { | |
148 StringBuffer bf = new StringBuffer(""); | |
149 bf.write("foo"); | |
150 bf.clear(); | |
151 Expect.equals("", bf.toString()); | |
152 testBufferLength(0, bf); | |
153 | |
154 bf.write("bar"); | |
155 Expect.equals("bar", bf.toString()); | |
156 testBufferLength(3, bf); | |
157 bf.clear(); | |
158 Expect.equals("", bf.toString()); | |
159 testBufferLength(0, bf); | |
160 } | |
161 | |
162 void testToString() { | |
163 StringBuffer bf = new StringBuffer(""); | |
164 Expect.equals("", bf.toString()); | |
165 | |
166 bf = new StringBuffer("foo"); | |
167 Expect.equals("foo", bf.toString()); | |
168 | |
169 bf = new StringBuffer("foo"); | |
170 bf.write("bar"); | |
171 Expect.equals("foobar", bf.toString()); | |
172 } | |
173 | |
174 void testChaining() { | |
175 StringBuffer bf = new StringBuffer(""); | |
176 StringBuffer bf2 = new StringBuffer(""); | |
177 bf2.write("bf2"); | |
178 bf..write("foo")..write("bar")..write(bf2)..write(bf2)..write("toto"); | |
179 Expect.equals("foobarbf2bf2toto", bf.toString()); | |
180 } | |
181 | |
182 void testWriteCharCode() { | |
183 StringBuffer bf1 = new StringBuffer(); | |
184 StringBuffer bf2 = new StringBuffer(); | |
185 bf1.write("a"); | |
186 bf2.writeCharCode(0x61); // a | |
187 bf1.write("b"); | |
188 bf2.writeCharCode(0x62); // b | |
189 bf1.write("c"); | |
190 bf2.writeCharCode(0x63); // c | |
191 bf1.write(new String.fromCharCode(0xD823)); | |
192 bf2.writeCharCode(0xD823); | |
193 bf1.write(new String.fromCharCode(0xDC23)); | |
194 bf2.writeCharCode(0xDC23); | |
195 bf1.write("\u{1d49e}"); | |
196 bf2.writeCharCode(0x1d49e); | |
197 bf1.write("\x00"); | |
198 bf2.writeCharCode(0); | |
199 Expect.equals(bf1.toString(), bf2.toString()); | |
200 Expect.equals("abc\u{18c23}\u{1d49e}\x00", bf2.toString()); | |
201 | |
202 // Mixing strings and char-codes. | |
203 bf1.clear(); | |
204 bf1.write("abcde"); | |
205 bf1.writeCharCode(0x61); | |
206 bf1.writeCharCode(0x62); | |
207 bf1.writeCharCode(0x63); | |
208 bf1.write("d"); | |
209 bf1.writeCharCode(0x65); | |
210 Expect.equals("abcdeabcde", bf1.toString()); | |
211 | |
212 // Out-of-range character codes are not allowed. | |
213 Expect.throws(() { | |
214 bf2.writeCharCode(-1); | |
215 }); | |
216 Expect.throws(() { | |
217 bf2.writeCharCode(0x110000); | |
218 }); | |
219 } | |
220 | |
221 void testBufferLength(int length, StringBuffer bf) { | |
222 Expect.equals(length, bf.length); | |
223 (length == 0 ? Expect.isTrue : Expect.isFalse)(bf.isEmpty); | |
224 (length != 0 ? Expect.isTrue : Expect.isFalse)(bf.isNotEmpty); | |
225 } | |
226 | |
227 void main() { | |
228 testToString(); | |
229 testConstructor(); | |
230 testLength(); | |
231 testIsEmpty(); | |
232 testWrite(); | |
233 testWriteCharCode(); | |
234 testWriteAll(); | |
235 testWriteAll2(); | |
236 testWriteln(); | |
237 testClear(); | |
238 testChaining(); | |
239 } | |
OLD | NEW |