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