| OLD | NEW |
| (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 // TODO(ngeoffray): test String methods with null arguments. | |
| 6 class StringTest { | |
| 7 | |
| 8 static testMain() { | |
| 9 testOutOfRange(); | |
| 10 testIllegalArgument(); | |
| 11 testConcat(); | |
| 12 testIndex(); | |
| 13 testCharCodeAt(); | |
| 14 testEquals(); | |
| 15 testEndsWith(); | |
| 16 testStartsWith(); | |
| 17 testIndexOf(); | |
| 18 testLastIndexOf(); | |
| 19 testContains(); | |
| 20 testReplaceAll(); | |
| 21 testCompareTo(); | |
| 22 testToList(); | |
| 23 testCharCodes(); | |
| 24 } | |
| 25 | |
| 26 static void testOutOfRange() { | |
| 27 String a = "Hello"; | |
| 28 bool exception_caught = false; | |
| 29 try { | |
| 30 var c = a[20]; // Throw exception. | |
| 31 } catch (IndexOutOfRangeException e) { | |
| 32 exception_caught = true; | |
| 33 } | |
| 34 Expect.equals(true, exception_caught); | |
| 35 } | |
| 36 | |
| 37 static testIllegalArgument() { | |
| 38 String a = "Hello"; | |
| 39 bool exception_caught = false; | |
| 40 try { | |
| 41 var c = a[2.2]; // Throw exception. | |
| 42 Expect.equals(true, false); | |
| 43 } catch (IllegalArgumentException e) { | |
| 44 exception_caught = true; | |
| 45 } catch (TypeError e) { // Thrown in checked mode only. | |
| 46 exception_caught = true; | |
| 47 } | |
| 48 Expect.equals(true, exception_caught); | |
| 49 } | |
| 50 | |
| 51 static testIndex() { | |
| 52 String str = "string"; | |
| 53 for (int i = 0; i < str.length; i++) { | |
| 54 Expect.equals(true, str[i] is String); | |
| 55 Expect.equals(1, str[i].length); | |
| 56 } | |
| 57 } | |
| 58 | |
| 59 static testCharCodeAt() { | |
| 60 String str = "string"; | |
| 61 for (int i = 0; i < str.length; i++) { | |
| 62 Expect.equals(true, str.charCodeAt(i) is int); | |
| 63 } | |
| 64 } | |
| 65 | |
| 66 static testConcat() { | |
| 67 var a = "One"; | |
| 68 var b = "Four"; | |
| 69 var c = a.concat(b); | |
| 70 Expect.equals(7, c.length); | |
| 71 Expect.equals("OneFour", c); | |
| 72 } | |
| 73 | |
| 74 static testEquals() { | |
| 75 Expect.equals("str", "str"); | |
| 76 | |
| 77 Expect.equals("str", "s".concat("t").concat("r")); | |
| 78 Expect.equals("s".concat("t").concat("r"), "str"); | |
| 79 | |
| 80 Expect.equals(false, "str" == "s"); | |
| 81 Expect.equals(false, "str" == "r"); | |
| 82 Expect.equals(false, "str" == "st"); | |
| 83 Expect.equals(false, "str" == "tr"); | |
| 84 | |
| 85 Expect.equals(false, "s" == "str"); | |
| 86 Expect.equals(false, "r" == "str"); | |
| 87 Expect.equals(false, "st" == "str"); | |
| 88 Expect.equals(false, "tr" == "str"); | |
| 89 | |
| 90 Expect.equals(false, "" == "s"); | |
| 91 Expect.equals("", ""); | |
| 92 } | |
| 93 | |
| 94 static testEndsWith() { | |
| 95 Expect.equals(true, "str".endsWith("r")); | |
| 96 Expect.equals(true, "str".endsWith("tr")); | |
| 97 Expect.equals(true, "str".endsWith("str")); | |
| 98 | |
| 99 Expect.equals(false, "str".endsWith("stri")); | |
| 100 Expect.equals(false, "str".endsWith("t")); | |
| 101 Expect.equals(false, "str".endsWith("st")); | |
| 102 Expect.equals(false, "str".endsWith("s")); | |
| 103 | |
| 104 Expect.equals(true, "".endsWith("")); | |
| 105 Expect.equals(false, "".endsWith("s")); | |
| 106 } | |
| 107 | |
| 108 static testStartsWith() { | |
| 109 Expect.equals(true, "str".startsWith("s")); | |
| 110 Expect.equals(true, "str".startsWith("st")); | |
| 111 Expect.equals(true, "str".startsWith("str")); | |
| 112 | |
| 113 Expect.equals(false, "str".startsWith("stri")); | |
| 114 Expect.equals(false, "str".startsWith("r")); | |
| 115 Expect.equals(false, "str".startsWith("tr")); | |
| 116 Expect.equals(false, "str".startsWith("t")); | |
| 117 | |
| 118 Expect.equals(true, "".startsWith("")); | |
| 119 Expect.equals(false, "".startsWith("s")); | |
| 120 } | |
| 121 | |
| 122 static testIndexOf() { | |
| 123 Expect.equals(0, "str".indexOf("", 0)); | |
| 124 Expect.equals(0, "".indexOf("", 0)); | |
| 125 Expect.equals(-1, "".indexOf("a", 0)); | |
| 126 | |
| 127 Expect.equals(1, "str".indexOf("t", 0)); | |
| 128 Expect.equals(1, "str".indexOf("tr", 0)); | |
| 129 Expect.equals(0, "str".indexOf("str", 0)); | |
| 130 Expect.equals(0, "str".indexOf("st", 0)); | |
| 131 Expect.equals(0, "str".indexOf("s", 0)); | |
| 132 Expect.equals(2, "str".indexOf("r", 0)); | |
| 133 Expect.equals(-1, "str".indexOf("string", 0)); | |
| 134 | |
| 135 Expect.equals(1, "strstr".indexOf("t", 0)); | |
| 136 Expect.equals(1, "strstr".indexOf("tr", 0)); | |
| 137 Expect.equals(0, "strstr".indexOf("str", 0)); | |
| 138 Expect.equals(0, "strstr".indexOf("st", 0)); | |
| 139 Expect.equals(0, "strstr".indexOf("s", 0)); | |
| 140 Expect.equals(2, "strstr".indexOf("r", 0)); | |
| 141 Expect.equals(-1, "str".indexOf("string", 0)); | |
| 142 | |
| 143 Expect.equals(4, "strstr".indexOf("t", 2)); | |
| 144 Expect.equals(4, "strstr".indexOf("tr", 2)); | |
| 145 Expect.equals(3, "strstr".indexOf("str", 1)); | |
| 146 Expect.equals(3, "strstr".indexOf("str", 2)); | |
| 147 Expect.equals(3, "strstr".indexOf("str", 3)); | |
| 148 Expect.equals(3, "strstr".indexOf("st", 1)); | |
| 149 Expect.equals(3, "strstr".indexOf("s", 3)); | |
| 150 Expect.equals(5, "strstr".indexOf("r", 3)); | |
| 151 Expect.equals(5, "strstr".indexOf("r", 4)); | |
| 152 Expect.equals(5, "strstr".indexOf("r", 5)); | |
| 153 | |
| 154 String str = "hello"; | |
| 155 for (int i = 0; i < 10; i++) { | |
| 156 int result = str.indexOf("", i); | |
| 157 if (i > str.length) { | |
| 158 Expect.equals(str.length, result); | |
| 159 } else { | |
| 160 Expect.equals(i, result); | |
| 161 } | |
| 162 } | |
| 163 } | |
| 164 | |
| 165 static testLastIndexOf() { | |
| 166 Expect.equals(2, "str".lastIndexOf("", 2)); | |
| 167 Expect.equals(0, "".lastIndexOf("", 0)); | |
| 168 Expect.equals(-1, "".lastIndexOf("a", 0)); | |
| 169 | |
| 170 Expect.equals(1, "str".lastIndexOf("t", 2)); | |
| 171 Expect.equals(1, "str".lastIndexOf("tr", 2)); | |
| 172 Expect.equals(0, "str".lastIndexOf("str", 2)); | |
| 173 Expect.equals(0, "str".lastIndexOf("st", 2)); | |
| 174 Expect.equals(0, "str".lastIndexOf("s", 2)); | |
| 175 Expect.equals(2, "str".lastIndexOf("r", 2)); | |
| 176 Expect.equals(-1, "str".lastIndexOf("string", 2)); | |
| 177 | |
| 178 Expect.equals(4, "strstr".lastIndexOf("t", 5)); | |
| 179 Expect.equals(4, "strstr".lastIndexOf("tr", 5)); | |
| 180 Expect.equals(3, "strstr".lastIndexOf("str", 5)); | |
| 181 Expect.equals(3, "strstr".lastIndexOf("st", 5)); | |
| 182 Expect.equals(3, "strstr".lastIndexOf("s", 5)); | |
| 183 Expect.equals(5, "strstr".lastIndexOf("r", 5)); | |
| 184 Expect.equals(-1, "str".lastIndexOf("string", 5)); | |
| 185 | |
| 186 Expect.equals(4, "strstr".lastIndexOf("t", 5)); | |
| 187 Expect.equals(4, "strstr".lastIndexOf("tr", 5)); | |
| 188 Expect.equals(3, "strstr".lastIndexOf("str", 5)); | |
| 189 Expect.equals(3, "strstr".lastIndexOf("str", 5)); | |
| 190 Expect.equals(3, "strstr".lastIndexOf("str", 5)); | |
| 191 Expect.equals(3, "strstr".lastIndexOf("st", 5)); | |
| 192 Expect.equals(3, "strstr".lastIndexOf("s", 5)); | |
| 193 Expect.equals(5, "strstr".lastIndexOf("r", 5)); | |
| 194 Expect.equals(2, "strstr".lastIndexOf("r", 4)); | |
| 195 Expect.equals(2, "strstr".lastIndexOf("r", 3)); | |
| 196 | |
| 197 String str = "hello"; | |
| 198 for (int i = 0; i < 10; i++) { | |
| 199 int result = str.lastIndexOf("", i); | |
| 200 if (i > str.length) { | |
| 201 Expect.equals(str.length, result); | |
| 202 } else { | |
| 203 Expect.equals(i, result); | |
| 204 } | |
| 205 } | |
| 206 } | |
| 207 | |
| 208 static testContains() { | |
| 209 Expect.equals(true, "str".contains("s", 0)); | |
| 210 Expect.equals(true, "str".contains("st", 0)); | |
| 211 Expect.equals(true, "str".contains("str", 0)); | |
| 212 Expect.equals(true, "str".contains("t", 0)); | |
| 213 Expect.equals(true, "str".contains("r", 0)); | |
| 214 Expect.equals(true, "str".contains("tr", 0)); | |
| 215 | |
| 216 Expect.equals(false, "str".contains("sr", 0)); | |
| 217 Expect.equals(false, "str".contains("string", 0)); | |
| 218 | |
| 219 Expect.equals(true, "str".contains("", 0)); | |
| 220 Expect.equals(true, "".contains("", 0)); | |
| 221 Expect.equals(false, "".contains("s", 0)); | |
| 222 } | |
| 223 | |
| 224 static testReplaceAll() { | |
| 225 Expect.equals( | |
| 226 "AtoBtoCDtoE", "AfromBfromCDfromE".replaceAll("from", "to")); | |
| 227 | |
| 228 // Test with the replaced string at the begining. | |
| 229 Expect.equals( | |
| 230 "toABtoCDtoE", "fromABfromCDfromE".replaceAll("from", "to")); | |
| 231 | |
| 232 // Test with the replaced string at the end. | |
| 233 Expect.equals( | |
| 234 "toABtoCDtoEto", "fromABfromCDfromEfrom".replaceAll("from", "to")); | |
| 235 | |
| 236 // Test when there are no occurence of the string to replace. | |
| 237 Expect.equals("ABC", "ABC".replaceAll("from", "to")); | |
| 238 | |
| 239 // Test when the string to change is the empty string. | |
| 240 Expect.equals("", "".replaceAll("from", "to")); | |
| 241 | |
| 242 // Test when the string to change is a substring of the string to | |
| 243 // replace. | |
| 244 Expect.equals("fro", "fro".replaceAll("from", "to")); | |
| 245 | |
| 246 // Test when the string to change is the replaced string. | |
| 247 Expect.equals("to", "from".replaceAll("from", "to")); | |
| 248 | |
| 249 // Test when the string to change is the replacement string. | |
| 250 Expect.equals("to", "to".replaceAll("from", "to")); | |
| 251 | |
| 252 // Test replacing by the empty string. | |
| 253 Expect.equals("", "from".replaceAll("from", "")); | |
| 254 Expect.equals("AB", "AfromB".replaceAll("from", "")); | |
| 255 | |
| 256 // Test changing the empty string. | |
| 257 Expect.equals("to", "".replaceAll("", "to")); | |
| 258 | |
| 259 // Test replacing the empty string. | |
| 260 Expect.equals("toAtoBtoCto", "ABC".replaceAll("", "to")); | |
| 261 } | |
| 262 | |
| 263 static testCompareTo() { | |
| 264 Expect.equals(0, "".compareTo("")); | |
| 265 Expect.equals(0, "str".compareTo("str")); | |
| 266 Expect.equals(-1, "str".compareTo("string")); | |
| 267 Expect.equals(1, "string".compareTo("str")); | |
| 268 Expect.equals(1, "string".compareTo("")); | |
| 269 Expect.equals(-1, "".compareTo("string")); | |
| 270 } | |
| 271 | |
| 272 static testToList() { | |
| 273 test(str) { | |
| 274 var list = str.splitChars(); | |
| 275 Expect.equals(str.length, list.length); | |
| 276 for (int i = 0; i < str.length; i++) { | |
| 277 Expect.equals(str[i], list[i]); | |
| 278 } | |
| 279 } | |
| 280 test("abc"); | |
| 281 test(""); | |
| 282 test(" "); | |
| 283 } | |
| 284 | |
| 285 static testCharCodes() { | |
| 286 test(str) { | |
| 287 var list = str.charCodes(); | |
| 288 Expect.equals(str.length, list.length); | |
| 289 for (int i = 0; i < str.length; i++) { | |
| 290 Expect.equals(str.charCodeAt(i), list[i]); | |
| 291 } | |
| 292 } | |
| 293 test("abc"); | |
| 294 test(""); | |
| 295 test(" "); | |
| 296 } | |
| 297 } | |
| 298 | |
| 299 main() { | |
| 300 StringTest.testMain(); | |
| 301 } | |
| OLD | NEW |