Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012, 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 // Testing Function.apply calls correctly. | |
| 6 // This test is not testing error handling, only that correct parameters | |
| 7 // cause a correct call. | |
| 8 | |
| 9 int test0() => 42; | |
| 10 int test0a({int a}) => 37 + a; | |
| 11 int test1(int i) => i + 1; | |
| 12 int test1a(int i, {int a}) => i + a; | |
| 13 int test2(int i, int j) => i + j; | |
| 14 int test2a(int i, int j, {int a}) => i + j + a; | |
| 15 | |
| 16 bool testPassed1([int x]) => ?x; | |
| 17 bool testPassedX({int x}) => ?x; | |
| 18 | |
| 19 class C { | |
| 20 int x = 10; | |
| 21 int foo(y) => this.x + y; | |
| 22 } | |
| 23 | |
| 24 class Callable { | |
| 25 int call(int x, int y) => x + y; | |
| 26 } | |
| 27 | |
| 28 confuse() { | |
| 29 try { | |
| 30 throw [Function.apply]; | |
| 31 } catch (e) { | |
| 32 return e[0]; | |
| 33 } | |
| 34 return null; | |
| 35 } | |
| 36 | |
| 37 main() { | |
| 38 testMap(res, func, map) { | |
|
ngeoffray
2012/10/05 12:57:29
Looks better with named parameters:
test(res, func
Lasse Reichstein Nielsen
2012/10/08 07:52:01
The content of the functions differ, so having a s
| |
| 39 Expect.equals(res, Function.apply(func, null, map)); | |
| 40 Expect.equals(res, Function.apply(func, [], map)); | |
| 41 } | |
| 42 testList(res, func, list) { | |
| 43 Expect.equals(res, Function.apply(func, list)); | |
| 44 Expect.equals(res, Function.apply(func, list, null)); | |
| 45 Expect.equals(res, Function.apply(func, list, {})); | |
| 46 } | |
| 47 test(res, func, list, map) { | |
| 48 Expect.equals(res, Function.apply(func, list, map)); | |
| 49 } | |
| 50 testList(42, test0, null); | |
| 51 testList(42, test0, []]); | |
| 52 testMap(42, test0a, {"a": 5}); | |
| 53 testList(42, test1, [41]); | |
| 54 test(42, test1a, [20], {"a": 22}); | |
| 55 testList(42, test2, [20, 22]); | |
| 56 test(42, test2a, [10, 15], {"a" : 17}); | |
| 57 | |
| 58 testList(false, testPassed1, null); | |
| 59 testList(false, testPassed1, []); | |
| 60 testList(true, testPassed1, [42]); | |
| 61 | |
| 62 testMap(false, testPassedX, null); | |
| 63 testMap(false, testPassedX, {}); | |
| 64 testMap(true, testPassedX, {"x": 42}); | |
| 65 | |
| 66 // Test that "this" is correct when calling closurized functions. | |
| 67 var cfoo = new C().foo; | |
| 68 testList(42, cfoo, [32]); | |
| 69 | |
| 70 // Test that apply works even with a different name. | |
| 71 var app = confuse(); | |
| 72 Expect.equals(42, app(test2, [22, 20])); | |
| 73 | |
| 74 // Test that apply can itself be applied. | |
| 75 Expect.equals(42, Function.apply(Function.apply, [test2, [17, 25]])); | |
| 76 | |
| 77 // Test that apply works on callable objects. | |
| 78 testList(42, new Callable(), [13, 29]); | |
| 79 } | |
| OLD | NEW |