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

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

Issue 16957002: Use Pattern.matchAsPrefix to let String.indexOf/lastIndexOf/startsWith accept Pattern. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 6 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 | Annotate | Revision Log
OLDNEW
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() {
(...skipping 22 matching lines...) Expand all
33 } 33 }
34 34
35 static void testOutOfRange() { 35 static 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.equals(true, exception_caught); 43 Expect.isTrue(exception_caught);
44 } 44 }
45 45
46 static testIllegalArgument() { 46 static 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.equals(true, false); 51 Expect.fail("Accepting doulbe as index");
floitsch 2013/06/13 11:54:58 double
Lasse Reichstein Nielsen 2013/06/13 12:37:49 Done.
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) { // Thrown in checked mode only.
55 exception_caught = true; 55 exception_caught = true;
56 } 56 }
57 Expect.equals(true, exception_caught); 57 Expect.isTrue(exception_caught);
58 } 58 }
59 59
60 static testIndex() { 60 static testIndex() {
61 String str = "string"; 61 String str = "string";
62 for (int i = 0; i < str.length; i++) { 62 for (int i = 0; i < str.length; i++) {
63 Expect.equals(true, str[i] is String); 63 Expect.isTrue(str[i] is String);
64 testStringLength(1, str[i]); 64 testStringLength(1, str[i]);
65 } 65 }
66 } 66 }
67 67
68 static testCodeUnitAt() { 68 static testCodeUnitAt() {
69 String str = "string"; 69 String str = "string";
70 for (int i = 0; i < str.length; i++) { 70 for (int i = 0; i < str.length; i++) {
71 Expect.equals(true, str.codeUnitAt(i) is int); 71 Expect.isTrue(str.codeUnitAt(i) is int);
72 } 72 }
73 } 73 }
74 74
75 static testConcat() { 75 static testConcat() {
76 var a = "One"; 76 var a = "One";
77 var b = "Four"; 77 var b = "Four";
78 var c = a + b; 78 var c = a + b;
79 testStringLength(7, c); 79 testStringLength(7, c);
80 Expect.equals("OneFour", c); 80 Expect.equals("OneFour", c);
81 } 81 }
82 82
83 static testEquals() { 83 static testEquals() {
84 Expect.equals("str", "str"); 84 Expect.equals("str", "str");
85 85
86 Expect.equals("str", "s" + "t" + "r"); 86 Expect.equals("str", "s" + "t" + "r");
87 Expect.equals("s" + "t" + "r", "str"); 87 Expect.equals("s" + "t" + "r", "str");
88 88
89 Expect.equals(false, "str" == "s"); 89 Expect.isFalse("str" == "s");
90 Expect.equals(false, "str" == "r"); 90 Expect.isFalse("str" == "r");
91 Expect.equals(false, "str" == "st"); 91 Expect.isFalse("str" == "st");
92 Expect.equals(false, "str" == "tr"); 92 Expect.isFalse("str" == "tr");
93 93
94 Expect.equals(false, "s" == "str"); 94 Expect.isFalse("s" == "str");
95 Expect.equals(false, "r" == "str"); 95 Expect.isFalse("r" == "str");
96 Expect.equals(false, "st" == "str"); 96 Expect.isFalse("st" == "str");
97 Expect.equals(false, "tr" == "str"); 97 Expect.isFalse("tr" == "str");
98 98
99 Expect.equals(false, "" == "s"); 99 Expect.isFalse("" == "s");
100 Expect.equals("", ""); 100 Expect.equals("", "");
101 } 101 }
102 102
103 static testEndsWith() { 103 static testEndsWith() {
104 Expect.equals(true, "str".endsWith("r")); 104 Expect.isTrue("str".endsWith("r"));
105 Expect.equals(true, "str".endsWith("tr")); 105 Expect.isTrue("str".endsWith("tr"));
106 Expect.equals(true, "str".endsWith("str")); 106 Expect.isTrue("str".endsWith("str"));
107 107
108 Expect.equals(false, "str".endsWith("stri")); 108 Expect.isFalse("str".endsWith("stri"));
109 Expect.equals(false, "str".endsWith("t")); 109 Expect.isFalse("str".endsWith("t"));
110 Expect.equals(false, "str".endsWith("st")); 110 Expect.isFalse("str".endsWith("st"));
111 Expect.equals(false, "str".endsWith("s")); 111 Expect.isFalse("str".endsWith("s"));
112 112
113 Expect.equals(true, "".endsWith("")); 113 Expect.isTrue("".endsWith(""));
114 Expect.equals(false, "".endsWith("s")); 114 Expect.isFalse("".endsWith("s"));
115 } 115 }
116 116
117 static testStartsWith() { 117 static testStartsWith() {
118 Expect.equals(true, "str".startsWith("s")); 118 Expect.isTrue("str".startsWith("s"));
119 Expect.equals(true, "str".startsWith("st")); 119 Expect.isTrue("str".startsWith("st"));
120 Expect.equals(true, "str".startsWith("str")); 120 Expect.isTrue("str".startsWith("str"));
121 121
122 Expect.equals(false, "str".startsWith("stri")); 122 Expect.isFalse("str".startsWith("stri"));
123 Expect.equals(false, "str".startsWith("r")); 123 Expect.isFalse("str".startsWith("r"));
124 Expect.equals(false, "str".startsWith("tr")); 124 Expect.isFalse("str".startsWith("tr"));
125 Expect.equals(false, "str".startsWith("t")); 125 Expect.isFalse("str".startsWith("t"));
126 126
127 Expect.equals(true, "".startsWith("")); 127 Expect.isTrue("".startsWith(""));
128 Expect.equals(false, "".startsWith("s")); 128 Expect.isFalse("".startsWith("s"));
129
130 var regexp = new RegExp("s(?:tr?)?");
131 Expect.isTrue("s".startsWith(regexp));
132 Expect.isTrue("st".startsWith(regexp));
133 Expect.isTrue("str".startsWith(regexp));
134 Expect.isTrue("sX".startsWith(regexp));
135 Expect.isTrue("stX".startsWith(regexp));
136 Expect.isTrue("strX".startsWith(regexp));
137
138 Expect.isFalse("".startsWith(regexp));
139 Expect.isFalse("astr".startsWith(regexp));
140
141 Expect.isTrue("".startsWith(new RegExp("")));
142 Expect.isTrue("".startsWith(new RegExp("a?")));
129 } 143 }
130 144
131 static testIndexOf() { 145 static testIndexOf() {
132 Expect.equals(0, "str".indexOf("", 0)); 146 Expect.equals(0, "str".indexOf("", 0));
133 Expect.equals(0, "".indexOf("", 0)); 147 Expect.equals(0, "".indexOf("", 0));
134 Expect.equals(-1, "".indexOf("a", 0)); 148 Expect.equals(-1, "".indexOf("a", 0));
135 149
136 Expect.equals(1, "str".indexOf("t", 0)); 150 Expect.equals(1, "str".indexOf("t", 0));
137 Expect.equals(1, "str".indexOf("tr", 0)); 151 Expect.equals(1, "str".indexOf("tr", 0));
138 Expect.equals(0, "str".indexOf("str", 0)); 152 Expect.equals(0, "str".indexOf("str", 0));
(...skipping 16 matching lines...) Expand all
155 Expect.equals(3, "strstr".indexOf("str", 2)); 169 Expect.equals(3, "strstr".indexOf("str", 2));
156 Expect.equals(3, "strstr".indexOf("str", 3)); 170 Expect.equals(3, "strstr".indexOf("str", 3));
157 Expect.equals(3, "strstr".indexOf("st", 1)); 171 Expect.equals(3, "strstr".indexOf("st", 1));
158 Expect.equals(3, "strstr".indexOf("s", 3)); 172 Expect.equals(3, "strstr".indexOf("s", 3));
159 Expect.equals(5, "strstr".indexOf("r", 3)); 173 Expect.equals(5, "strstr".indexOf("r", 3));
160 Expect.equals(5, "strstr".indexOf("r", 4)); 174 Expect.equals(5, "strstr".indexOf("r", 4));
161 Expect.equals(5, "strstr".indexOf("r", 5)); 175 Expect.equals(5, "strstr".indexOf("r", 5));
162 176
163 String str = "hello"; 177 String str = "hello";
164 for (int i = 0; i < 10; i++) { 178 for (int i = 0; i < 10; i++) {
165 int result = str.indexOf("", i);
166 if (i > str.length) { 179 if (i > str.length) {
167 Expect.equals(str.length, result); 180 Expect.throws(() => str.indexOf("", i));
168 } else { 181 } else {
182 int result = str.indexOf("", i);
169 Expect.equals(i, result); 183 Expect.equals(i, result);
170 } 184 }
171 } 185 }
186
187 var re = new RegExp("an?");
188 Expect.equals(1, "banana".indexOf(re));
189 Expect.equals(1, "banana".indexOf(re, 0));
190 Expect.equals(1, "banana".indexOf(re, 1));
191 Expect.equals(3, "banana".indexOf(re, 2));
192 Expect.equals(3, "banana".indexOf(re, 3));
193 Expect.equals(5, "banana".indexOf(re, 4));
194 Expect.equals(5, "banana".indexOf(re, 5));
195 Expect.equals(-1, "banana".indexOf(re, 6));
196 Expect.throws(() => "banana".indexOf(re, -1));
197 Expect.throws(() => "banana".indexOf(re, 7));
198 re = new RegExp("x?");
199 for (int i = 0; i <= str.length; i++) {
200 Expect.equals(i, str.indexOf(re, i));
201 }
172 } 202 }
173 203
174 static testLastIndexOf() { 204 static testLastIndexOf() {
175 Expect.equals(2, "str".lastIndexOf("", 2)); 205 Expect.equals(2, "str".lastIndexOf("", 2));
176 Expect.equals(0, "".lastIndexOf("", 0)); 206 Expect.equals(0, "".lastIndexOf("", 0));
177 Expect.equals(-1, "".lastIndexOf("a", 0)); 207 Expect.equals(-1, "".lastIndexOf("a", 0));
178 208
179 Expect.equals(1, "str".lastIndexOf("t", 2)); 209 Expect.equals(1, "str".lastIndexOf("t", 2));
180 Expect.equals(1, "str".lastIndexOf("tr", 2)); 210 Expect.equals(1, "str".lastIndexOf("tr", 2));
181 Expect.equals(0, "str".lastIndexOf("str", 2)); 211 Expect.equals(0, "str".lastIndexOf("str", 2));
182 Expect.equals(0, "str".lastIndexOf("st", 2)); 212 Expect.equals(0, "str".lastIndexOf("st", 2));
183 Expect.equals(0, "str".lastIndexOf("s", 2)); 213 Expect.equals(0, "str".lastIndexOf("s", 2));
184 Expect.equals(2, "str".lastIndexOf("r", 2)); 214 Expect.equals(2, "str".lastIndexOf("r", 2));
185 Expect.equals(-1, "str".lastIndexOf("string", 2)); 215 Expect.equals(-1, "str".lastIndexOf("string", 2));
186 216
187 Expect.equals(4, "strstr".lastIndexOf("t", 5)); 217 Expect.equals(4, "strstr".lastIndexOf("t", 5));
188 Expect.equals(4, "strstr".lastIndexOf("tr", 5)); 218 Expect.equals(4, "strstr".lastIndexOf("tr", 5));
189 Expect.equals(3, "strstr".lastIndexOf("str", 5)); 219 Expect.equals(3, "strstr".lastIndexOf("str", 5));
190 Expect.equals(3, "strstr".lastIndexOf("st", 5)); 220 Expect.equals(3, "strstr".lastIndexOf("st", 5));
191 Expect.equals(3, "strstr".lastIndexOf("s", 5)); 221 Expect.equals(3, "strstr".lastIndexOf("s", 5));
192 Expect.equals(5, "strstr".lastIndexOf("r", 5)); 222 Expect.equals(5, "strstr".lastIndexOf("r", 5));
193 Expect.equals(-1, "str".lastIndexOf("string", 5)); 223 Expect.throws(() {
194 224 "str".lastIndexOf("string", 5);
225 });
195 Expect.equals(4, "strstr".lastIndexOf("t", 5)); 226 Expect.equals(4, "strstr".lastIndexOf("t", 5));
196 Expect.equals(4, "strstr".lastIndexOf("tr", 5)); 227 Expect.equals(4, "strstr".lastIndexOf("tr", 5));
197 Expect.equals(3, "strstr".lastIndexOf("str", 5)); 228 Expect.equals(3, "strstr".lastIndexOf("str", 5));
198 Expect.equals(3, "strstr".lastIndexOf("str", 5)); 229 Expect.equals(3, "strstr".lastIndexOf("str", 5));
199 Expect.equals(3, "strstr".lastIndexOf("str", 5)); 230 Expect.equals(3, "strstr".lastIndexOf("str", 5));
200 Expect.equals(3, "strstr".lastIndexOf("st", 5)); 231 Expect.equals(3, "strstr".lastIndexOf("st", 5));
201 Expect.equals(3, "strstr".lastIndexOf("s", 5)); 232 Expect.equals(3, "strstr".lastIndexOf("s", 5));
202 Expect.equals(5, "strstr".lastIndexOf("r", 5)); 233 Expect.equals(5, "strstr".lastIndexOf("r", 5));
203 Expect.equals(2, "strstr".lastIndexOf("r", 4)); 234 Expect.equals(2, "strstr".lastIndexOf("r", 4));
204 Expect.equals(2, "strstr".lastIndexOf("r", 3)); 235 Expect.equals(2, "strstr".lastIndexOf("r", 3));
205 Expect.equals(5, "strstr".lastIndexOf("r")); 236 Expect.equals(5, "strstr".lastIndexOf("r"));
206 Expect.equals(5, "strstr".lastIndexOf("r"), null); 237 Expect.equals(5, "strstr".lastIndexOf("r", null));
207 238
208 String str = "hello"; 239 String str = "hello";
209 for (int i = 0; i < 10; i++) { 240 for (int i = 0; i < 10; i++) {
210 int result = str.lastIndexOf("", i);
211 if (i > str.length) { 241 if (i > str.length) {
212 Expect.equals(str.length, result); 242 Expect.throws(() => str.indexOf("", i));
213 } else { 243 } else {
244 int result = str.lastIndexOf("", i);
214 Expect.equals(i, result); 245 Expect.equals(i, result);
215 } 246 }
216 } 247 }
248
249 var re = new RegExp("an?");
250 Expect.equals(5, "banana".lastIndexOf(re));
251 Expect.equals(5, "banana".lastIndexOf(re, 6));
252 Expect.equals(5, "banana".lastIndexOf(re, 5));
253 Expect.equals(3, "banana".lastIndexOf(re, 4));
254 Expect.equals(3, "banana".lastIndexOf(re, 3));
255 Expect.equals(1, "banana".lastIndexOf(re, 2));
256 Expect.equals(1, "banana".lastIndexOf(re, 1));
257 Expect.equals(-1, "banana".lastIndexOf(re, 0));
258 Expect.throws(() => "banana".lastIndexOf(re, -1));
259 Expect.throws(() => "banana".lastIndexOf(re, 7));
260 re = new RegExp("x?");
261 for (int i = 0; i <= str.length; i++) {
262 Expect.equals(i, str.indexOf(re, i));
263 }
217 } 264 }
218 265
219 static testContains() { 266 static testContains() {
220 Expect.equals(true, "str".contains("s", 0)); 267 Expect.isTrue("str".contains("s", 0));
221 Expect.equals(true, "str".contains("st", 0)); 268 Expect.isTrue("str".contains("st", 0));
222 Expect.equals(true, "str".contains("str", 0)); 269 Expect.isTrue("str".contains("str", 0));
223 Expect.equals(true, "str".contains("t", 0)); 270 Expect.isTrue("str".contains("t", 0));
224 Expect.equals(true, "str".contains("r", 0)); 271 Expect.isTrue("str".contains("r", 0));
225 Expect.equals(true, "str".contains("tr", 0)); 272 Expect.isTrue("str".contains("tr", 0));
226 273
227 Expect.equals(false, "str".contains("sr", 0)); 274 Expect.isFalse("str".contains("sr", 0));
228 Expect.equals(false, "str".contains("string", 0)); 275 Expect.isFalse("str".contains("string", 0));
229 276
230 Expect.equals(true, "str".contains("", 0)); 277 Expect.isTrue("str".contains("", 0));
231 Expect.equals(true, "".contains("", 0)); 278 Expect.isTrue("".contains("", 0));
232 Expect.equals(false, "".contains("s", 0)); 279 Expect.isFalse("".contains("s", 0));
233 } 280 }
234 281
235 static testReplaceAll() { 282 static testReplaceAll() {
236 Expect.equals( 283 Expect.equals(
237 "AtoBtoCDtoE", "AfromBfromCDfromE".replaceAll("from", "to")); 284 "AtoBtoCDtoE", "AfromBfromCDfromE".replaceAll("from", "to"));
238 285
239 // Test with the replaced string at the begining. 286 // Test with the replaced string at the begining.
240 Expect.equals( 287 Expect.equals(
241 "toABtoCDtoE", "fromABfromCDfromE".replaceAll("from", "to")); 288 "toABtoCDtoE", "fromABfromCDfromE".replaceAll("from", "to"));
242 289
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
296 343
297 void testStringLength(int length, String str) { 344 void testStringLength(int length, String str) {
298 Expect.equals(length, str.length); 345 Expect.equals(length, str.length);
299 (length == 0 ? Expect.isTrue : Expect.isFalse)(str.isEmpty); 346 (length == 0 ? Expect.isTrue : Expect.isFalse)(str.isEmpty);
300 (length != 0 ? Expect.isTrue : Expect.isFalse)(str.isNotEmpty); 347 (length != 0 ? Expect.isTrue : Expect.isFalse)(str.isNotEmpty);
301 } 348 }
302 349
303 main() { 350 main() {
304 StringTest.testMain(); 351 StringTest.testMain();
305 } 352 }
OLDNEW
« runtime/lib/string_patch.dart ('K') | « tests/co19/co19-runtime.status ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698