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 import "package:expect/expect.dart"; | 5 import "package:expect/expect.dart"; |
6 | 6 |
7 // Tests that we can call functions through getters. | 7 // Tests that we can call functions through getters. |
8 | 8 |
9 const TOP_LEVEL_CONST = 1; | 9 const TOP_LEVEL_CONST = 1; |
10 const TOP_LEVEL_CONST_REF = TOP_LEVEL_CONST; | 10 const TOP_LEVEL_CONST_REF = TOP_LEVEL_CONST; |
11 const TOP_LEVEL_NULL = null; | 11 const TOP_LEVEL_NULL = null; |
12 | 12 |
13 var topLevel; | 13 var topLevel; |
14 | 14 |
15 class CallThroughGetterTest { | 15 class CallThroughGetterTest { |
| 16 |
16 static void testMain() { | 17 static void testMain() { |
17 testTopLevel(); | 18 testTopLevel(); |
18 testField(); | 19 testField(); |
19 testGetter(); | 20 testGetter(); |
20 testMethod(); | 21 testMethod(); |
21 testEvaluationOrder(); | 22 testEvaluationOrder(); |
22 } | 23 } |
23 | 24 |
24 static void testTopLevel() { | 25 static void testTopLevel() { |
25 topLevel = () { | 26 topLevel = () { |
(...skipping 15 matching lines...) Expand all Loading... |
41 A a = new A(); | 42 A a = new A(); |
42 a.field = () => 42; | 43 a.field = () => 42; |
43 Expect.equals(42, a.field()); | 44 Expect.equals(42, a.field()); |
44 Expect.equals(42, (a.field)()); | 45 Expect.equals(42, (a.field)()); |
45 | 46 |
46 a.field = () => 87; | 47 a.field = () => 87; |
47 Expect.equals(87, a.field()); | 48 Expect.equals(87, a.field()); |
48 Expect.equals(87, (a.field)()); | 49 Expect.equals(87, (a.field)()); |
49 | 50 |
50 a.field = 99; | 51 a.field = 99; |
51 expectThrowsNoSuchMethod(() { | 52 expectThrowsNoSuchMethod(() { a.field(); }); |
52 a.field(); | 53 expectThrowsNoSuchMethod(() { (a.field)(); }); |
53 }); | |
54 expectThrowsNoSuchMethod(() { | |
55 (a.field)(); | |
56 }); | |
57 } | 54 } |
58 | 55 |
59 static void testGetter() { | 56 static void testGetter() { |
60 A a = new A(); | 57 A a = new A(); |
61 a.field = () => 42; | 58 a.field = () => 42; |
62 Expect.equals(42, a.getter()); | 59 Expect.equals(42, a.getter()); |
63 Expect.equals(42, (a.getter)()); | 60 Expect.equals(42, (a.getter)()); |
64 | 61 |
65 a.field = () => 87; | 62 a.field = () => 87; |
66 Expect.equals(87, a.getter()); | 63 Expect.equals(87, a.getter()); |
67 Expect.equals(87, (a.getter)()); | 64 Expect.equals(87, (a.getter)()); |
68 | 65 |
69 a.field = 99; | 66 a.field = 99; |
70 expectThrowsNoSuchMethod(() { | 67 expectThrowsNoSuchMethod(() { a.getter(); }); |
71 a.getter(); | 68 expectThrowsNoSuchMethod(() { (a.getter)(); }); |
72 }); | |
73 expectThrowsNoSuchMethod(() { | |
74 (a.getter)(); | |
75 }); | |
76 } | 69 } |
77 | 70 |
78 static void testMethod() { | 71 static void testMethod() { |
79 A a = new A(); | 72 A a = new A(); |
80 a.field = () => 42; | 73 a.field = () => 42; |
81 Expect.equals(true, a.method() is Function); | 74 Expect.equals(true, a.method() is Function); |
82 Expect.equals(42, a.method()()); | 75 Expect.equals(42, a.method()()); |
83 | 76 |
84 a.field = () => 87; | 77 a.field = () => 87; |
85 Expect.equals(true, a.method() is Function); | 78 Expect.equals(true, a.method() is Function); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 Expect.fail("Wrong exception. Expected: NoSuchMethodError" | 115 Expect.fail("Wrong exception. Expected: NoSuchMethodError" |
123 " got: ${exception}"); | 116 " got: ${exception}"); |
124 } | 117 } |
125 } | 118 } |
126 | 119 |
127 static catchException(fn) { | 120 static catchException(fn) { |
128 bool caught = false; | 121 bool caught = false; |
129 var result = null; | 122 var result = null; |
130 try { | 123 try { |
131 fn(); | 124 fn(); |
132 Expect.equals(true, false); // Shouldn't reach this. | 125 Expect.equals(true, false); // Shouldn't reach this. |
133 } catch (e) { | 126 } catch (e) { |
134 caught = true; | 127 caught = true; |
135 result = e; | 128 result = e; |
136 } | 129 } |
137 Expect.equals(true, caught); | 130 Expect.equals(true, caught); |
138 return result; | 131 return result; |
139 } | 132 } |
| 133 |
140 } | 134 } |
141 | 135 |
| 136 |
142 class A { | 137 class A { |
143 A() {} | 138 |
| 139 A() { } |
144 var field; | 140 var field; |
145 get getter { | 141 get getter { return field; } |
146 return field; | 142 method() { return field; } |
147 } | |
148 | 143 |
149 method() { | |
150 return field; | |
151 } | |
152 } | 144 } |
153 | 145 |
| 146 |
154 class B { | 147 class B { |
155 B() : _order = new StringBuffer("") {} | |
156 | 148 |
157 get g0 { | 149 B() : _order = new StringBuffer("") { } |
158 _mark('g'); | |
159 return () { | |
160 return _mark('f'); | |
161 }; | |
162 } | |
163 | 150 |
164 get g1 { | 151 get g0 { _mark('g'); return () { return _mark('f'); }; } |
165 _mark('g'); | 152 get g1 { _mark('g'); return (x) { return _mark('f'); }; } |
166 return (x) { | 153 get g2 { _mark('g'); return (x, y) { return _mark('f'); }; } |
167 return _mark('f'); | 154 get g3 { _mark('g'); return (x, y, z) { return _mark('f'); }; } |
168 }; | |
169 } | |
170 | 155 |
171 get g2 { | 156 get x { _mark('x'); return 0; } |
172 _mark('g'); | 157 get y { _mark('y'); return 1; } |
173 return (x, y) { | 158 get z { _mark('z'); return 2; } |
174 return _mark('f'); | |
175 }; | |
176 } | |
177 | 159 |
178 get g3 { | 160 _mark(m) { _order.write(m); return _order.toString(); } |
179 _mark('g'); | 161 StringBuffer _order; |
180 return (x, y, z) { | |
181 return _mark('f'); | |
182 }; | |
183 } | |
184 | 162 |
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 } | 163 } |
207 | 164 |
208 main() { | 165 main() { |
209 CallThroughGetterTest.testMain(); | 166 CallThroughGetterTest.testMain(); |
210 } | 167 } |
OLD | NEW |