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(ngeoffray): test String methods with null arguments. | 7 // TODO(ngeoffray): test String methods with null arguments. |
8 class StringTest { | 8 class StringTest { |
9 | 9 |
10 static testMain() { | 10 static testMain() { |
11 testOutOfRange(); | 11 testOutOfRange(); |
12 testIllegalArgument(); | 12 testIllegalArgument(); |
13 testConcat(); | 13 testConcat(); |
14 testIndex(); | 14 testIndex(); |
15 testCodeUnitAt(); | 15 testCodeUnitAt(); |
16 testEquals(); | 16 testEquals(); |
17 testEndsWith(); | 17 testEndsWith(); |
18 testStartsWith(); | 18 testStartsWith(); |
19 testIndexOf(); | 19 testIndexOf(); |
20 testLastIndexOf(); | 20 testLastIndexOf(); |
21 testContains(); | 21 testContains(); |
22 testReplaceAll(); | 22 testReplaceAll(); |
23 testCompareTo(); | 23 testCompareTo(); |
24 testCharCodes(); | 24 testCharCodes(); |
25 } | 25 } |
26 | 26 |
| 27 static void testLength() { |
| 28 String str = ""; |
| 29 for (var i = 0; i < 20; i++) { |
| 30 testStringLength(i, str); |
| 31 str += " "; |
| 32 } |
| 33 } |
| 34 |
27 static void testOutOfRange() { | 35 static void testOutOfRange() { |
28 String a = "Hello"; | 36 String a = "Hello"; |
29 bool exception_caught = false; | 37 bool exception_caught = false; |
30 try { | 38 try { |
31 var c = a[20]; // Throw exception. | 39 var c = a[20]; // Throw exception. |
32 } on RangeError catch (e) { | 40 } on RangeError catch (e) { |
33 exception_caught = true; | 41 exception_caught = true; |
34 } | 42 } |
35 Expect.equals(true, exception_caught); | 43 Expect.equals(true, exception_caught); |
36 } | 44 } |
37 | 45 |
38 static testIllegalArgument() { | 46 static testIllegalArgument() { |
39 String a = "Hello"; | 47 String a = "Hello"; |
40 bool exception_caught = false; | 48 bool exception_caught = false; |
41 try { | 49 try { |
42 var c = a[2.2]; // Throw exception. | 50 var c = a[2.2]; // Throw exception. |
43 Expect.equals(true, false); | 51 Expect.equals(true, false); |
44 } on ArgumentError catch (e) { | 52 } on ArgumentError catch (e) { |
45 exception_caught = true; | 53 exception_caught = true; |
46 } on TypeError catch (e) { // Thrown in checked mode only. | 54 } on TypeError catch (e) { // Thrown in checked mode only. |
47 exception_caught = true; | 55 exception_caught = true; |
48 } | 56 } |
49 Expect.equals(true, exception_caught); | 57 Expect.equals(true, exception_caught); |
50 } | 58 } |
51 | 59 |
52 static testIndex() { | 60 static testIndex() { |
53 String str = "string"; | 61 String str = "string"; |
54 for (int i = 0; i < str.length; i++) { | 62 for (int i = 0; i < str.length; i++) { |
55 Expect.equals(true, str[i] is String); | 63 Expect.equals(true, str[i] is String); |
56 Expect.equals(1, str[i].length); | 64 testStringLength(1, str[i]); |
57 } | 65 } |
58 } | 66 } |
59 | 67 |
60 static testCodeUnitAt() { | 68 static testCodeUnitAt() { |
61 String str = "string"; | 69 String str = "string"; |
62 for (int i = 0; i < str.length; i++) { | 70 for (int i = 0; i < str.length; i++) { |
63 Expect.equals(true, str.codeUnitAt(i) is int); | 71 Expect.equals(true, str.codeUnitAt(i) is int); |
64 } | 72 } |
65 } | 73 } |
66 | 74 |
67 static testConcat() { | 75 static testConcat() { |
68 var a = "One"; | 76 var a = "One"; |
69 var b = "Four"; | 77 var b = "Four"; |
70 var c = a + b; | 78 var c = a + b; |
71 Expect.equals(7, c.length); | 79 testStringLength(7, c); |
72 Expect.equals("OneFour", c); | 80 Expect.equals("OneFour", c); |
73 } | 81 } |
74 | 82 |
75 static testEquals() { | 83 static testEquals() { |
76 Expect.equals("str", "str"); | 84 Expect.equals("str", "str"); |
77 | 85 |
78 Expect.equals("str", "s" + "t" + "r"); | 86 Expect.equals("str", "s" + "t" + "r"); |
79 Expect.equals("s" + "t" + "r", "str"); | 87 Expect.equals("s" + "t" + "r", "str"); |
80 | 88 |
81 Expect.equals(false, "str" == "s"); | 89 Expect.equals(false, "str" == "s"); |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
279 for (int i = 0; i < str.length; i++) { | 287 for (int i = 0; i < str.length; i++) { |
280 Expect.equals(str.codeUnitAt(i), list[i]); | 288 Expect.equals(str.codeUnitAt(i), list[i]); |
281 } | 289 } |
282 } | 290 } |
283 test("abc"); | 291 test("abc"); |
284 test(""); | 292 test(""); |
285 test(" "); | 293 test(" "); |
286 } | 294 } |
287 } | 295 } |
288 | 296 |
| 297 void testStringLength(int length, String str) { |
| 298 Expect.equals(length, str.length); |
| 299 (length == 0 ? Expect.isTrue : Expect.isFalse)(str.isEmpty); |
| 300 (length != 0 ? Expect.isTrue : Expect.isFalse)(str.isNotEmpty); |
| 301 } |
| 302 |
289 main() { | 303 main() { |
290 StringTest.testMain(); | 304 StringTest.testMain(); |
291 } | 305 } |
OLD | NEW |