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