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 function statements and expressions. | 7 // Tests function statements and expressions. |
8 | 8 |
9 class Bug4089219 { | 9 class Bug4089219 { |
10 int x; | 10 int x; |
11 var f; | 11 var f; |
12 | 12 |
13 Bug4089219(int i) : this.x = i { | 13 Bug4089219(int i) : this.x = i { |
14 f = () => x; | 14 f = () => x; |
15 } | 15 } |
16 } | 16 } |
17 | 17 |
18 class Bug4342163 { | 18 class Bug4342163 { |
19 final m; | 19 final m; |
20 Bug4342163(int a) : this.m = (() => a) {} | 20 Bug4342163(int a) : this.m = (() => a) {} |
21 } | 21 } |
22 | 22 |
23 class StaticFunctionDef { | 23 class StaticFunctionDef { |
24 static const int one = 1; | 24 static const int one = 1; |
25 static var fn1; | 25 static var fn1; |
26 static var fn2; | 26 static var fn2; |
27 static var fn3; | 27 static var fn3; |
28 | 28 |
29 static init() { | 29 static init() { |
30 fn1 = () { return one; }; | 30 fn1 = () { |
31 fn2 = () { return (() { return one; })(); }; | 31 return one; |
| 32 }; |
| 33 fn2 = () { |
| 34 return (() { |
| 35 return one; |
| 36 })(); |
| 37 }; |
32 fn3 = () { | 38 fn3 = () { |
33 final local = 1; | 39 final local = 1; |
34 return (() { return local; })(); | 40 return (() { |
35 }; | 41 return local; |
| 42 })(); |
| 43 }; |
36 } | 44 } |
37 } | 45 } |
38 | 46 |
39 class A { | 47 class A { |
40 var ma; | 48 var ma; |
41 A(a) {ma = a;} | 49 A(a) { |
| 50 ma = a; |
| 51 } |
42 } | 52 } |
43 | 53 |
44 class B1 extends A { | 54 class B1 extends A { |
45 final mfn; | 55 final mfn; |
46 B1(int a) : super(a), this.mfn = (() {return a;}) { | 56 B1(int a) |
47 } | 57 : super(a), |
| 58 this.mfn = (() { |
| 59 return a; |
| 60 }) {} |
48 } | 61 } |
49 | 62 |
50 class B2 extends A { | 63 class B2 extends A { |
51 final mfn; | 64 final mfn; |
52 B2(int a) : super(2), this.mfn = (() {return a;}) { | 65 B2(int a) |
53 } | 66 : super(2), |
| 67 this.mfn = (() { |
| 68 return a; |
| 69 }) {} |
54 } | 70 } |
55 | 71 |
56 class B3 extends A { | 72 class B3 extends A { |
57 final mfn; | 73 final mfn; |
58 B3(int a) : super(() {return a;}), this.mfn = (() {return a;}) { | 74 B3(int a) |
59 } | 75 : super(() { |
| 76 return a; |
| 77 }), |
| 78 this.mfn = (() { |
| 79 return a; |
| 80 }) {} |
60 } | 81 } |
61 | 82 |
62 typedef void Fisk(); | 83 typedef void Fisk(); |
63 | 84 |
64 class FunctionTest { | 85 class FunctionTest { |
65 | |
66 FunctionTest() {} | 86 FunctionTest() {} |
67 | 87 |
68 static void testMain() { | 88 static void testMain() { |
69 var test = new FunctionTest(); | 89 var test = new FunctionTest(); |
70 test.testForEach(); | 90 test.testForEach(); |
71 test.testVarOrder1(); | 91 test.testVarOrder1(); |
72 test.testVarOrder2(); | 92 test.testVarOrder2(); |
73 test.testLexicalClosureRef1(); | 93 test.testLexicalClosureRef1(); |
74 test.testLexicalClosureRef2(); | 94 test.testLexicalClosureRef2(); |
75 test.testLexicalClosureRef3(); | 95 test.testLexicalClosureRef3(); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 void testVarOrder1() { | 140 void testVarOrder1() { |
121 var a = 0, b = a++, c = a++; | 141 var a = 0, b = a++, c = a++; |
122 | 142 |
123 Expect.equals(a, 2); | 143 Expect.equals(a, 2); |
124 Expect.equals(b, 0); | 144 Expect.equals(b, 0); |
125 Expect.equals(c, 1); | 145 Expect.equals(c, 1); |
126 } | 146 } |
127 | 147 |
128 void testVarOrder2() { | 148 void testVarOrder2() { |
129 var a = 0; | 149 var a = 0; |
130 f() {return a++;}; | 150 f() { |
| 151 return a++; |
| 152 } |
| 153 |
| 154 ; |
131 var b = f(), c = f(); | 155 var b = f(), c = f(); |
132 | 156 |
133 Expect.equals(a, 2); | 157 Expect.equals(a, 2); |
134 Expect.equals(b, 0); | 158 Expect.equals(b, 0); |
135 Expect.equals(c, 1); | 159 Expect.equals(c, 1); |
136 } | 160 } |
137 | 161 |
138 void testLexicalClosureRef1() { | 162 void testLexicalClosureRef1() { |
139 var a = 1; | 163 var a = 1; |
140 var f, g; | 164 var f, g; |
141 { | 165 { |
142 var b = 2; | 166 var b = 2; |
143 f = () {return b - a;}; | 167 f = () { |
| 168 return b - a; |
| 169 }; |
144 } | 170 } |
145 | 171 |
146 { | 172 { |
147 var b = 3; | 173 var b = 3; |
148 g = () {return b - a;}; | 174 g = () { |
| 175 return b - a; |
| 176 }; |
149 } | 177 } |
150 Expect.equals(1, f()); | 178 Expect.equals(1, f()); |
151 Expect.equals(2, g()); | 179 Expect.equals(2, g()); |
152 } | 180 } |
153 | 181 |
154 void testLexicalClosureRef2() { | 182 void testLexicalClosureRef2() { |
155 var a = 1; | 183 var a = 1; |
156 var f, g; | 184 var f, g; |
157 { | 185 { |
158 var b = 2; | 186 var b = 2; |
159 f = () {return ((){return b - a;})();}; | 187 f = () { |
| 188 return (() { |
| 189 return b - a; |
| 190 })(); |
| 191 }; |
160 } | 192 } |
161 | 193 |
162 { | 194 { |
163 var b = 3; | 195 var b = 3; |
164 g = () {return ((){return b - a;})();}; | 196 g = () { |
| 197 return (() { |
| 198 return b - a; |
| 199 })(); |
| 200 }; |
165 } | 201 } |
166 Expect.equals(1, f()); | 202 Expect.equals(1, f()); |
167 Expect.equals(2, g()); | 203 Expect.equals(2, g()); |
168 } | 204 } |
169 | 205 |
170 void testLexicalClosureRef3() { | 206 void testLexicalClosureRef3() { |
171 var a = new List(); | 207 var a = new List(); |
172 for (int i = 0; i < 10; i++) { | 208 for (int i = 0; i < 10; i++) { |
173 var x = i; | 209 var x = i; |
174 a.add(() {return x;}); | 210 a.add(() { |
| 211 return x; |
| 212 }); |
175 } | 213 } |
176 | 214 |
177 var sum = 0; | 215 var sum = 0; |
178 for (int i = 0; i < a.length; i++) { | 216 for (int i = 0; i < a.length; i++) { |
179 sum += (a[i])(); | 217 sum += (a[i])(); |
180 } | 218 } |
181 | 219 |
182 Expect.equals(45, sum); | 220 Expect.equals(45, sum); |
183 } | 221 } |
184 | 222 |
185 void testLexicalClosureRef5() { | 223 void testLexicalClosureRef5() { |
186 { | 224 { |
187 var a; | 225 var a; |
188 Expect.equals(null, a); | 226 Expect.equals(null, a); |
189 a = 1; | 227 a = 1; |
190 Expect.equals(1, a); | 228 Expect.equals(1, a); |
191 } | 229 } |
192 | 230 |
193 { | 231 { |
194 var a; | 232 var a; |
195 Expect.equals(null, a); | 233 Expect.equals(null, a); |
196 a = 1; | 234 a = 1; |
197 Expect.equals(1, a); | 235 Expect.equals(1, a); |
198 } | 236 } |
199 } | 237 } |
200 | 238 |
201 // Make sure labels are preserved, and a second 'i' does influence the first. | 239 // Make sure labels are preserved, and a second 'i' does influence the first. |
202 void testLexicalClosureRef4() { | 240 void testLexicalClosureRef4() { |
203 var a = new List(); | 241 var a = new List(); |
204 x:for (int i = 0; i < 10; i++) { | 242 x: |
205 a.add(() {return i;}); | 243 for (int i = 0; i < 10; i++) { |
| 244 a.add(() { |
| 245 return i; |
| 246 }); |
206 continue x; | 247 continue x; |
207 } | 248 } |
208 | 249 |
209 var sum = 0; | 250 var sum = 0; |
210 for (int i = 0; i < a.length; i++) { | 251 for (int i = 0; i < a.length; i++) { |
211 sum += (a[i])(); | 252 sum += (a[i])(); |
212 } | 253 } |
213 | 254 |
214 Expect.equals(45, sum); | 255 Expect.equals(45, sum); |
215 } | 256 } |
216 | 257 |
217 int tempField; | 258 int tempField; |
218 | 259 |
219 void testForEach() { | 260 void testForEach() { |
220 List<int> vals = [1,2,3]; | 261 List<int> vals = [1, 2, 3]; |
221 int total = 0; | 262 int total = 0; |
222 vals.forEach((int v) { | 263 vals.forEach((int v) { |
223 total += v; | 264 total += v; |
224 }); | 265 }); |
225 Expect.equals(6, total); | 266 Expect.equals(6, total); |
226 } | 267 } |
227 | 268 |
228 void testDefaultParametersOrder() { | 269 void testDefaultParametersOrder() { |
229 f([a = 1, b = 3]) { | 270 f([a = 1, b = 3]) { |
230 return a - b; | 271 return a - b; |
231 } | 272 } |
| 273 |
232 Expect.equals(-2, f()); | 274 Expect.equals(-2, f()); |
233 } | 275 } |
234 | 276 |
235 void testParametersOrder() { | 277 void testParametersOrder() { |
236 f(a, b) { | 278 f(a, b) { |
237 return a - b; | 279 return a - b; |
238 } | 280 } |
239 Expect.equals(-2, f(1,3)); | 281 |
| 282 Expect.equals(-2, f(1, 3)); |
240 } | 283 } |
241 | 284 |
242 void testFunctionDefaults1() { | 285 void testFunctionDefaults1() { |
243 // TODO(jimhug): This return null shouldn't be necessary. | 286 // TODO(jimhug): This return null shouldn't be necessary. |
244 f() { return null; }; | 287 f() { |
245 (([a = 10]) { Expect.equals(10, a); })(); | 288 return null; |
246 ((a, [b = 10]) { Expect.equals(10, b); })(1); | 289 } |
247 (([a = 10]) { Expect.equals(null, a); })( f() ); | 290 |
| 291 ; |
| 292 (([a = 10]) { |
| 293 Expect.equals(10, a); |
| 294 })(); |
| 295 ((a, [b = 10]) { |
| 296 Expect.equals(10, b); |
| 297 })(1); |
| 298 (([a = 10]) { |
| 299 Expect.equals(null, a); |
| 300 })(f()); |
248 // FAILS: (([a = 10]) { Expect.equals(null ,a); })( f() ); | 301 // FAILS: (([a = 10]) { Expect.equals(null ,a); })( f() ); |
249 } | 302 } |
250 | 303 |
251 void testFunctionDefaults2() { | 304 void testFunctionDefaults2() { |
252 Expect.equals(10, helperFunctionDefaults2()); | 305 Expect.equals(10, helperFunctionDefaults2()); |
253 Expect.equals(1, helperFunctionDefaults2(1)); | 306 Expect.equals(1, helperFunctionDefaults2(1)); |
254 } | 307 } |
255 | 308 |
256 num helperFunctionDefaults2([a = 10]) { | 309 num helperFunctionDefaults2([a = 10]) { |
257 return ((){return a;})(); | 310 return (() { |
| 311 return a; |
| 312 })(); |
258 } | 313 } |
259 | 314 |
260 void testEscapingFunctions() { | 315 void testEscapingFunctions() { |
261 f() { return 42; }; | 316 f() { |
262 (() { Expect.equals(42, f()); })(); | 317 return 42; |
| 318 } |
| 319 |
| 320 ; |
| 321 (() { |
| 322 Expect.equals(42, f()); |
| 323 })(); |
263 var o = new Bug4089219(42); | 324 var o = new Bug4089219(42); |
264 Expect.equals(42, (o.f)()); | 325 Expect.equals(42, (o.f)()); |
265 } | 326 } |
266 | 327 |
267 void testThisBinding() { | 328 void testThisBinding() { |
268 Expect.equals(this, () { return this; }()); | 329 Expect.equals(this, () { |
| 330 return this; |
| 331 }()); |
269 } | 332 } |
270 } | 333 } |
271 | 334 |
272 typedef void Foo<A, B>(A a, B b); | 335 typedef void Foo<A, B>(A a, B b); |
273 | 336 |
274 class Bar<A, B> { | 337 class Bar<A, B> { |
275 Foo<A, B> field; | 338 Foo<A, B> field; |
276 Bar(A a, B b) : this.field = ((A a1, B b2){}) { | 339 Bar(A a, B b) : this.field = ((A a1, B b2) {}) { |
277 field(a, b); | 340 field(a, b); |
278 } | 341 } |
279 } | 342 } |
280 | 343 |
281 typedef UntypedFunction(arg); | 344 typedef UntypedFunction(arg); |
282 typedef UntypedFunction2(arg); | 345 typedef UntypedFunction2(arg); |
283 | 346 |
284 class UseFunctionTypes { | 347 class UseFunctionTypes { |
285 void test() { | 348 void test() { |
286 Function f = null; | 349 Function f = null; |
(...skipping 16 matching lines...) Expand all Loading... |
303 fooIntString = foo; | 366 fooIntString = foo; |
304 | 367 |
305 uf = uf2; | 368 uf = uf2; |
306 uf2 = uf; | 369 uf2 = uf; |
307 } | 370 } |
308 } | 371 } |
309 | 372 |
310 main() { | 373 main() { |
311 FunctionTest.testMain(); | 374 FunctionTest.testMain(); |
312 } | 375 } |
OLD | NEW |