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 void main() { | 7 void main() { |
8 testOutOfRange(); | 8 testOutOfRange(); |
9 testIllegalArgument(); | 9 testIllegalArgument(); |
10 testConcat(); | 10 testConcat(); |
(...skipping 18 matching lines...) Expand all Loading... |
29 for (var i = 0; i < 20; i++) { | 29 for (var i = 0; i < 20; i++) { |
30 testStringLength(i, str); | 30 testStringLength(i, str); |
31 str += " "; | 31 str += " "; |
32 } | 32 } |
33 } | 33 } |
34 | 34 |
35 void testOutOfRange() { | 35 void testOutOfRange() { |
36 String a = "Hello"; | 36 String a = "Hello"; |
37 bool exception_caught = false; | 37 bool exception_caught = false; |
38 try { | 38 try { |
39 var c = a[20]; // Throw exception. | 39 var c = a[20]; // Throw exception. |
40 } on RangeError catch (e) { | 40 } on RangeError catch (e) { |
41 exception_caught = true; | 41 exception_caught = true; |
42 } | 42 } |
43 Expect.isTrue(exception_caught); | 43 Expect.isTrue(exception_caught); |
44 } | 44 } |
45 | 45 |
46 void testIllegalArgument() { | 46 void testIllegalArgument() { |
47 String a = "Hello"; | 47 String a = "Hello"; |
48 bool exception_caught = false; | 48 bool exception_caught = false; |
49 try { | 49 try { |
50 var c = a[2.2]; // Throw exception. | 50 var c = a[2.2]; // Throw exception. |
51 Expect.fail("Accepting double as index"); | 51 Expect.fail("Accepting double as index"); |
52 } on ArgumentError catch (e) { | 52 } on ArgumentError catch (e) { |
53 exception_caught = true; | 53 exception_caught = true; |
54 } on TypeError catch (e) { // Thrown in checked mode only. | 54 } on TypeError catch (e) { |
| 55 // Thrown in checked mode only. |
55 exception_caught = true; | 56 exception_caught = true; |
56 } | 57 } |
57 Expect.isTrue(exception_caught); | 58 Expect.isTrue(exception_caught); |
58 } | 59 } |
59 | 60 |
60 void testIndex() { | 61 void testIndex() { |
61 String str = "string"; | 62 String str = "string"; |
62 for (int i = 0; i < str.length; i++) { | 63 for (int i = 0; i < str.length; i++) { |
63 Expect.isTrue(str[i] is String); | 64 Expect.isTrue(str[i] is String); |
64 testStringLength(1, str[i]); | 65 testStringLength(1, str[i]); |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 Expect.isTrue("str".startsWith(new RegExp("a?"), 3)); | 180 Expect.isTrue("str".startsWith(new RegExp("a?"), 3)); |
180 | 181 |
181 Expect.throws(() => "str".startsWith(regexp, -1)); | 182 Expect.throws(() => "str".startsWith(regexp, -1)); |
182 Expect.throws(() => "str".startsWith(regexp, 4)); | 183 Expect.throws(() => "str".startsWith(regexp, 4)); |
183 | 184 |
184 regexp = new RegExp("^str"); | 185 regexp = new RegExp("^str"); |
185 Expect.isTrue("strstr".startsWith(regexp)); | 186 Expect.isTrue("strstr".startsWith(regexp)); |
186 Expect.isTrue("strstr".startsWith(regexp, 0)); | 187 Expect.isTrue("strstr".startsWith(regexp, 0)); |
187 Expect.isFalse("strstr".startsWith(regexp, 1)); | 188 Expect.isFalse("strstr".startsWith(regexp, 1)); |
188 Expect.isFalse("strstr".startsWith(regexp, 2)); | 189 Expect.isFalse("strstr".startsWith(regexp, 2)); |
189 Expect.isFalse("strstr".startsWith(regexp, 3)); // Second "str" isn't at ^. | 190 Expect.isFalse("strstr".startsWith(regexp, 3)); // Second "str" isn't at ^. |
190 } | 191 } |
191 | 192 |
192 void testIndexOf() { | 193 void testIndexOf() { |
193 Expect.equals(0, "str".indexOf("", 0)); | 194 Expect.equals(0, "str".indexOf("", 0)); |
194 Expect.equals(0, "".indexOf("", 0)); | 195 Expect.equals(0, "".indexOf("", 0)); |
195 Expect.equals(-1, "".indexOf("a", 0)); | 196 Expect.equals(-1, "".indexOf("a", 0)); |
196 | 197 |
197 Expect.equals(1, "str".indexOf("t", 0)); | 198 Expect.equals(1, "str".indexOf("t", 0)); |
198 Expect.equals(1, "str".indexOf("tr", 0)); | 199 Expect.equals(1, "str".indexOf("tr", 0)); |
199 Expect.equals(0, "str".indexOf("str", 0)); | 200 Expect.equals(0, "str".indexOf("str", 0)); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 Expect.equals(2, "str".lastIndexOf("r", 2)); | 262 Expect.equals(2, "str".lastIndexOf("r", 2)); |
262 Expect.equals(-1, "str".lastIndexOf("string", 2)); | 263 Expect.equals(-1, "str".lastIndexOf("string", 2)); |
263 | 264 |
264 Expect.equals(4, "strstr".lastIndexOf("t", 5)); | 265 Expect.equals(4, "strstr".lastIndexOf("t", 5)); |
265 Expect.equals(4, "strstr".lastIndexOf("tr", 5)); | 266 Expect.equals(4, "strstr".lastIndexOf("tr", 5)); |
266 Expect.equals(3, "strstr".lastIndexOf("str", 5)); | 267 Expect.equals(3, "strstr".lastIndexOf("str", 5)); |
267 Expect.equals(3, "strstr".lastIndexOf("st", 5)); | 268 Expect.equals(3, "strstr".lastIndexOf("st", 5)); |
268 Expect.equals(3, "strstr".lastIndexOf("s", 5)); | 269 Expect.equals(3, "strstr".lastIndexOf("s", 5)); |
269 Expect.equals(5, "strstr".lastIndexOf("r", 5)); | 270 Expect.equals(5, "strstr".lastIndexOf("r", 5)); |
270 Expect.throws(() { | 271 Expect.throws(() { |
271 "str".lastIndexOf("string", 5); | 272 "str".lastIndexOf("string", 5); |
272 }); | 273 }); |
273 Expect.equals(4, "strstr".lastIndexOf("t", 5)); | 274 Expect.equals(4, "strstr".lastIndexOf("t", 5)); |
274 Expect.equals(4, "strstr".lastIndexOf("tr", 5)); | 275 Expect.equals(4, "strstr".lastIndexOf("tr", 5)); |
275 Expect.equals(3, "strstr".lastIndexOf("str", 5)); | 276 Expect.equals(3, "strstr".lastIndexOf("str", 5)); |
276 Expect.equals(3, "strstr".lastIndexOf("str", 5)); | 277 Expect.equals(3, "strstr".lastIndexOf("str", 5)); |
277 Expect.equals(3, "strstr".lastIndexOf("str", 5)); | 278 Expect.equals(3, "strstr".lastIndexOf("str", 5)); |
278 Expect.equals(3, "strstr".lastIndexOf("st", 5)); | 279 Expect.equals(3, "strstr".lastIndexOf("st", 5)); |
279 Expect.equals(3, "strstr".lastIndexOf("s", 5)); | 280 Expect.equals(3, "strstr".lastIndexOf("s", 5)); |
280 Expect.equals(5, "strstr".lastIndexOf("r", 5)); | 281 Expect.equals(5, "strstr".lastIndexOf("r", 5)); |
281 Expect.equals(2, "strstr".lastIndexOf("r", 4)); | 282 Expect.equals(2, "strstr".lastIndexOf("r", 4)); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 | 321 |
321 Expect.isFalse("str".contains("sr", 0)); | 322 Expect.isFalse("str".contains("sr", 0)); |
322 Expect.isFalse("str".contains("string", 0)); | 323 Expect.isFalse("str".contains("string", 0)); |
323 | 324 |
324 Expect.isTrue("str".contains("", 0)); | 325 Expect.isTrue("str".contains("", 0)); |
325 Expect.isTrue("".contains("", 0)); | 326 Expect.isTrue("".contains("", 0)); |
326 Expect.isFalse("".contains("s", 0)); | 327 Expect.isFalse("".contains("s", 0)); |
327 } | 328 } |
328 | 329 |
329 void testReplaceAll() { | 330 void testReplaceAll() { |
330 Expect.equals( | 331 Expect.equals("AtoBtoCDtoE", "AfromBfromCDfromE".replaceAll("from", "to")); |
331 "AtoBtoCDtoE", "AfromBfromCDfromE".replaceAll("from", "to")); | |
332 | 332 |
333 // Test with the replaced string at the begining. | 333 // Test with the replaced string at the begining. |
334 Expect.equals( | 334 Expect.equals("toABtoCDtoE", "fromABfromCDfromE".replaceAll("from", "to")); |
335 "toABtoCDtoE", "fromABfromCDfromE".replaceAll("from", "to")); | |
336 | 335 |
337 // Test with the replaced string at the end. | 336 // Test with the replaced string at the end. |
338 Expect.equals( | 337 Expect.equals( |
339 "toABtoCDtoEto", "fromABfromCDfromEfrom".replaceAll("from", "to")); | 338 "toABtoCDtoEto", "fromABfromCDfromEfrom".replaceAll("from", "to")); |
340 | 339 |
341 // Test when there are no occurence of the string to replace. | 340 // Test when there are no occurence of the string to replace. |
342 Expect.equals("ABC", "ABC".replaceAll("from", "to")); | 341 Expect.equals("ABC", "ABC".replaceAll("from", "to")); |
343 | 342 |
344 // Test when the string to change is the empty string. | 343 // Test when the string to change is the empty string. |
345 Expect.equals("", "".replaceAll("from", "to")); | 344 Expect.equals("", "".replaceAll("from", "to")); |
(...skipping 29 matching lines...) Expand all Loading... |
375 } | 374 } |
376 | 375 |
377 void testCharCodes() { | 376 void testCharCodes() { |
378 test(str) { | 377 test(str) { |
379 var list = str.codeUnits; | 378 var list = str.codeUnits; |
380 Expect.equals(str.length, list.length); | 379 Expect.equals(str.length, list.length); |
381 for (int i = 0; i < str.length; i++) { | 380 for (int i = 0; i < str.length; i++) { |
382 Expect.equals(str.codeUnitAt(i), list[i]); | 381 Expect.equals(str.codeUnitAt(i), list[i]); |
383 } | 382 } |
384 } | 383 } |
| 384 |
385 test("abc"); | 385 test("abc"); |
386 test(""); | 386 test(""); |
387 test(" "); | 387 test(" "); |
388 } | 388 } |
389 | 389 |
390 void testStringLength(int length, String str) { | 390 void testStringLength(int length, String str) { |
391 Expect.equals(length, str.length); | 391 Expect.equals(length, str.length); |
392 (length == 0 ? Expect.isTrue : Expect.isFalse)(str.isEmpty); | 392 (length == 0 ? Expect.isTrue : Expect.isFalse)(str.isEmpty); |
393 (length != 0 ? Expect.isTrue : Expect.isFalse)(str.isNotEmpty); | 393 (length != 0 ? Expect.isTrue : Expect.isFalse)(str.isNotEmpty); |
394 } | 394 } |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
436 } else if (repeat == 1) { | 436 } else if (repeat == 1) { |
437 expect = str; | 437 expect = str; |
438 } else { | 438 } else { |
439 StringBuffer buf = new StringBuffer(); | 439 StringBuffer buf = new StringBuffer(); |
440 for (int i = 0; i < repeat; i++) { | 440 for (int i = 0; i < repeat; i++) { |
441 buf.write(str); | 441 buf.write(str); |
442 } | 442 } |
443 expect = buf.toString(); | 443 expect = buf.toString(); |
444 } | 444 } |
445 String actual = str * repeat; | 445 String actual = str * repeat; |
446 Expect.equals(expect, actual, | 446 Expect.equals(expect, actual, "$str#${str.length} * $repeat"); |
447 "$str#${str.length} * $repeat"); | |
448 } | 447 } |
| 448 |
449 for (String str in testStrings) { | 449 for (String str in testStrings) { |
450 for (int repeat in counts) { | 450 for (int repeat in counts) { |
451 testRepeat(str, repeat); | 451 testRepeat(str, repeat); |
452 } | 452 } |
453 } | 453 } |
454 } | 454 } |
455 | 455 |
456 void testPadLeft() { | 456 void testPadLeft() { |
457 Expect.equals(" 1", "1".padLeft(5, ' ')); | 457 Expect.equals(" 1", "1".padLeft(5, ' ')); |
458 Expect.equals(" 11", "11".padLeft(5, ' ')); | 458 Expect.equals(" 11", "11".padLeft(5, ' ')); |
(...skipping 26 matching lines...) Expand all Loading... |
485 Expect.equals('a', ''.padRight(1, 'a')); | 485 Expect.equals('a', ''.padRight(1, 'a')); |
486 Expect.equals('aaaaa', ''.padRight(5, 'a')); | 486 Expect.equals('aaaaa', ''.padRight(5, 'a')); |
487 Expect.equals('', ''.padRight(-2, 'a')); | 487 Expect.equals('', ''.padRight(-2, 'a')); |
488 | 488 |
489 Expect.equals('xyzxyzxyzxyzxyz', ''.padRight(5, 'xyz')); | 489 Expect.equals('xyzxyzxyzxyzxyz', ''.padRight(5, 'xyz')); |
490 Expect.equals('axyzxyzxyzxyz', 'a'.padRight(5, 'xyz')); | 490 Expect.equals('axyzxyzxyzxyz', 'a'.padRight(5, 'xyz')); |
491 Expect.equals('aaxyzxyzxyz', 'aa'.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}')); | 492 Expect.equals('aa\u{10002}\u{10002}\u{10002}', 'aa'.padRight(5, '\u{10002}')); |
493 Expect.equals('a', 'a'.padRight(10, '')); | 493 Expect.equals('a', 'a'.padRight(10, '')); |
494 } | 494 } |
OLD | NEW |