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 // Tests function statements and expressions. | 5 // Tests function statements and expressions. |
6 | 6 |
7 class Bug4089219 { | 7 class Bug4089219 { |
8 int x; | 8 int x; |
9 var f; | 9 var f; |
10 | 10 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 } | 58 } |
59 | 59 |
60 typedef void Fisk(); | 60 typedef void Fisk(); |
61 | 61 |
62 class FunctionTest { | 62 class FunctionTest { |
63 | 63 |
64 FunctionTest() {} | 64 FunctionTest() {} |
65 | 65 |
66 static void testMain() { | 66 static void testMain() { |
67 var test = new FunctionTest(); | 67 var test = new FunctionTest(); |
68 test.testRecursiveClosureRef(); | |
69 test.testForEach(); | 68 test.testForEach(); |
70 test.testVarOrder1(); | 69 test.testVarOrder1(); |
71 test.testVarOrder2(); | 70 test.testVarOrder2(); |
72 test.testLexicalClosureRef1(); | 71 test.testLexicalClosureRef1(); |
73 test.testLexicalClosureRef2(); | 72 test.testLexicalClosureRef2(); |
74 test.testLexicalClosureRef3(); | 73 test.testLexicalClosureRef3(); |
75 test.testLexicalClosureRef4(); | 74 test.testLexicalClosureRef4(); |
76 test.testLexicalClosureRef5(); | 75 test.testLexicalClosureRef5(); |
77 test.testFunctionScopes(); | |
78 test.testDefaultParametersOrder(); | 76 test.testDefaultParametersOrder(); |
79 test.testParametersOrder(); | 77 test.testParametersOrder(); |
80 test.testFunctionDefaults1(); | 78 test.testFunctionDefaults1(); |
81 test.testFunctionDefaults2(); | 79 test.testFunctionDefaults2(); |
82 test.testEscapingFunctions(); | 80 test.testEscapingFunctions(); |
83 test.testThisBinding(); | 81 test.testThisBinding(); |
84 test.testFnBindingInStatics(); | 82 test.testFnBindingInStatics(); |
85 test.testFnBindingInInitLists(); | 83 test.testFnBindingInInitLists(); |
86 test.testSubclassConstructorScopeAlias(); | 84 test.testSubclassConstructorScopeAlias(); |
87 } | 85 } |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 var sum = 0; | 207 var sum = 0; |
210 for (int i = 0; i < a.length; i++) { | 208 for (int i = 0; i < a.length; i++) { |
211 sum += (a[i])(); | 209 sum += (a[i])(); |
212 } | 210 } |
213 | 211 |
214 Expect.equals(45, sum); | 212 Expect.equals(45, sum); |
215 } | 213 } |
216 | 214 |
217 int tempField; | 215 int tempField; |
218 | 216 |
219 // Validate that a closure that calls the private name of a function (for | |
220 // for recursion) calls the version of function with the bound names. | |
221 void testRecursiveClosureRef() { | |
222 tempField = 2; | |
223 var x = 3; | |
224 var g = f(a) { | |
225 tempField++; | |
226 x++; | |
227 if (a > 0) { | |
228 f(--a); | |
229 } | |
230 }; | |
231 g(2); | |
232 | |
233 | |
234 Expect.equals(5, tempField); | |
235 Expect.equals(6, x); | |
236 } | |
237 | |
238 void testForEach() { | 217 void testForEach() { |
239 List<int> vals = [1,2,3]; | 218 List<int> vals = [1,2,3]; |
240 int total = 0; | 219 int total = 0; |
241 vals.forEach((int v) { | 220 vals.forEach((int v) { |
242 total += v; | 221 total += v; |
243 }); | 222 }); |
244 Expect.equals(6, total); | 223 Expect.equals(6, total); |
245 } | 224 } |
246 | 225 |
247 void testFunctionScopes() { | |
248 // Function expression. 'recurse' is only defined within the function body. | |
249 // FAILS: | |
250 // var factorial0 = function recurse(int x) { | |
251 // return (x == 1) ? 1 : (x * recurse(x - 1)); | |
252 // }; | |
253 // TEMP: | |
254 var factorial0; | |
255 factorial0 = recurse(int x) { | |
256 return (x == 1) ? 1 : (x * factorial0(x - 1)); | |
257 }; | |
258 // END TEMP | |
259 | |
260 | |
261 // Function statement. 'factorial1' is defined in the outer scope. | |
262 int factorial1(int x) { | |
263 return (x == 1) ? 1 : (x * factorial1(x - 1)); | |
264 } | |
265 | |
266 // This would fail to compile if 'recurse' were defined in the outer scope. | |
267 // Which it shouldn't be. | |
268 int recurse = 42; | |
269 | |
270 Expect.equals(6, factorial0(3)); | |
271 Expect.equals(24, factorial0(4)); | |
272 } | |
273 | |
274 void testDefaultParametersOrder() { | 226 void testDefaultParametersOrder() { |
275 f([a = 1, b = 3]) { | 227 f([a = 1, b = 3]) { |
276 return a - b; | 228 return a - b; |
277 } | 229 } |
278 Expect.equals(-2, f()); | 230 Expect.equals(-2, f()); |
279 } | 231 } |
280 | 232 |
281 void testParametersOrder() { | 233 void testParametersOrder() { |
282 f(a, b) { | 234 f(a, b) { |
283 return a - b; | 235 return a - b; |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
349 fooIntString = foo; | 301 fooIntString = foo; |
350 | 302 |
351 uf = uf2; | 303 uf = uf2; |
352 uf2 = uf; | 304 uf2 = uf; |
353 } | 305 } |
354 } | 306 } |
355 | 307 |
356 main() { | 308 main() { |
357 FunctionTest.testMain(); | 309 FunctionTest.testMain(); |
358 } | 310 } |
OLD | NEW |