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 import "package:expect/expect.dart"; | |
6 | |
7 // Tests that we can call functions through getters. | |
8 | |
9 const TOP_LEVEL_CONST = 1; | |
10 const TOP_LEVEL_CONST_REF = TOP_LEVEL_CONST; | |
11 const TOP_LEVEL_NULL = null; | |
12 | |
13 var topLevel; | |
14 | |
15 class CallThroughGetterTest { | |
16 static void testMain() { | |
17 testTopLevel(); | |
18 testField(); | |
19 testGetter(); | |
20 testMethod(); | |
21 testEvaluationOrder(); | |
22 } | |
23 | |
24 static void testTopLevel() { | |
25 topLevel = () { | |
26 return 2; | |
27 }; | |
28 Expect.equals(1, TOP_LEVEL_CONST); | |
29 Expect.equals(1, TOP_LEVEL_CONST_REF); | |
30 Expect.equals(2, topLevel()); | |
31 | |
32 expectThrowsNoSuchMethod(() { | |
33 TOP_LEVEL_CONST(); //# static type warning | |
34 }); | |
35 expectThrowsNoSuchMethod(() { | |
36 (TOP_LEVEL_CONST)(); // //# static type warning | |
37 }); | |
38 } | |
39 | |
40 static void testField() { | |
41 A a = new A(); | |
42 a.field = () => 42; | |
43 Expect.equals(42, a.field()); | |
44 Expect.equals(42, (a.field)()); | |
45 | |
46 a.field = () => 87; | |
47 Expect.equals(87, a.field()); | |
48 Expect.equals(87, (a.field)()); | |
49 | |
50 a.field = 99; | |
51 expectThrowsNoSuchMethod(() { | |
52 a.field(); | |
53 }); | |
54 expectThrowsNoSuchMethod(() { | |
55 (a.field)(); | |
56 }); | |
57 } | |
58 | |
59 static void testGetter() { | |
60 A a = new A(); | |
61 a.field = () => 42; | |
62 Expect.equals(42, a.getter()); | |
63 Expect.equals(42, (a.getter)()); | |
64 | |
65 a.field = () => 87; | |
66 Expect.equals(87, a.getter()); | |
67 Expect.equals(87, (a.getter)()); | |
68 | |
69 a.field = 99; | |
70 expectThrowsNoSuchMethod(() { | |
71 a.getter(); | |
72 }); | |
73 expectThrowsNoSuchMethod(() { | |
74 (a.getter)(); | |
75 }); | |
76 } | |
77 | |
78 static void testMethod() { | |
79 A a = new A(); | |
80 a.field = () => 42; | |
81 Expect.equals(true, a.method() is Function); | |
82 Expect.equals(42, a.method()()); | |
83 | |
84 a.field = () => 87; | |
85 Expect.equals(true, a.method() is Function); | |
86 Expect.equals(87, a.method()()); | |
87 | |
88 a.field = null; | |
89 Expect.equals(null, a.method()); | |
90 } | |
91 | |
92 static void testEvaluationOrder() { | |
93 B b = new B(); | |
94 Expect.equals("gf", b.g0()); | |
95 b = new B(); | |
96 Expect.equals("gf", (b.g0)()); | |
97 | |
98 b = new B(); | |
99 Expect.equals("xgf", b.g1(b.x)); | |
100 b = new B(); | |
101 Expect.equals("gxf", (b.g1)(b.x)); | |
102 | |
103 b = new B(); | |
104 Expect.equals("xygf", b.g2(b.x, b.y)); | |
105 b = new B(); | |
106 Expect.equals("gxyf", (b.g2)(b.x, b.y)); | |
107 | |
108 b = new B(); | |
109 Expect.equals("xyzgf", b.g3(b.x, b.y, b.z)); | |
110 b = new B(); | |
111 Expect.equals("gxyzf", (b.g3)(b.x, b.y, b.z)); | |
112 | |
113 b = new B(); | |
114 Expect.equals("yzxgf", b.g3(b.y, b.z, b.x)); | |
115 b = new B(); | |
116 Expect.equals("gyzxf", (b.g3)(b.y, b.z, b.x)); | |
117 } | |
118 | |
119 static expectThrowsNoSuchMethod(fn) { | |
120 var exception = catchException(fn); | |
121 if (exception is! NoSuchMethodError) { | |
122 Expect.fail("Wrong exception. Expected: NoSuchMethodError" | |
123 " got: ${exception}"); | |
124 } | |
125 } | |
126 | |
127 static catchException(fn) { | |
128 bool caught = false; | |
129 var result = null; | |
130 try { | |
131 fn(); | |
132 Expect.equals(true, false); // Shouldn't reach this. | |
133 } catch (e) { | |
134 caught = true; | |
135 result = e; | |
136 } | |
137 Expect.equals(true, caught); | |
138 return result; | |
139 } | |
140 } | |
141 | |
142 class A { | |
143 A() {} | |
144 var field; | |
145 get getter { | |
146 return field; | |
147 } | |
148 | |
149 method() { | |
150 return field; | |
151 } | |
152 } | |
153 | |
154 class B { | |
155 B() : _order = new StringBuffer("") {} | |
156 | |
157 get g0 { | |
158 _mark('g'); | |
159 return () { | |
160 return _mark('f'); | |
161 }; | |
162 } | |
163 | |
164 get g1 { | |
165 _mark('g'); | |
166 return (x) { | |
167 return _mark('f'); | |
168 }; | |
169 } | |
170 | |
171 get g2 { | |
172 _mark('g'); | |
173 return (x, y) { | |
174 return _mark('f'); | |
175 }; | |
176 } | |
177 | |
178 get g3 { | |
179 _mark('g'); | |
180 return (x, y, z) { | |
181 return _mark('f'); | |
182 }; | |
183 } | |
184 | |
185 get x { | |
186 _mark('x'); | |
187 return 0; | |
188 } | |
189 | |
190 get y { | |
191 _mark('y'); | |
192 return 1; | |
193 } | |
194 | |
195 get z { | |
196 _mark('z'); | |
197 return 2; | |
198 } | |
199 | |
200 _mark(m) { | |
201 _order.write(m); | |
202 return _order.toString(); | |
203 } | |
204 | |
205 StringBuffer _order; | |
206 } | |
207 | |
208 main() { | |
209 CallThroughGetterTest.testMain(); | |
210 } | |
OLD | NEW |