| 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 // VMOptions=--enable_type_checks --enable_asserts | 4 // VMOptions=--enable_type_checks --enable_asserts |
| 5 // | 5 // |
| 6 // Dart test program testing type checks. | 6 // Dart test program testing type checks. |
| 7 | 7 |
| 8 class TypeTest { | 8 class TypeTest { |
| 9 static test() { | 9 static test() { |
| 10 int result = 0; | 10 int result = 0; |
| 11 try { | 11 try { |
| 12 int i = "hello"; // Throws a TypeError if type checks are enabled. | 12 int i = "hello"; // Throws a TypeError if type checks are enabled. |
| 13 } catch (TypeError error) { | 13 } catch (TypeError error) { |
| 14 result = 1; | 14 result = 1; |
| 15 Expect.equals("int", error.dstType); | 15 Expect.equals("int", error.dstType); |
| 16 Expect.equals("String", error.srcType); | 16 Expect.equals("OneByteString", error.srcType); |
| 17 Expect.equals("i", error.dstName); | 17 Expect.equals("i", error.dstName); |
| 18 int pos = error.url.lastIndexOf("/", error.url.length); | 18 int pos = error.url.lastIndexOf("/", error.url.length); |
| 19 if (pos == -1) { | 19 if (pos == -1) { |
| 20 pos = error.url.lastIndexOf("\\", error.url.length); | 20 pos = error.url.lastIndexOf("\\", error.url.length); |
| 21 } | 21 } |
| 22 String subs = error.url.substring(pos + 1, error.url.length); | 22 String subs = error.url.substring(pos + 1, error.url.length); |
| 23 Expect.equals("type_vm_test.dart", subs); | 23 Expect.equals("type_vm_test.dart", subs); |
| 24 Expect.equals(12, error.line); | 24 Expect.equals(12, error.line); |
| 25 Expect.equals(15, error.column); | 25 Expect.equals(15, error.column); |
| 26 } | 26 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 47 static testArgument() { | 47 static testArgument() { |
| 48 int result = 0; | 48 int result = 0; |
| 49 int f(int i) { | 49 int f(int i) { |
| 50 return i; | 50 return i; |
| 51 } | 51 } |
| 52 try { | 52 try { |
| 53 int i = f("hello"); // Throws a TypeError if type checks are enabled. | 53 int i = f("hello"); // Throws a TypeError if type checks are enabled. |
| 54 } catch (TypeError error) { | 54 } catch (TypeError error) { |
| 55 result = 1; | 55 result = 1; |
| 56 Expect.equals("int", error.dstType); | 56 Expect.equals("int", error.dstType); |
| 57 Expect.equals("String", error.srcType); | 57 Expect.equals("OneByteString", error.srcType); |
| 58 Expect.equals("i", error.dstName); | 58 Expect.equals("i", error.dstName); |
| 59 int pos = error.url.lastIndexOf("/", error.url.length); | 59 int pos = error.url.lastIndexOf("/", error.url.length); |
| 60 if (pos == -1) { | 60 if (pos == -1) { |
| 61 pos = error.url.lastIndexOf("\\", error.url.length); | 61 pos = error.url.lastIndexOf("\\", error.url.length); |
| 62 } | 62 } |
| 63 String subs = error.url.substring(pos + 1, error.url.length); | 63 String subs = error.url.substring(pos + 1, error.url.length); |
| 64 Expect.equals("type_vm_test.dart", subs); | 64 Expect.equals("type_vm_test.dart", subs); |
| 65 Expect.equals(49, error.line); | 65 Expect.equals(49, error.line); |
| 66 Expect.equals(15, error.column); | 66 Expect.equals(15, error.column); |
| 67 } | 67 } |
| 68 return result; | 68 return result; |
| 69 } | 69 } |
| 70 | 70 |
| 71 static testReturn() { | 71 static testReturn() { |
| 72 int result = 0; | 72 int result = 0; |
| 73 int f(String s) { | 73 int f(String s) { |
| 74 return s; | 74 return s; |
| 75 } | 75 } |
| 76 try { | 76 try { |
| 77 int i = f("hello"); // Throws a TypeError if type checks are enabled. | 77 int i = f("hello"); // Throws a TypeError if type checks are enabled. |
| 78 } catch (TypeError error) { | 78 } catch (TypeError error) { |
| 79 result = 1; | 79 result = 1; |
| 80 Expect.equals("int", error.dstType); | 80 Expect.equals("int", error.dstType); |
| 81 Expect.equals("String", error.srcType); | 81 Expect.equals("OneByteString", error.srcType); |
| 82 Expect.equals("function result", error.dstName); | 82 Expect.equals("function result", error.dstName); |
| 83 int pos = error.url.lastIndexOf("/", error.url.length); | 83 int pos = error.url.lastIndexOf("/", error.url.length); |
| 84 if (pos == -1) { | 84 if (pos == -1) { |
| 85 pos = error.url.lastIndexOf("\\", error.url.length); | 85 pos = error.url.lastIndexOf("\\", error.url.length); |
| 86 } | 86 } |
| 87 String subs = error.url.substring(pos + 1, error.url.length); | 87 String subs = error.url.substring(pos + 1, error.url.length); |
| 88 Expect.equals("type_vm_test.dart", subs); | 88 Expect.equals("type_vm_test.dart", subs); |
| 89 Expect.equals(74, error.line); | 89 Expect.equals(74, error.line); |
| 90 Expect.equals(14, error.column); | 90 Expect.equals(14, error.column); |
| 91 } | 91 } |
| 92 return result; | 92 return result; |
| 93 } | 93 } |
| 94 | 94 |
| 95 static int field; | 95 static int field; |
| 96 static testField() { | 96 static testField() { |
| 97 int result = 0; | 97 int result = 0; |
| 98 try { | 98 try { |
| 99 field = "hello"; // Throws a TypeError if type checks are enabled. | 99 field = "hello"; // Throws a TypeError if type checks are enabled. |
| 100 } catch (TypeError error) { | 100 } catch (TypeError error) { |
| 101 result = 1; | 101 result = 1; |
| 102 Expect.equals("int", error.dstType); | 102 Expect.equals("int", error.dstType); |
| 103 Expect.equals("String", error.srcType); | 103 Expect.equals("OneByteString", error.srcType); |
| 104 Expect.equals("field", error.dstName); | 104 Expect.equals("field", error.dstName); |
| 105 int pos = error.url.lastIndexOf("/", error.url.length); | 105 int pos = error.url.lastIndexOf("/", error.url.length); |
| 106 if (pos == -1) { | 106 if (pos == -1) { |
| 107 pos = error.url.lastIndexOf("\\", error.url.length); | 107 pos = error.url.lastIndexOf("\\", error.url.length); |
| 108 } | 108 } |
| 109 String subs = error.url.substring(pos + 1, error.url.length); | 109 String subs = error.url.substring(pos + 1, error.url.length); |
| 110 Expect.equals("type_vm_test.dart", subs); | 110 Expect.equals("type_vm_test.dart", subs); |
| 111 Expect.equals(99, error.line); | 111 Expect.equals(99, error.line); |
| 112 Expect.equals(15, error.column); | 112 Expect.equals(15, error.column); |
| 113 } | 113 } |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 return result; | 203 return result; |
| 204 } | 204 } |
| 205 | 205 |
| 206 static testBoolCheck() { | 206 static testBoolCheck() { |
| 207 int result = 0; | 207 int result = 0; |
| 208 try { | 208 try { |
| 209 bool i = !"hello"; // Throws a TypeError if type checks are enabled. | 209 bool i = !"hello"; // Throws a TypeError if type checks are enabled. |
| 210 } catch (TypeError error) { | 210 } catch (TypeError error) { |
| 211 result++; | 211 result++; |
| 212 Expect.equals("bool", error.dstType); | 212 Expect.equals("bool", error.dstType); |
| 213 Expect.equals("String", error.srcType); | 213 Expect.equals("OneByteString", error.srcType); |
| 214 Expect.equals("boolean expression", error.dstName); | 214 Expect.equals("boolean expression", error.dstName); |
| 215 int pos = error.url.lastIndexOf("/", error.url.length); | 215 int pos = error.url.lastIndexOf("/", error.url.length); |
| 216 if (pos == -1) { | 216 if (pos == -1) { |
| 217 pos = error.url.lastIndexOf("\\", error.url.length); | 217 pos = error.url.lastIndexOf("\\", error.url.length); |
| 218 } | 218 } |
| 219 String subs = error.url.substring(pos + 1, error.url.length); | 219 String subs = error.url.substring(pos + 1, error.url.length); |
| 220 Expect.equals("type_vm_test.dart", subs); | 220 Expect.equals("type_vm_test.dart", subs); |
| 221 Expect.equals(209, error.line); | 221 Expect.equals(209, error.line); |
| 222 Expect.equals(17, error.column); | 222 Expect.equals(17, error.column); |
| 223 } | 223 } |
| 224 try { | 224 try { |
| 225 while ("hello") {}; // Throws a TypeError if type checks are enabled. | 225 while ("hello") {}; // Throws a TypeError if type checks are enabled. |
| 226 } catch (TypeError error) { | 226 } catch (TypeError error) { |
| 227 result++; | 227 result++; |
| 228 Expect.equals("bool", error.dstType); | 228 Expect.equals("bool", error.dstType); |
| 229 Expect.equals("String", error.srcType); | 229 Expect.equals("OneByteString", error.srcType); |
| 230 Expect.equals("boolean expression", error.dstName); | 230 Expect.equals("boolean expression", error.dstName); |
| 231 int pos = error.url.lastIndexOf("/", error.url.length); | 231 int pos = error.url.lastIndexOf("/", error.url.length); |
| 232 if (pos == -1) { | 232 if (pos == -1) { |
| 233 pos = error.url.lastIndexOf("\\", error.url.length); | 233 pos = error.url.lastIndexOf("\\", error.url.length); |
| 234 } | 234 } |
| 235 String subs = error.url.substring(pos + 1, error.url.length); | 235 String subs = error.url.substring(pos + 1, error.url.length); |
| 236 Expect.equals("type_vm_test.dart", subs); | 236 Expect.equals("type_vm_test.dart", subs); |
| 237 Expect.equals(225, error.line); | 237 Expect.equals(225, error.line); |
| 238 Expect.equals(14, error.column); | 238 Expect.equals(14, error.column); |
| 239 } | 239 } |
| 240 try { | 240 try { |
| 241 do {} while ("hello"); // Throws a TypeError if type checks are enabled. | 241 do {} while ("hello"); // Throws a TypeError if type checks are enabled. |
| 242 } catch (TypeError error) { | 242 } catch (TypeError error) { |
| 243 result++; | 243 result++; |
| 244 Expect.equals("bool", error.dstType); | 244 Expect.equals("bool", error.dstType); |
| 245 Expect.equals("String", error.srcType); | 245 Expect.equals("OneByteString", error.srcType); |
| 246 Expect.equals("boolean expression", error.dstName); | 246 Expect.equals("boolean expression", error.dstName); |
| 247 int pos = error.url.lastIndexOf("/", error.url.length); | 247 int pos = error.url.lastIndexOf("/", error.url.length); |
| 248 if (pos == -1) { | 248 if (pos == -1) { |
| 249 pos = error.url.lastIndexOf("\\", error.url.length); | 249 pos = error.url.lastIndexOf("\\", error.url.length); |
| 250 } | 250 } |
| 251 String subs = error.url.substring(pos + 1, error.url.length); | 251 String subs = error.url.substring(pos + 1, error.url.length); |
| 252 Expect.equals("type_vm_test.dart", subs); | 252 Expect.equals("type_vm_test.dart", subs); |
| 253 Expect.equals(241, error.line); | 253 Expect.equals(241, error.line); |
| 254 Expect.equals(20, error.column); | 254 Expect.equals(20, error.column); |
| 255 } | 255 } |
| 256 try { | 256 try { |
| 257 for (;"hello";) {}; // Throws a TypeError if type checks are enabled. | 257 for (;"hello";) {}; // Throws a TypeError if type checks are enabled. |
| 258 } catch (TypeError error) { | 258 } catch (TypeError error) { |
| 259 result++; | 259 result++; |
| 260 Expect.equals("bool", error.dstType); | 260 Expect.equals("bool", error.dstType); |
| 261 Expect.equals("String", error.srcType); | 261 Expect.equals("OneByteString", error.srcType); |
| 262 Expect.equals("boolean expression", error.dstName); | 262 Expect.equals("boolean expression", error.dstName); |
| 263 int pos = error.url.lastIndexOf("/", error.url.length); | 263 int pos = error.url.lastIndexOf("/", error.url.length); |
| 264 if (pos == -1) { | 264 if (pos == -1) { |
| 265 pos = error.url.lastIndexOf("\\", error.url.length); | 265 pos = error.url.lastIndexOf("\\", error.url.length); |
| 266 } | 266 } |
| 267 String subs = error.url.substring(pos + 1, error.url.length); | 267 String subs = error.url.substring(pos + 1, error.url.length); |
| 268 Expect.equals("type_vm_test.dart", subs); | 268 Expect.equals("type_vm_test.dart", subs); |
| 269 Expect.equals(257, error.line); | 269 Expect.equals(257, error.line); |
| 270 Expect.equals(13, error.column); | 270 Expect.equals(13, error.column); |
| 271 } | 271 } |
| 272 try { | 272 try { |
| 273 int i = "hello" ? 1 : 0; // Throws a TypeError if type checks are enabled
. | 273 int i = "hello" ? 1 : 0; // Throws a TypeError if type checks are enabled
. |
| 274 } catch (TypeError error) { | 274 } catch (TypeError error) { |
| 275 result++; | 275 result++; |
| 276 Expect.equals("bool", error.dstType); | 276 Expect.equals("bool", error.dstType); |
| 277 Expect.equals("String", error.srcType); | 277 Expect.equals("OneByteString", error.srcType); |
| 278 Expect.equals("boolean expression", error.dstName); | 278 Expect.equals("boolean expression", error.dstName); |
| 279 int pos = error.url.lastIndexOf("/", error.url.length); | 279 int pos = error.url.lastIndexOf("/", error.url.length); |
| 280 if (pos == -1) { | 280 if (pos == -1) { |
| 281 pos = error.url.lastIndexOf("\\", error.url.length); | 281 pos = error.url.lastIndexOf("\\", error.url.length); |
| 282 } | 282 } |
| 283 String subs = error.url.substring(pos + 1, error.url.length); | 283 String subs = error.url.substring(pos + 1, error.url.length); |
| 284 Expect.equals("type_vm_test.dart", subs); | 284 Expect.equals("type_vm_test.dart", subs); |
| 285 Expect.equals(273, error.line); | 285 Expect.equals(273, error.line); |
| 286 Expect.equals(15, error.column); | 286 Expect.equals(15, error.column); |
| 287 } | 287 } |
| 288 try { | 288 try { |
| 289 if ("hello") {}; // Throws a TypeError if type checks are enabled. | 289 if ("hello") {}; // Throws a TypeError if type checks are enabled. |
| 290 } catch (TypeError error) { | 290 } catch (TypeError error) { |
| 291 result++; | 291 result++; |
| 292 Expect.equals("bool", error.dstType); | 292 Expect.equals("bool", error.dstType); |
| 293 Expect.equals("String", error.srcType); | 293 Expect.equals("OneByteString", error.srcType); |
| 294 Expect.equals("boolean expression", error.dstName); | 294 Expect.equals("boolean expression", error.dstName); |
| 295 int pos = error.url.lastIndexOf("/", error.url.length); | 295 int pos = error.url.lastIndexOf("/", error.url.length); |
| 296 if (pos == -1) { | 296 if (pos == -1) { |
| 297 pos = error.url.lastIndexOf("\\", error.url.length); | 297 pos = error.url.lastIndexOf("\\", error.url.length); |
| 298 } | 298 } |
| 299 String subs = error.url.substring(pos + 1, error.url.length); | 299 String subs = error.url.substring(pos + 1, error.url.length); |
| 300 Expect.equals("type_vm_test.dart", subs); | 300 Expect.equals("type_vm_test.dart", subs); |
| 301 Expect.equals(289, error.line); | 301 Expect.equals(289, error.line); |
| 302 Expect.equals(11, error.column); | 302 Expect.equals(11, error.column); |
| 303 } | 303 } |
| 304 try { | 304 try { |
| 305 if ("hello" || false) {}; // Throws a TypeError if type checks are enable
d. | 305 if ("hello" || false) {}; // Throws a TypeError if type checks are enable
d. |
| 306 } catch (TypeError error) { | 306 } catch (TypeError error) { |
| 307 result++; | 307 result++; |
| 308 Expect.equals("bool", error.dstType); | 308 Expect.equals("bool", error.dstType); |
| 309 Expect.equals("String", error.srcType); | 309 Expect.equals("OneByteString", error.srcType); |
| 310 Expect.equals("boolean expression", error.dstName); | 310 Expect.equals("boolean expression", error.dstName); |
| 311 int pos = error.url.lastIndexOf("/", error.url.length); | 311 int pos = error.url.lastIndexOf("/", error.url.length); |
| 312 if (pos == -1) { | 312 if (pos == -1) { |
| 313 pos = error.url.lastIndexOf("\\", error.url.length); | 313 pos = error.url.lastIndexOf("\\", error.url.length); |
| 314 } | 314 } |
| 315 String subs = error.url.substring(pos + 1, error.url.length); | 315 String subs = error.url.substring(pos + 1, error.url.length); |
| 316 Expect.equals("type_vm_test.dart", subs); | 316 Expect.equals("type_vm_test.dart", subs); |
| 317 Expect.equals(305, error.line); | 317 Expect.equals(305, error.line); |
| 318 Expect.equals(11, error.column); | 318 Expect.equals(11, error.column); |
| 319 } | 319 } |
| 320 try { | 320 try { |
| 321 if (false || "hello") {}; // Throws a TypeError if type checks are enable
d. | 321 if (false || "hello") {}; // Throws a TypeError if type checks are enable
d. |
| 322 } catch (TypeError error) { | 322 } catch (TypeError error) { |
| 323 result++; | 323 result++; |
| 324 Expect.equals("bool", error.dstType); | 324 Expect.equals("bool", error.dstType); |
| 325 Expect.equals("String", error.srcType); | 325 Expect.equals("OneByteString", error.srcType); |
| 326 Expect.equals("boolean expression", error.dstName); | 326 Expect.equals("boolean expression", error.dstName); |
| 327 int pos = error.url.lastIndexOf("/", error.url.length); | 327 int pos = error.url.lastIndexOf("/", error.url.length); |
| 328 if (pos == -1) { | 328 if (pos == -1) { |
| 329 pos = error.url.lastIndexOf("\\", error.url.length); | 329 pos = error.url.lastIndexOf("\\", error.url.length); |
| 330 } | 330 } |
| 331 String subs = error.url.substring(pos + 1, error.url.length); | 331 String subs = error.url.substring(pos + 1, error.url.length); |
| 332 Expect.equals("type_vm_test.dart", subs); | 332 Expect.equals("type_vm_test.dart", subs); |
| 333 Expect.equals(321, error.line); | 333 Expect.equals(321, error.line); |
| 334 Expect.equals(20, error.column); | 334 Expect.equals(20, error.column); |
| 335 } | 335 } |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 470 class C { | 470 class C { |
| 471 factory C() { | 471 factory C() { |
| 472 return 1; // Implicit result type is 'C', not int. | 472 return 1; // Implicit result type is 'C', not int. |
| 473 } | 473 } |
| 474 } | 474 } |
| 475 | 475 |
| 476 | 476 |
| 477 main() { | 477 main() { |
| 478 TypeTest.testMain(); | 478 TypeTest.testMain(); |
| 479 } | 479 } |
| OLD | NEW |