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. | |
6 | |
7 final TOP_LEVEL_CONST = 1; | |
8 final TOP_LEVEL_CONST_REF = TOP_LEVEL_CONST; | |
9 final TOP_LEVEL_NULL = null; | |
10 | |
11 var topLevel; | |
12 | |
13 class CallThroughGetterTest { | |
14 | |
15 static void testMain() { | |
16 testTopLevel(); | |
17 testField(); | |
18 testGetter(); | |
19 testMethod(); | |
20 testEvaluationOrder(); | |
21 } | |
22 | |
23 static void testTopLevel() { | |
24 topLevel = function() { | |
25 return 2; | |
26 }; | |
27 Expect.equals(1, TOP_LEVEL_CONST); | |
28 Expect.equals(1, TOP_LEVEL_CONST_REF); | |
29 Expect.equals(2, topLevel()); | |
30 | |
31 expectThrowsNotClosure(() { | |
32 TOP_LEVEL_CONST(); /// static type warning | |
33 }); | |
34 expectThrowsNotClosure(() { | |
35 (TOP_LEVEL_CONST)(); /// static type warning | |
36 }); | |
37 } | |
38 | |
39 static void testField() { | |
40 A a = new A(); | |
41 a.field = () => 42; | |
42 Expect.equals(42, a.field()); | |
43 Expect.equals(42, (a.field)()); | |
44 | |
45 a.field = () => 87; | |
46 Expect.equals(87, a.field()); | |
47 Expect.equals(87, (a.field)()); | |
48 | |
49 a.field = 99; | |
50 expectThrowsNotClosure(() { a.field(); }); | |
51 expectThrowsNotClosure(() { (a.field)(); }); | |
52 } | |
53 | |
54 static void testGetter() { | |
55 A a = new A(); | |
56 a.field = () => 42; | |
57 Expect.equals(42, a.getter()); | |
58 Expect.equals(42, (a.getter)()); | |
59 | |
60 a.field = () => 87; | |
61 Expect.equals(87, a.getter()); | |
62 Expect.equals(87, (a.getter)()); | |
63 | |
64 a.field = 99; | |
65 expectThrowsNotClosure(() { a.getter(); }); | |
66 expectThrowsNotClosure(() { (a.getter)(); }); | |
67 } | |
68 | |
69 static void testMethod() { | |
70 A a = new A(); | |
71 a.field = () => 42; | |
72 Expect.equals(true, a.method() is Function); | |
73 Expect.equals(42, a.method()()); | |
74 | |
75 a.field = () => 87; | |
76 Expect.equals(true, a.method() is Function); | |
77 Expect.equals(87, a.method()()); | |
78 | |
79 a.field = null; | |
80 Expect.equals(null, a.method()); | |
81 } | |
82 | |
83 static void testEvaluationOrder() { | |
84 B b = new B(); | |
85 Expect.equals("gf", b.g0()); | |
86 b = new B(); | |
87 Expect.equals("gf", (b.g0)()); | |
88 | |
89 b = new B(); | |
90 Expect.equals("xgf", b.g1(b.x)); | |
91 b = new B(); | |
92 Expect.equals("gxf", (b.g1)(b.x)); | |
93 | |
94 b = new B(); | |
95 Expect.equals("xygf", b.g2(b.x, b.y)); | |
96 b = new B(); | |
97 Expect.equals("gxyf", (b.g2)(b.x, b.y)); | |
98 | |
99 b = new B(); | |
100 Expect.equals("xyzgf", b.g3(b.x, b.y, b.z)); | |
101 b = new B(); | |
102 Expect.equals("gxyzf", (b.g3)(b.x, b.y, b.z)); | |
103 | |
104 b = new B(); | |
105 Expect.equals("yzxgf", b.g3(b.y, b.z, b.x)); | |
106 b = new B(); | |
107 Expect.equals("gyzxf", (b.g3)(b.y, b.z, b.x)); | |
108 } | |
109 | |
110 static void expectThrowsNotClosure(fn) { | |
111 var exception = catchException(fn); | |
112 if (!(exception is ObjectNotClosureException)) { | |
113 Expect.fail("Wrong exception. Expected: ObjectNotClosureException" | |
114 " got: ${exception}"); | |
115 } | |
116 } | |
117 | |
118 static catchException(fn) { | |
119 bool caught = false; | |
120 var result = null; | |
121 try { | |
122 fn(); | |
123 Expect.equals(true, false); // Shouldn't reach this. | |
124 } catch (var e) { | |
125 caught = true; | |
126 result = e; | |
127 } | |
128 Expect.equals(true, caught); | |
129 return result; | |
130 } | |
131 | |
132 } | |
133 | |
134 | |
135 class A { | |
136 | |
137 A() { } | |
138 var field; | |
139 get getter() { return field; } | |
140 method() { return field; } | |
141 | |
142 } | |
143 | |
144 | |
145 class B { | |
146 | |
147 B() : _order = new StringBuffer("") { } | |
148 | |
149 get g0() { _mark('g'); return f0() { return _mark('f'); }; } | |
150 get g1() { _mark('g'); return f1(x) { return _mark('f'); }; } | |
151 get g2() { _mark('g'); return f2(x, y) { return _mark('f'); }; } | |
152 get g3() { _mark('g'); return f3(x, y, z) { return _mark('f'); }; } | |
153 | |
154 get x() { _mark('x'); return 0; } | |
155 get y() { _mark('y'); return 1; } | |
156 get z() { _mark('z'); return 2; } | |
157 | |
158 _mark(m) { _order.add(m); return _order.toString(); } | |
159 StringBuffer _order; | |
160 | |
161 } | |
162 | |
163 main() { | |
164 CallThroughGetterTest.testMain(); | |
165 } | |
OLD | NEW |