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 const TOP_LEVEL_NULL = null; | 7 const 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 expectThrowsNoSuchMethodError(() { topLevel(); }); | 22 expectThrowsNullPointerException(() { topLevel(); }); |
23 expectThrowsNoSuchMethodError(() { (topLevel)(); }); | 23 expectThrowsNullPointerException(() { (topLevel)(); }); |
24 expectThrowsNoSuchMethodError(() { TOP_LEVEL_NULL(); }); | 24 expectThrowsNullPointerException(() { TOP_LEVEL_NULL(); }); |
25 expectThrowsNoSuchMethodError(() { (TOP_LEVEL_NULL)(); }); | 25 expectThrowsNullPointerException(() { (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 expectThrowsNoSuchMethodError(() { a.field(); }); | 32 expectThrowsNullPointerException(() { a.field(); }); |
33 expectThrowsNoSuchMethodError(() { (a.field)(); }); | 33 expectThrowsNullPointerException(() { (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 expectThrowsNoSuchMethodError(() { a.getter(); }); | 40 expectThrowsNullPointerException(() { a.getter(); }); |
41 expectThrowsNoSuchMethodError(() { (a.getter)(); }); | 41 expectThrowsNullPointerException(() { (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 expectThrowsNoSuchMethodError(() { a.method()(); }); | 48 expectThrowsNullPointerException(() { a.method()(); }); |
49 } | 49 } |
50 | 50 |
51 static void expectThrowsNoSuchMethodError(fn) { | 51 static void expectThrowsNullPointerException(fn) { |
52 Expect.throws(fn, (e) => e is NoSuchMethodError, | 52 var exception = catchException(fn); |
53 "Should throw NoSuchMethodError"); | 53 if (exception is! NullPointerException) { |
| 54 Expect.fail("Wrong exception. Expected: NullPointerException" |
| 55 " got: ${exception}"); |
| 56 } |
54 } | 57 } |
| 58 |
| 59 static catchException(fn) { |
| 60 bool caught = false; |
| 61 var result = null; |
| 62 try { |
| 63 fn(); |
| 64 Expect.equals(true, false); // Shouldn't reach this. |
| 65 } catch (e) { |
| 66 caught = true; |
| 67 result = e; |
| 68 } |
| 69 Expect.equals(true, caught); |
| 70 return result; |
| 71 } |
| 72 |
55 } | 73 } |
56 | 74 |
57 | 75 |
58 class A { | 76 class A { |
59 | 77 |
60 A() { } | 78 A() { } |
61 var field; | 79 var field; |
62 get getter { return field; } | 80 get getter { return field; } |
63 method() { return field; } | 81 method() { return field; } |
64 | 82 |
65 } | 83 } |
66 | 84 |
67 main() { | 85 main() { |
68 CallThroughNullGetterTest.testMain(); | 86 CallThroughNullGetterTest.testMain(); |
69 } | 87 } |
OLD | NEW |