| 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 | 4 |
| 5 // Tests that we can call functions through getters which return null. | 5 // Tests that we can call functions through getters which return null. |
| 6 | 6 |
| 7 final TOP_LEVEL_NULL = null; | 7 final TOP_LEVEL_NULL = null; |
| 8 | 8 |
| 9 var topLevel; | 9 var topLevel; |
| 10 | 10 |
| 11 class CallThroughNullGetterTest { | 11 class CallThroughNullGetterTest { |
| 12 | 12 |
| 13 static void testMain() { | 13 static void testMain() { |
| 14 testTopLevel(); | 14 testTopLevel(); |
| 15 testField(); | 15 testField(); |
| 16 testGetter(); | 16 testGetter(); |
| 17 testMethod(); | 17 testMethod(); |
| 18 } | 18 } |
| 19 | 19 |
| 20 static void testTopLevel() { | 20 static void testTopLevel() { |
| 21 topLevel = null; | 21 topLevel = null; |
| 22 expectThrowsNullPointerException(() { topLevel(); }); | 22 expectThrowsObjectNotClosureException(() { topLevel(); }); |
| 23 expectThrowsNullPointerException(() { (topLevel)(); }); | 23 expectThrowsObjectNotClosureException(() { (topLevel)(); }); |
| 24 expectThrowsNullPointerException(() { TOP_LEVEL_NULL(); }); | 24 expectThrowsObjectNotClosureException(() { TOP_LEVEL_NULL(); }); |
| 25 expectThrowsNullPointerException(() { (TOP_LEVEL_NULL)(); }); | 25 expectThrowsObjectNotClosureException(() { (TOP_LEVEL_NULL)(); }); |
| 26 } | 26 } |
| 27 | 27 |
| 28 static void testField() { | 28 static void testField() { |
| 29 A a = new A(); | 29 A a = new A(); |
| 30 | 30 |
| 31 a.field = null; | 31 a.field = null; |
| 32 expectThrowsNullPointerException(() { a.field(); }); | 32 expectThrowsObjectNotClosureException(() { a.field(); }); |
| 33 expectThrowsNullPointerException(() { (a.field)(); }); | 33 expectThrowsObjectNotClosureException(() { (a.field)(); }); |
| 34 } | 34 } |
| 35 | 35 |
| 36 static void testGetter() { | 36 static void testGetter() { |
| 37 A a = new A(); | 37 A a = new A(); |
| 38 | 38 |
| 39 a.field = null; | 39 a.field = null; |
| 40 expectThrowsNullPointerException(() { a.getter(); }); | 40 expectThrowsObjectNotClosureException(() { a.getter(); }); |
| 41 expectThrowsNullPointerException(() { (a.getter)(); }); | 41 expectThrowsObjectNotClosureException(() { (a.getter)(); }); |
| 42 } | 42 } |
| 43 | 43 |
| 44 static void testMethod() { | 44 static void testMethod() { |
| 45 A a = new A(); | 45 A a = new A(); |
| 46 | 46 |
| 47 a.field = null; | 47 a.field = null; |
| 48 expectThrowsNullPointerException(() { a.method()(); }); | 48 expectThrowsObjectNotClosureException(() { a.method()(); }); |
| 49 } | 49 } |
| 50 | 50 |
| 51 static void expectThrowsNullPointerException(fn) { | 51 static void expectThrowsNullPointerException(fn) { |
| 52 var exception = catchException(fn); | 52 var exception = catchException(fn); |
| 53 if (!(exception is NullPointerException)) { | 53 if (!(exception is NullPointerException)) { |
| 54 Expect.fail("Wrong exception. Expected: NullPointerException" | 54 Expect.fail("Wrong exception. Expected: NullPointerException" |
| 55 + " got: ${exception}"); | 55 + " got: ${exception}"); |
| 56 } | 56 } |
| 57 } | 57 } |
| 58 | 58 |
| 59 static void expectThrowsObjectNotClosureException(fn) { |
| 60 var exception = catchException(fn); |
| 61 if (!(exception is ObjectNotClosureException)) { |
| 62 Expect.fail("Wrong exception. Expected: ObjectNotClosureException" |
| 63 + " got: ${exception}"); |
| 64 } |
| 65 } |
| 66 |
| 59 static catchException(fn) { | 67 static catchException(fn) { |
| 60 bool caught = false; | 68 bool caught = false; |
| 61 var result = null; | 69 var result = null; |
| 62 try { | 70 try { |
| 63 fn(); | 71 fn(); |
| 64 Expect.equals(true, false); // Shouldn't reach this. | 72 Expect.equals(true, false); // Shouldn't reach this. |
| 65 } catch (var e) { | 73 } catch (var e) { |
| 66 caught = true; | 74 caught = true; |
| 67 result = e; | 75 result = e; |
| 68 } | 76 } |
| 69 Expect.equals(true, caught); | 77 Expect.equals(true, caught); |
| 70 return result; | 78 return result; |
| 71 } | 79 } |
| 72 | 80 |
| 73 } | 81 } |
| 74 | 82 |
| 75 | 83 |
| 76 class A { | 84 class A { |
| 77 | 85 |
| 78 A() { } | 86 A() { } |
| 79 var field; | 87 var field; |
| 80 get getter() { return field; } | 88 get getter() { return field; } |
| 81 method() { return field; } | 89 method() { return field; } |
| 82 | 90 |
| 83 } | 91 } |
| 84 | 92 |
| 85 main() { | 93 main() { |
| 86 CallThroughNullGetterTest.testMain(); | 94 CallThroughNullGetterTest.testMain(); |
| 87 } | 95 } |
| OLD | NEW |