| 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 // VMOptions=--enable_type_checks --enable_asserts | |
| 5 // | |
| 6 // Dart test program testing type checks. | |
| 7 | |
| 8 class TypeTest { | |
| 9 static test() { | |
| 10 int result = 0; | |
| 11 try { | |
| 12 int i = "hello"; // Throws a TypeError if type checks are enabled. | |
| 13 } catch (TypeError error) { | |
| 14 result = 1; | |
| 15 Expect.equals("int", error.dstType); | |
| 16 Expect.equals("OneByteString", error.srcType); | |
| 17 Expect.equals("i", error.dstName); | |
| 18 int pos = error.url.lastIndexOf("/", error.url.length); | |
| 19 if (pos == -1) { | |
| 20 pos = error.url.lastIndexOf("\\", error.url.length); | |
| 21 } | |
| 22 String subs = error.url.substring(pos + 1, error.url.length); | |
| 23 Expect.equals("TypeVMTest.dart", subs); | |
| 24 Expect.equals(12, error.line); | |
| 25 Expect.equals(15, error.column); | |
| 26 } | |
| 27 return result; | |
| 28 } | |
| 29 | |
| 30 static testSideEffect() { | |
| 31 int result = 0; | |
| 32 int index() { | |
| 33 result++; | |
| 34 return 0; | |
| 35 } | |
| 36 try { | |
| 37 List<int> a = new List<int>(1); | |
| 38 a[0] = 0; | |
| 39 a[index()]++; // Type check succeeds, but does not create side effects. | |
| 40 assert(a[0] == 1); | |
| 41 } catch (TypeError error) { | |
| 42 result = 100; | |
| 43 } | |
| 44 return result; | |
| 45 } | |
| 46 | |
| 47 static testArgument() { | |
| 48 int result = 0; | |
| 49 int f(int i) { | |
| 50 return i; | |
| 51 } | |
| 52 try { | |
| 53 int i = f("hello"); // Throws a TypeError if type checks are enabled. | |
| 54 } catch (TypeError error) { | |
| 55 result = 1; | |
| 56 Expect.equals("int", error.dstType); | |
| 57 Expect.equals("OneByteString", error.srcType); | |
| 58 Expect.equals("i", error.dstName); | |
| 59 int pos = error.url.lastIndexOf("/", error.url.length); | |
| 60 if (pos == -1) { | |
| 61 pos = error.url.lastIndexOf("\\", error.url.length); | |
| 62 } | |
| 63 String subs = error.url.substring(pos + 1, error.url.length); | |
| 64 Expect.equals("TypeVMTest.dart", subs); | |
| 65 Expect.equals(49, error.line); | |
| 66 Expect.equals(15, error.column); | |
| 67 } | |
| 68 return result; | |
| 69 } | |
| 70 | |
| 71 static testReturn() { | |
| 72 int result = 0; | |
| 73 int f(String s) { | |
| 74 return s; | |
| 75 } | |
| 76 try { | |
| 77 int i = f("hello"); // Throws a TypeError if type checks are enabled. | |
| 78 } catch (TypeError error) { | |
| 79 result = 1; | |
| 80 Expect.equals("int", error.dstType); | |
| 81 Expect.equals("OneByteString", error.srcType); | |
| 82 Expect.equals("function result", error.dstName); | |
| 83 int pos = error.url.lastIndexOf("/", error.url.length); | |
| 84 if (pos == -1) { | |
| 85 pos = error.url.lastIndexOf("\\", error.url.length); | |
| 86 } | |
| 87 String subs = error.url.substring(pos + 1, error.url.length); | |
| 88 Expect.equals("TypeVMTest.dart", subs); | |
| 89 Expect.equals(74, error.line); | |
| 90 Expect.equals(14, error.column); | |
| 91 } | |
| 92 return result; | |
| 93 } | |
| 94 | |
| 95 static int field; | |
| 96 static testField() { | |
| 97 int result = 0; | |
| 98 try { | |
| 99 field = "hello"; // Throws a TypeError if type checks are enabled. | |
| 100 } catch (TypeError error) { | |
| 101 result = 1; | |
| 102 Expect.equals("int", error.dstType); | |
| 103 Expect.equals("OneByteString", error.srcType); | |
| 104 Expect.equals("field", error.dstName); | |
| 105 int pos = error.url.lastIndexOf("/", error.url.length); | |
| 106 if (pos == -1) { | |
| 107 pos = error.url.lastIndexOf("\\", error.url.length); | |
| 108 } | |
| 109 String subs = error.url.substring(pos + 1, error.url.length); | |
| 110 Expect.equals("TypeVMTest.dart", subs); | |
| 111 Expect.equals(99, error.line); | |
| 112 Expect.equals(15, error.column); | |
| 113 } | |
| 114 return result; | |
| 115 } | |
| 116 | |
| 117 static testAnyFunction() { | |
| 118 int result = 0; | |
| 119 Function anyFunction; | |
| 120 f() { }; | |
| 121 anyFunction = f; // No error. | |
| 122 try { | |
| 123 int i = f; // Throws a TypeError if type checks are enabled. | |
| 124 } catch (TypeError error) { | |
| 125 result = 1; | |
| 126 Expect.equals("int", error.dstType); | |
| 127 Expect.equals("() => Dynamic", error.srcType); | |
| 128 Expect.equals("i", error.dstName); | |
| 129 int pos = error.url.lastIndexOf("/", error.url.length); | |
| 130 if (pos == -1) { | |
| 131 pos = error.url.lastIndexOf("\\", error.url.length); | |
| 132 } | |
| 133 String subs = error.url.substring(pos + 1, error.url.length); | |
| 134 Expect.equals("TypeVMTest.dart", subs); | |
| 135 Expect.equals(123, error.line); | |
| 136 Expect.equals(15, error.column); | |
| 137 } | |
| 138 return result; | |
| 139 } | |
| 140 | |
| 141 static testVoidFunction() { | |
| 142 int result = 0; | |
| 143 Function anyFunction; | |
| 144 void acceptVoidFunObj(void voidFunObj(Object obj)) { }; | |
| 145 void acceptObjFunObj(Object objFunObj(Object obj)) { }; | |
| 146 void voidFunObj(Object obj) { }; | |
| 147 Object objFunObj(Object obj) { return obj; }; | |
| 148 anyFunction = voidFunObj; // No error. | |
| 149 anyFunction = objFunObj; // No error. | |
| 150 acceptVoidFunObj(voidFunObj); | |
| 151 acceptVoidFunObj(objFunObj); | |
| 152 acceptObjFunObj(objFunObj); | |
| 153 try { | |
| 154 acceptObjFunObj(voidFunObj); // Throws a TypeError. | |
| 155 } catch (TypeError error) { | |
| 156 result = 1; | |
| 157 Expect.equals("(Object) => Object", error.dstType); | |
| 158 Expect.equals("(Object) => void", error.srcType); | |
| 159 Expect.equals("objFunObj", error.dstName); | |
| 160 int pos = error.url.lastIndexOf("/", error.url.length); | |
| 161 if (pos == -1) { | |
| 162 pos = error.url.lastIndexOf("\\", error.url.length); | |
| 163 } | |
| 164 String subs = error.url.substring(pos + 1, error.url.length); | |
| 165 Expect.equals("TypeVMTest.dart", subs); | |
| 166 Expect.equals(145, error.line); | |
| 167 Expect.equals(33, error.column); | |
| 168 } | |
| 169 return result; | |
| 170 } | |
| 171 | |
| 172 static testFunctionNum() { | |
| 173 int result = 0; | |
| 174 Function anyFunction; | |
| 175 void acceptFunNum(void funNum(num num)) { }; | |
| 176 void funObj(Object obj) { }; | |
| 177 void funNum(num num) { }; | |
| 178 void funInt(int i) { }; | |
| 179 void funString(String s) { }; | |
| 180 anyFunction = funObj; // No error. | |
| 181 anyFunction = funNum; // No error. | |
| 182 anyFunction = funInt; // No error. | |
| 183 anyFunction = funString; // No error. | |
| 184 acceptFunNum(funObj); // No error. | |
| 185 acceptFunNum(funNum); // No error. | |
| 186 acceptFunNum(funInt); // No error. | |
| 187 try { | |
| 188 acceptFunNum(funString); // Throws an error. | |
| 189 } catch (TypeError error) { | |
| 190 result = 1; | |
| 191 Expect.equals("(num) => void", error.dstType); | |
| 192 Expect.equals("(String) => void", error.srcType); | |
| 193 Expect.equals("funNum", error.dstName); | |
| 194 int pos = error.url.lastIndexOf("/", error.url.length); | |
| 195 if (pos == -1) { | |
| 196 pos = error.url.lastIndexOf("\\", error.url.length); | |
| 197 } | |
| 198 String subs = error.url.substring(pos + 1, error.url.length); | |
| 199 Expect.equals("TypeVMTest.dart", subs); | |
| 200 Expect.equals(175, error.line); | |
| 201 Expect.equals(28, error.column); | |
| 202 } | |
| 203 return result; | |
| 204 } | |
| 205 | |
| 206 static testBoolCheck() { | |
| 207 int result = 0; | |
| 208 try { | |
| 209 bool i = !"hello"; // Throws a TypeError if type checks are enabled. | |
| 210 } catch (TypeError error) { | |
| 211 result++; | |
| 212 Expect.equals("bool", error.dstType); | |
| 213 Expect.equals("OneByteString", error.srcType); | |
| 214 Expect.equals("boolean expression", error.dstName); | |
| 215 int pos = error.url.lastIndexOf("/", error.url.length); | |
| 216 if (pos == -1) { | |
| 217 pos = error.url.lastIndexOf("\\", error.url.length); | |
| 218 } | |
| 219 String subs = error.url.substring(pos + 1, error.url.length); | |
| 220 Expect.equals("TypeVMTest.dart", subs); | |
| 221 Expect.equals(209, error.line); | |
| 222 Expect.equals(17, error.column); | |
| 223 } | |
| 224 try { | |
| 225 while ("hello") {}; // Throws a TypeError if type checks are enabled. | |
| 226 } catch (TypeError error) { | |
| 227 result++; | |
| 228 Expect.equals("bool", error.dstType); | |
| 229 Expect.equals("OneByteString", error.srcType); | |
| 230 Expect.equals("boolean expression", error.dstName); | |
| 231 int pos = error.url.lastIndexOf("/", error.url.length); | |
| 232 if (pos == -1) { | |
| 233 pos = error.url.lastIndexOf("\\", error.url.length); | |
| 234 } | |
| 235 String subs = error.url.substring(pos + 1, error.url.length); | |
| 236 Expect.equals("TypeVMTest.dart", subs); | |
| 237 Expect.equals(225, error.line); | |
| 238 Expect.equals(14, error.column); | |
| 239 } | |
| 240 try { | |
| 241 do {} while ("hello"); // Throws a TypeError if type checks are enabled. | |
| 242 } catch (TypeError error) { | |
| 243 result++; | |
| 244 Expect.equals("bool", error.dstType); | |
| 245 Expect.equals("OneByteString", error.srcType); | |
| 246 Expect.equals("boolean expression", error.dstName); | |
| 247 int pos = error.url.lastIndexOf("/", error.url.length); | |
| 248 if (pos == -1) { | |
| 249 pos = error.url.lastIndexOf("\\", error.url.length); | |
| 250 } | |
| 251 String subs = error.url.substring(pos + 1, error.url.length); | |
| 252 Expect.equals("TypeVMTest.dart", subs); | |
| 253 Expect.equals(241, error.line); | |
| 254 Expect.equals(20, error.column); | |
| 255 } | |
| 256 try { | |
| 257 for (;"hello";) {}; // Throws a TypeError if type checks are enabled. | |
| 258 } catch (TypeError error) { | |
| 259 result++; | |
| 260 Expect.equals("bool", error.dstType); | |
| 261 Expect.equals("OneByteString", error.srcType); | |
| 262 Expect.equals("boolean expression", error.dstName); | |
| 263 int pos = error.url.lastIndexOf("/", error.url.length); | |
| 264 if (pos == -1) { | |
| 265 pos = error.url.lastIndexOf("\\", error.url.length); | |
| 266 } | |
| 267 String subs = error.url.substring(pos + 1, error.url.length); | |
| 268 Expect.equals("TypeVMTest.dart", subs); | |
| 269 Expect.equals(257, error.line); | |
| 270 Expect.equals(13, error.column); | |
| 271 } | |
| 272 try { | |
| 273 int i = "hello" ? 1 : 0; // Throws a TypeError if type checks are enabled
. | |
| 274 } catch (TypeError error) { | |
| 275 result++; | |
| 276 Expect.equals("bool", error.dstType); | |
| 277 Expect.equals("OneByteString", error.srcType); | |
| 278 Expect.equals("boolean expression", error.dstName); | |
| 279 int pos = error.url.lastIndexOf("/", error.url.length); | |
| 280 if (pos == -1) { | |
| 281 pos = error.url.lastIndexOf("\\", error.url.length); | |
| 282 } | |
| 283 String subs = error.url.substring(pos + 1, error.url.length); | |
| 284 Expect.equals("TypeVMTest.dart", subs); | |
| 285 Expect.equals(273, error.line); | |
| 286 Expect.equals(15, error.column); | |
| 287 } | |
| 288 try { | |
| 289 if ("hello") {}; // Throws a TypeError if type checks are enabled. | |
| 290 } catch (TypeError error) { | |
| 291 result++; | |
| 292 Expect.equals("bool", error.dstType); | |
| 293 Expect.equals("OneByteString", error.srcType); | |
| 294 Expect.equals("boolean expression", error.dstName); | |
| 295 int pos = error.url.lastIndexOf("/", error.url.length); | |
| 296 if (pos == -1) { | |
| 297 pos = error.url.lastIndexOf("\\", error.url.length); | |
| 298 } | |
| 299 String subs = error.url.substring(pos + 1, error.url.length); | |
| 300 Expect.equals("TypeVMTest.dart", subs); | |
| 301 Expect.equals(289, error.line); | |
| 302 Expect.equals(11, error.column); | |
| 303 } | |
| 304 try { | |
| 305 if ("hello" || false) {}; // Throws a TypeError if type checks are enable
d. | |
| 306 } catch (TypeError error) { | |
| 307 result++; | |
| 308 Expect.equals("bool", error.dstType); | |
| 309 Expect.equals("OneByteString", error.srcType); | |
| 310 Expect.equals("boolean expression", error.dstName); | |
| 311 int pos = error.url.lastIndexOf("/", error.url.length); | |
| 312 if (pos == -1) { | |
| 313 pos = error.url.lastIndexOf("\\", error.url.length); | |
| 314 } | |
| 315 String subs = error.url.substring(pos + 1, error.url.length); | |
| 316 Expect.equals("TypeVMTest.dart", subs); | |
| 317 Expect.equals(305, error.line); | |
| 318 Expect.equals(11, error.column); | |
| 319 } | |
| 320 try { | |
| 321 if (false || "hello") {}; // Throws a TypeError if type checks are enable
d. | |
| 322 } catch (TypeError error) { | |
| 323 result++; | |
| 324 Expect.equals("bool", error.dstType); | |
| 325 Expect.equals("OneByteString", error.srcType); | |
| 326 Expect.equals("boolean expression", error.dstName); | |
| 327 int pos = error.url.lastIndexOf("/", error.url.length); | |
| 328 if (pos == -1) { | |
| 329 pos = error.url.lastIndexOf("\\", error.url.length); | |
| 330 } | |
| 331 String subs = error.url.substring(pos + 1, error.url.length); | |
| 332 Expect.equals("TypeVMTest.dart", subs); | |
| 333 Expect.equals(321, error.line); | |
| 334 Expect.equals(20, error.column); | |
| 335 } | |
| 336 try { | |
| 337 if (null) {}; // Throws a TypeError if type checks are enabled. | |
| 338 } catch (TypeError error) { | |
| 339 result++; | |
| 340 Expect.equals("bool", error.dstType); | |
| 341 Expect.equals("Null", error.srcType); | |
| 342 Expect.equals("boolean expression", error.dstName); | |
| 343 int pos = error.url.lastIndexOf("/", error.url.length); | |
| 344 if (pos == -1) { | |
| 345 pos = error.url.lastIndexOf("\\", error.url.length); | |
| 346 } | |
| 347 String subs = error.url.substring(pos + 1, error.url.length); | |
| 348 Expect.equals("TypeVMTest.dart", subs); | |
| 349 Expect.equals(337, error.line); | |
| 350 Expect.equals(11, error.column); | |
| 351 } | |
| 352 return result; | |
| 353 } | |
| 354 | |
| 355 | |
| 356 static int testFactory() { | |
| 357 int result = 0; | |
| 358 try { | |
| 359 var x = new C(); | |
| 360 } catch (TypeError error) { | |
| 361 result++; | |
| 362 Expect.equals("C", error.dstType); | |
| 363 Expect.equals("int", error.srcType); | |
| 364 Expect.equals("function result", error.dstName); | |
| 365 int pos = error.url.lastIndexOf("/", error.url.length); | |
| 366 if (pos == -1) { | |
| 367 pos = error.url.lastIndexOf("\\", error.url.length); | |
| 368 } | |
| 369 String subs = error.url.substring(pos + 1, error.url.length); | |
| 370 Expect.equals("TypeVMTest.dart", subs); | |
| 371 Expect.equals(472, error.line); | |
| 372 Expect.equals(12, error.column); | |
| 373 } | |
| 374 return result; | |
| 375 } | |
| 376 | |
| 377 static int testListAssigment() { | |
| 378 int result = 0; | |
| 379 { | |
| 380 var a = new List(5); | |
| 381 List a0 = a; | |
| 382 List<Object> ao = a; | |
| 383 List<int> ai = a; | |
| 384 List<num> an = a; | |
| 385 List<String> as = a; | |
| 386 } | |
| 387 { | |
| 388 var a = new List<Object>(5); | |
| 389 List a0 = a; | |
| 390 List<Object> ao = a; | |
| 391 try { | |
| 392 List<int> ai = a; | |
| 393 } catch (TypeError error) { | |
| 394 result++; | |
| 395 } | |
| 396 try { | |
| 397 List<num> an = a; | |
| 398 } catch (TypeError error) { | |
| 399 result++; | |
| 400 } | |
| 401 try { | |
| 402 List<String> as = a; | |
| 403 } catch (TypeError error) { | |
| 404 result++; | |
| 405 } | |
| 406 } | |
| 407 { | |
| 408 var a = new List<int>(5); | |
| 409 List a0 = a; | |
| 410 List<Object> ao = a; | |
| 411 List<int> ai = a; | |
| 412 List<num> an = a; | |
| 413 try { | |
| 414 List<String> as = a; | |
| 415 } catch (TypeError error) { | |
| 416 result++; | |
| 417 } | |
| 418 } | |
| 419 { | |
| 420 var a = new List<num>(5); | |
| 421 List a0 = a; | |
| 422 List<Object> ao = a; | |
| 423 try { | |
| 424 List<int> ai = a; | |
| 425 } catch (TypeError error) { | |
| 426 result++; | |
| 427 } | |
| 428 List<num> an = a; | |
| 429 try { | |
| 430 List<String> as = a; | |
| 431 } catch (TypeError error) { | |
| 432 result++; | |
| 433 } | |
| 434 } | |
| 435 { | |
| 436 var a = new List<String>(5); | |
| 437 List a0 = a; | |
| 438 List<Object> ao = a; | |
| 439 try { | |
| 440 List<int> ai = a; | |
| 441 } catch (TypeError error) { | |
| 442 result++; | |
| 443 } | |
| 444 try { | |
| 445 List<num> an = a; | |
| 446 } catch (TypeError error) { | |
| 447 result++; | |
| 448 } | |
| 449 List<String> as = a; | |
| 450 } | |
| 451 return result; | |
| 452 } | |
| 453 | |
| 454 static testMain() { | |
| 455 Expect.equals(1, test()); | |
| 456 Expect.equals(1, testSideEffect()); | |
| 457 Expect.equals(1, testArgument()); | |
| 458 Expect.equals(1, testReturn()); | |
| 459 Expect.equals(1, testField()); | |
| 460 Expect.equals(1, testAnyFunction()); | |
| 461 Expect.equals(1, testVoidFunction()); | |
| 462 Expect.equals(1, testFunctionNum()); | |
| 463 Expect.equals(9, testBoolCheck()); | |
| 464 Expect.equals(1, testFactory()); | |
| 465 Expect.equals(8, testListAssigment()); | |
| 466 } | |
| 467 } | |
| 468 | |
| 469 | |
| 470 class C { | |
| 471 factory C() { | |
| 472 return 1; // Implicit result type is 'C', not int. | |
| 473 } | |
| 474 } | |
| 475 | |
| 476 | |
| 477 main() { | |
| 478 TypeTest.testMain(); | |
| 479 } | |
| OLD | NEW |