Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(26)

Side by Side Diff: tests/corelib/string_test.dart

Issue 2983253002: Migrated test block 29 to Dart 2.0 (Closed)
Patch Set: Migrated test block 29 to Dart 2.0 Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 void main() {
8 testOutOfRange();
9 testIllegalArgument();
10 testConcat();
11 testIndex();
12 testCodeUnitAt();
13 testEquals();
14 testEndsWith();
15 testStartsWith();
16 testIndexOf();
17 testLastIndexOf();
18 testContains();
19 testReplaceAll();
20 testCompareTo();
21 testCharCodes();
22 testRepeat();
23 testPadLeft();
24 testPadRight();
25 }
26
27 void testLength() {
28 String str = "";
29 for (var i = 0; i < 20; i++) {
30 testStringLength(i, str);
31 str += " ";
32 }
33 }
34
35 void testOutOfRange() {
36 String a = "Hello";
37 bool exception_caught = false;
38 try {
39 var c = a[20]; // Throw exception.
40 } on RangeError catch (e) {
41 exception_caught = true;
42 }
43 Expect.isTrue(exception_caught);
44 }
45
46 void testIllegalArgument() {
47 String a = "Hello";
48 bool exception_caught = false;
49 try {
50 var c = a[2.2]; // Throw exception.
51 Expect.fail("Accepting double as index");
52 } on ArgumentError catch (e) {
53 exception_caught = true;
54 } on TypeError catch (e) {
55 // Thrown in checked mode only.
56 exception_caught = true;
57 }
58 Expect.isTrue(exception_caught);
59 }
60
61 void testIndex() {
62 String str = "string";
63 for (int i = 0; i < str.length; i++) {
64 Expect.isTrue(str[i] is String);
65 testStringLength(1, str[i]);
66 }
67 }
68
69 void testCodeUnitAt() {
70 String str = "string";
71 for (int i = 0; i < str.length; i++) {
72 Expect.isTrue(str.codeUnitAt(i) is int);
73 }
74 }
75
76 void testConcat() {
77 var a = "One";
78 var b = "Four";
79 var c = a + b;
80 testStringLength(7, c);
81 Expect.equals("OneFour", c);
82 }
83
84 void testEquals() {
85 Expect.equals("str", "str");
86
87 Expect.equals("str", "s" + "t" + "r");
88 Expect.equals("s" + "t" + "r", "str");
89
90 Expect.isFalse("str" == "s");
91 Expect.isFalse("str" == "r");
92 Expect.isFalse("str" == "st");
93 Expect.isFalse("str" == "tr");
94
95 Expect.isFalse("s" == "str");
96 Expect.isFalse("r" == "str");
97 Expect.isFalse("st" == "str");
98 Expect.isFalse("tr" == "str");
99
100 Expect.isFalse("" == "s");
101 Expect.equals("", "");
102 }
103
104 void testEndsWith() {
105 Expect.isTrue("str".endsWith("r"));
106 Expect.isTrue("str".endsWith("tr"));
107 Expect.isTrue("str".endsWith("str"));
108
109 Expect.isFalse("str".endsWith("stri"));
110 Expect.isFalse("str".endsWith("t"));
111 Expect.isFalse("str".endsWith("st"));
112 Expect.isFalse("str".endsWith("s"));
113
114 Expect.isTrue("".endsWith(""));
115 Expect.isFalse("".endsWith("s"));
116 }
117
118 void testStartsWith() {
119 Expect.isTrue("str".startsWith("s"));
120 Expect.isTrue("str".startsWith("st"));
121 Expect.isTrue("str".startsWith("str"));
122
123 Expect.isFalse("str".startsWith("stri"));
124 Expect.isFalse("str".startsWith("r"));
125 Expect.isFalse("str".startsWith("tr"));
126 Expect.isFalse("str".startsWith("t"));
127
128 Expect.isTrue("".startsWith(""));
129 Expect.isFalse("".startsWith("s"));
130
131 Expect.isFalse("strstr".startsWith("s", 1));
132 Expect.isFalse("strstr".startsWith("s", 2));
133 Expect.isTrue("strstr".startsWith("s", 3));
134 Expect.isFalse("strstr".startsWith("s", 4));
135
136 Expect.isFalse("strstr".startsWith("st", 1));
137 Expect.isFalse("strstr".startsWith("st", 2));
138 Expect.isTrue("strstr".startsWith("st", 3));
139 Expect.isFalse("strstr".startsWith("st", 4));
140
141 Expect.isFalse("strstr".startsWith("str", 1));
142 Expect.isFalse("strstr".startsWith("str", 2));
143 Expect.isTrue("strstr".startsWith("str", 3));
144 Expect.isFalse("strstr".startsWith("str", 4));
145
146 Expect.isTrue("str".startsWith("", 0));
147 Expect.isTrue("str".startsWith("", 1));
148 Expect.isTrue("str".startsWith("", 2));
149 Expect.isTrue("str".startsWith("", 3));
150
151 Expect.throws(() => "str".startsWith("", -1));
152 Expect.throws(() => "str".startsWith("", 4));
153
154 var regexp = new RegExp("s(?:tr?)?");
155 Expect.isTrue("s".startsWith(regexp));
156 Expect.isTrue("st".startsWith(regexp));
157 Expect.isTrue("str".startsWith(regexp));
158 Expect.isTrue("sX".startsWith(regexp));
159 Expect.isTrue("stX".startsWith(regexp));
160 Expect.isTrue("strX".startsWith(regexp));
161
162 Expect.isFalse("".startsWith(regexp));
163 Expect.isFalse("astr".startsWith(regexp));
164
165 Expect.isTrue("".startsWith(new RegExp("")));
166 Expect.isTrue("".startsWith(new RegExp("a?")));
167
168 Expect.isFalse("strstr".startsWith(regexp, 1));
169 Expect.isFalse("strstr".startsWith(regexp, 2));
170 Expect.isTrue("strstr".startsWith(regexp, 3));
171 Expect.isFalse("strstr".startsWith(regexp, 4));
172
173 Expect.isTrue("str".startsWith(new RegExp(""), 0));
174 Expect.isTrue("str".startsWith(new RegExp(""), 1));
175 Expect.isTrue("str".startsWith(new RegExp(""), 2));
176 Expect.isTrue("str".startsWith(new RegExp(""), 3));
177 Expect.isTrue("str".startsWith(new RegExp("a?"), 0));
178 Expect.isTrue("str".startsWith(new RegExp("a?"), 1));
179 Expect.isTrue("str".startsWith(new RegExp("a?"), 2));
180 Expect.isTrue("str".startsWith(new RegExp("a?"), 3));
181
182 Expect.throws(() => "str".startsWith(regexp, -1));
183 Expect.throws(() => "str".startsWith(regexp, 4));
184
185 regexp = new RegExp("^str");
186 Expect.isTrue("strstr".startsWith(regexp));
187 Expect.isTrue("strstr".startsWith(regexp, 0));
188 Expect.isFalse("strstr".startsWith(regexp, 1));
189 Expect.isFalse("strstr".startsWith(regexp, 2));
190 Expect.isFalse("strstr".startsWith(regexp, 3)); // Second "str" isn't at ^.
191 }
192
193 void testIndexOf() {
194 Expect.equals(0, "str".indexOf("", 0));
195 Expect.equals(0, "".indexOf("", 0));
196 Expect.equals(-1, "".indexOf("a", 0));
197
198 Expect.equals(1, "str".indexOf("t", 0));
199 Expect.equals(1, "str".indexOf("tr", 0));
200 Expect.equals(0, "str".indexOf("str", 0));
201 Expect.equals(0, "str".indexOf("st", 0));
202 Expect.equals(0, "str".indexOf("s", 0));
203 Expect.equals(2, "str".indexOf("r", 0));
204 Expect.equals(-1, "str".indexOf("string", 0));
205
206 Expect.equals(1, "strstr".indexOf("t", 0));
207 Expect.equals(1, "strstr".indexOf("tr", 0));
208 Expect.equals(0, "strstr".indexOf("str", 0));
209 Expect.equals(0, "strstr".indexOf("st", 0));
210 Expect.equals(0, "strstr".indexOf("s", 0));
211 Expect.equals(2, "strstr".indexOf("r", 0));
212 Expect.equals(-1, "str".indexOf("string", 0));
213
214 Expect.equals(4, "strstr".indexOf("t", 2));
215 Expect.equals(4, "strstr".indexOf("tr", 2));
216 Expect.equals(3, "strstr".indexOf("str", 1));
217 Expect.equals(3, "strstr".indexOf("str", 2));
218 Expect.equals(3, "strstr".indexOf("str", 3));
219 Expect.equals(3, "strstr".indexOf("st", 1));
220 Expect.equals(3, "strstr".indexOf("s", 3));
221 Expect.equals(5, "strstr".indexOf("r", 3));
222 Expect.equals(5, "strstr".indexOf("r", 4));
223 Expect.equals(5, "strstr".indexOf("r", 5));
224
225 String str = "hello";
226 for (int i = 0; i < 10; i++) {
227 if (i > str.length) {
228 Expect.throws(() => str.indexOf("", i));
229 } else {
230 int result = str.indexOf("", i);
231 Expect.equals(i, result);
232 }
233 }
234
235 var re = new RegExp("an?");
236 Expect.equals(1, "banana".indexOf(re));
237 Expect.equals(1, "banana".indexOf(re, 0));
238 Expect.equals(1, "banana".indexOf(re, 1));
239 Expect.equals(3, "banana".indexOf(re, 2));
240 Expect.equals(3, "banana".indexOf(re, 3));
241 Expect.equals(5, "banana".indexOf(re, 4));
242 Expect.equals(5, "banana".indexOf(re, 5));
243 Expect.equals(-1, "banana".indexOf(re, 6));
244 Expect.throws(() => "banana".indexOf(re, -1));
245 Expect.throws(() => "banana".indexOf(re, 7));
246 re = new RegExp("x?");
247 for (int i = 0; i <= str.length; i++) {
248 Expect.equals(i, str.indexOf(re, i));
249 }
250 }
251
252 void testLastIndexOf() {
253 Expect.equals(2, "str".lastIndexOf("", 2));
254 Expect.equals(0, "".lastIndexOf("", 0));
255 Expect.equals(-1, "".lastIndexOf("a", 0));
256
257 Expect.equals(1, "str".lastIndexOf("t", 2));
258 Expect.equals(1, "str".lastIndexOf("tr", 2));
259 Expect.equals(0, "str".lastIndexOf("str", 2));
260 Expect.equals(0, "str".lastIndexOf("st", 2));
261 Expect.equals(0, "str".lastIndexOf("s", 2));
262 Expect.equals(2, "str".lastIndexOf("r", 2));
263 Expect.equals(-1, "str".lastIndexOf("string", 2));
264
265 Expect.equals(4, "strstr".lastIndexOf("t", 5));
266 Expect.equals(4, "strstr".lastIndexOf("tr", 5));
267 Expect.equals(3, "strstr".lastIndexOf("str", 5));
268 Expect.equals(3, "strstr".lastIndexOf("st", 5));
269 Expect.equals(3, "strstr".lastIndexOf("s", 5));
270 Expect.equals(5, "strstr".lastIndexOf("r", 5));
271 Expect.throws(() {
272 "str".lastIndexOf("string", 5);
273 });
274 Expect.equals(4, "strstr".lastIndexOf("t", 5));
275 Expect.equals(4, "strstr".lastIndexOf("tr", 5));
276 Expect.equals(3, "strstr".lastIndexOf("str", 5));
277 Expect.equals(3, "strstr".lastIndexOf("str", 5));
278 Expect.equals(3, "strstr".lastIndexOf("str", 5));
279 Expect.equals(3, "strstr".lastIndexOf("st", 5));
280 Expect.equals(3, "strstr".lastIndexOf("s", 5));
281 Expect.equals(5, "strstr".lastIndexOf("r", 5));
282 Expect.equals(2, "strstr".lastIndexOf("r", 4));
283 Expect.equals(2, "strstr".lastIndexOf("r", 3));
284 Expect.equals(5, "strstr".lastIndexOf("r"));
285 Expect.equals(5, "strstr".lastIndexOf("r", null));
286
287 String str = "hello";
288 for (int i = 0; i < 10; i++) {
289 if (i > str.length) {
290 Expect.throws(() => str.indexOf("", i));
291 } else {
292 int result = str.lastIndexOf("", i);
293 Expect.equals(i, result);
294 }
295 }
296
297 var re = new RegExp("an?");
298 Expect.equals(5, "banana".lastIndexOf(re));
299 Expect.equals(5, "banana".lastIndexOf(re, 6));
300 Expect.equals(5, "banana".lastIndexOf(re, 5));
301 Expect.equals(3, "banana".lastIndexOf(re, 4));
302 Expect.equals(3, "banana".lastIndexOf(re, 3));
303 Expect.equals(1, "banana".lastIndexOf(re, 2));
304 Expect.equals(1, "banana".lastIndexOf(re, 1));
305 Expect.equals(-1, "banana".lastIndexOf(re, 0));
306 Expect.throws(() => "banana".lastIndexOf(re, -1));
307 Expect.throws(() => "banana".lastIndexOf(re, 7));
308 re = new RegExp("x?");
309 for (int i = 0; i <= str.length; i++) {
310 Expect.equals(i, str.indexOf(re, i));
311 }
312 }
313
314 void testContains() {
315 Expect.isTrue("str".contains("s", 0));
316 Expect.isTrue("str".contains("st", 0));
317 Expect.isTrue("str".contains("str", 0));
318 Expect.isTrue("str".contains("t", 0));
319 Expect.isTrue("str".contains("r", 0));
320 Expect.isTrue("str".contains("tr", 0));
321
322 Expect.isFalse("str".contains("sr", 0));
323 Expect.isFalse("str".contains("string", 0));
324
325 Expect.isTrue("str".contains("", 0));
326 Expect.isTrue("".contains("", 0));
327 Expect.isFalse("".contains("s", 0));
328 }
329
330 void testReplaceAll() {
331 Expect.equals("AtoBtoCDtoE", "AfromBfromCDfromE".replaceAll("from", "to"));
332
333 // Test with the replaced string at the beginning.
334 Expect.equals("toABtoCDtoE", "fromABfromCDfromE".replaceAll("from", "to"));
335
336 // Test with the replaced string at the end.
337 Expect.equals(
338 "toABtoCDtoEto", "fromABfromCDfromEfrom".replaceAll("from", "to"));
339
340 // Test when there are no occurence of the string to replace.
341 Expect.equals("ABC", "ABC".replaceAll("from", "to"));
342
343 // Test when the string to change is the empty string.
344 Expect.equals("", "".replaceAll("from", "to"));
345
346 // Test when the string to change is a substring of the string to
347 // replace.
348 Expect.equals("fro", "fro".replaceAll("from", "to"));
349
350 // Test when the string to change is the replaced string.
351 Expect.equals("to", "from".replaceAll("from", "to"));
352
353 // Test when the string to change is the replacement string.
354 Expect.equals("to", "to".replaceAll("from", "to"));
355
356 // Test replacing by the empty string.
357 Expect.equals("", "from".replaceAll("from", ""));
358 Expect.equals("AB", "AfromB".replaceAll("from", ""));
359
360 // Test changing the empty string.
361 Expect.equals("to", "".replaceAll("", "to"));
362
363 // Test replacing the empty string.
364 Expect.equals("toAtoBtoCto", "ABC".replaceAll("", "to"));
365 }
366
367 void testCompareTo() {
368 Expect.equals(0, "".compareTo(""));
369 Expect.equals(0, "str".compareTo("str"));
370 Expect.equals(-1, "str".compareTo("string"));
371 Expect.equals(1, "string".compareTo("str"));
372 Expect.equals(1, "string".compareTo(""));
373 Expect.equals(-1, "".compareTo("string"));
374 }
375
376 void testCharCodes() {
377 test(str) {
378 var list = str.codeUnits;
379 Expect.equals(str.length, list.length);
380 for (int i = 0; i < str.length; i++) {
381 Expect.equals(str.codeUnitAt(i), list[i]);
382 }
383 }
384
385 test("abc");
386 test("");
387 test(" ");
388 }
389
390 void testStringLength(int length, String str) {
391 Expect.equals(length, str.length);
392 (length == 0 ? Expect.isTrue : Expect.isFalse)(str.isEmpty);
393 (length != 0 ? Expect.isTrue : Expect.isFalse)(str.isNotEmpty);
394 }
395
396 void testRepeat() {
397 List<String> testStrings = [
398 "",
399 "\x00",
400 "a",
401 "ab",
402 "\x80",
403 "\xff",
404 "\u2028",
405 "abcdef\u2028",
406 "\u{10002}",
407 "abcdef\u{10002}"
408 ];
409 List<int> counts = [
410 0,
411 1,
412 2,
413 3,
414 4,
415 5,
416 6,
417 7,
418 8,
419 9,
420 10,
421 11,
422 12,
423 13,
424 14,
425 15,
426 16,
427 17,
428 127,
429 128,
430 129
431 ];
432 void testRepeat(str, repeat) {
433 String expect;
434 if (repeat <= 0) {
435 expect = "";
436 } else if (repeat == 1) {
437 expect = str;
438 } else {
439 StringBuffer buf = new StringBuffer();
440 for (int i = 0; i < repeat; i++) {
441 buf.write(str);
442 }
443 expect = buf.toString();
444 }
445 String actual = str * repeat;
446 Expect.equals(expect, actual, "$str#${str.length} * $repeat");
447 }
448
449 for (String str in testStrings) {
450 for (int repeat in counts) {
451 testRepeat(str, repeat);
452 }
453 }
454 }
455
456 void testPadLeft() {
457 Expect.equals(" 1", "1".padLeft(5, ' '));
458 Expect.equals(" 11", "11".padLeft(5, ' '));
459 Expect.equals(" 111", "111".padLeft(5, ' '));
460 Expect.equals(" 1111", "1111".padLeft(5, ' '));
461 Expect.equals("11111", "11111".padLeft(5, ' '));
462 Expect.equals("111111", "111111".padLeft(5, ' '));
463 Expect.equals(" \u{10002}", "\u{10002}".padLeft(5, ' '));
464 Expect.equals('', ''.padLeft(0, 'a'));
465 Expect.equals('a', ''.padLeft(1, 'a'));
466 Expect.equals('aaaaa', ''.padLeft(5, 'a'));
467 Expect.equals('', ''.padLeft(-2, 'a'));
468
469 Expect.equals('xyzxyzxyzxyzxyz', ''.padLeft(5, 'xyz'));
470 Expect.equals('xyzxyzxyzxyza', 'a'.padLeft(5, 'xyz'));
471 Expect.equals('xyzxyzxyzaa', 'aa'.padLeft(5, 'xyz'));
472 Expect.equals('\u{10002}\u{10002}\u{10002}aa', 'aa'.padLeft(5, '\u{10002}'));
473 Expect.equals('a', 'a'.padLeft(10, ''));
474 }
475
476 void testPadRight() {
477 Expect.equals("1 ", "1".padRight(5, ' '));
478 Expect.equals("11 ", "11".padRight(5, ' '));
479 Expect.equals("111 ", "111".padRight(5, ' '));
480 Expect.equals("1111 ", "1111".padRight(5, ' '));
481 Expect.equals("11111", "11111".padRight(5, ' '));
482 Expect.equals("111111", "111111".padRight(5, ' '));
483 Expect.equals("\u{10002} ", "\u{10002}".padRight(5, ' '));
484 Expect.equals('', ''.padRight(0, 'a'));
485 Expect.equals('a', ''.padRight(1, 'a'));
486 Expect.equals('aaaaa', ''.padRight(5, 'a'));
487 Expect.equals('', ''.padRight(-2, 'a'));
488
489 Expect.equals('xyzxyzxyzxyzxyz', ''.padRight(5, 'xyz'));
490 Expect.equals('axyzxyzxyzxyz', 'a'.padRight(5, 'xyz'));
491 Expect.equals('aaxyzxyzxyz', 'aa'.padRight(5, 'xyz'));
492 Expect.equals('aa\u{10002}\u{10002}\u{10002}', 'aa'.padRight(5, '\u{10002}'));
493 Expect.equals('a', 'a'.padRight(10, ''));
494 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698