| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 // Dart test for type checks involving the void type. | 4 // Dart test for type checks involving the void type. |
| 5 | 5 |
| 6 import "package:expect/expect.dart"; | 6 import "package:expect/expect.dart"; |
| 7 | 7 |
| 8 isCheckedMode() { | 8 isCheckedMode() { |
| 9 try { | 9 try { |
| 10 var i = 1; | 10 var i = 1; |
| 11 String s = i; | 11 String s = i; |
| 12 return false; | 12 return false; |
| 13 } catch (e) { | 13 } catch (e) { |
| 14 return true; | 14 return true; |
| 15 } | 15 } |
| 16 } | 16 } |
| 17 | 17 |
| 18 void f() { return; } | 18 void f() { |
| 19 return; |
| 20 } |
| 19 | 21 |
| 20 void f_null() { return null; } | 22 void f_null() { |
| 23 return null; |
| 24 } |
| 21 | 25 |
| 22 void f_1() { return 1; } | 26 void f_1() { |
| 27 return 1; |
| 28 } |
| 23 | 29 |
| 24 void f_dyn_null() { | 30 void f_dyn_null() { |
| 25 var x = null; | 31 var x = null; |
| 26 return x; | 32 return x; |
| 27 } | 33 } |
| 28 | 34 |
| 29 void f_dyn_1() { | 35 void f_dyn_1() { |
| 30 var x = 1; | 36 var x = 1; |
| 31 return x; | 37 return x; |
| 32 } | 38 } |
| 33 | 39 |
| 34 void f_f() { return f(); } | 40 void f_f() { |
| 41 return f(); |
| 42 } |
| 35 | 43 |
| 36 void test(int n, void func(), bool must_get_error) { | 44 void test(int n, void func(), bool must_get_error) { |
| 37 // Test as closure call. | 45 // Test as closure call. |
| 38 { | 46 { |
| 39 bool got_type_error = false; | 47 bool got_type_error = false; |
| 40 try { | 48 try { |
| 41 var x = func(); | 49 var x = func(); |
| 42 } on TypeError catch (error) { | 50 } on TypeError catch (error) { |
| 43 got_type_error = true; | 51 got_type_error = true; |
| 44 } | 52 } |
| 45 // Never a type error in production mode. | 53 // Never a type error in production mode. |
| 46 if (isCheckedMode()) { | 54 if (isCheckedMode()) { |
| 47 Expect.isTrue(got_type_error == must_get_error); | 55 Expect.isTrue(got_type_error == must_get_error); |
| 48 } else { | 56 } else { |
| 49 Expect.isFalse(got_type_error); | 57 Expect.isFalse(got_type_error); |
| 50 } | 58 } |
| 51 } | 59 } |
| 52 // Test as direct call. | 60 // Test as direct call. |
| 53 { | 61 { |
| 54 bool got_type_error = false; | 62 bool got_type_error = false; |
| 55 try { | 63 try { |
| 56 var x; | 64 var x; |
| 57 switch (n) { | 65 switch (n) { |
| 58 case 0: x = f(); break; | 66 case 0: |
| 59 case 1: x = f_null(); break; | 67 x = f(); |
| 60 case 2: x = f_1(); break; | 68 break; |
| 61 case 3: x = f_dyn_null(); break; | 69 case 1: |
| 62 case 4: x = f_dyn_1(); break; | 70 x = f_null(); |
| 63 case 5: x = f_f(); break; | 71 break; |
| 72 case 2: |
| 73 x = f_1(); |
| 74 break; |
| 75 case 3: |
| 76 x = f_dyn_null(); |
| 77 break; |
| 78 case 4: |
| 79 x = f_dyn_1(); |
| 80 break; |
| 81 case 5: |
| 82 x = f_f(); |
| 83 break; |
| 64 } | 84 } |
| 65 } on TypeError catch (error) { | 85 } on TypeError catch (error) { |
| 66 got_type_error = true; | 86 got_type_error = true; |
| 67 } | 87 } |
| 68 // Never a type error in production mode. | 88 // Never a type error in production mode. |
| 69 if (isCheckedMode()) { | 89 if (isCheckedMode()) { |
| 70 Expect.isTrue(got_type_error == must_get_error); | 90 Expect.isTrue(got_type_error == must_get_error); |
| 71 } else { | 91 } else { |
| 72 Expect.isFalse(got_type_error); | 92 Expect.isFalse(got_type_error); |
| 73 } | 93 } |
| 74 } | 94 } |
| 75 } | 95 } |
| 76 | 96 |
| 77 main() { | 97 main() { |
| 78 test(0, f, false); | 98 test(0, f, false); |
| 79 test(1, f_null, false); | 99 test(1, f_null, false); |
| 80 test(2, f_1, true); | 100 test(2, f_1, true); |
| 81 test(3, f_dyn_null, false); | 101 test(3, f_dyn_null, false); |
| 82 test(4, f_dyn_1, true); | 102 test(4, f_dyn_1, true); |
| 83 test(5, f_f, false); | 103 test(5, f_f, false); |
| 84 } | 104 } |
| 85 | |
| 86 | |
| OLD | NEW |