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