OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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 import "symbol_map_helper.dart"; | 6 import "symbol_map_helper.dart"; |
7 | 7 |
8 // Testing Function.apply calls correctly. | 8 // Testing Function.apply calls correctly. |
9 // This test is not testing error handling, only that correct parameters | 9 // This test is not testing error handling, only that correct parameters |
10 // cause a correct call. | 10 // cause a correct call. |
11 | 11 |
12 int test0() => 42; | 12 int test0() => 42; |
13 int test0a({int a}) => 37 + a; | 13 int test0a({int a}) => 37 + a; |
14 int test1(int i) => i + 1; | 14 int test1(int i) => i + 1; |
15 int test1a(int i, {int a}) => i + a; | 15 int test1a(int i, {int a}) => i + a; |
16 int test2(int i, int j) => i + j; | 16 int test2(int i, int j) => i + j; |
17 int test2a(int i, int j, {int a}) => i + j + a; | 17 int test2a(int i, int j, {int a}) => i + j + a; |
18 | 18 |
19 class C { | 19 class C { |
20 int x = 10; | 20 int x = 10; |
21 int foo(y) => this.x + y; | 21 int foo(y) => this.x + y; |
22 } | 22 } |
23 | 23 |
24 class Callable { | 24 class Callable { |
25 int call(int x, int y) => x + y; | 25 int call(int x, int y) => x + y; |
26 } | 26 } |
27 | 27 |
28 confuse() { | 28 @NoInline() |
29 try { | 29 @AssumeDynamic() |
30 throw [Function.apply]; | 30 confuse(x) => x; |
31 } catch (e) { | |
32 return e[0]; | |
33 } | |
34 return null; | |
35 } | |
36 | 31 |
37 main() { | 32 main() { |
38 testMap(res, func, map) { | 33 testMap(res, func, map) { |
39 map = symbolMapToStringMap(map); | 34 map = symbolMapToStringMap(map); |
40 Expect.equals(res, Function.apply(func, null, map)); | 35 Expect.equals(res, Function.apply(func, null, map)); |
41 Expect.equals(res, Function.apply(func, [], map)); | 36 Expect.equals(res, Function.apply(func, [], map)); |
42 } | 37 } |
43 testList(res, func, list) { | 38 testList(res, func, list) { |
44 Expect.equals(res, Function.apply(func, list)); | 39 Expect.equals(res, Function.apply(func, list)); |
45 Expect.equals(res, Function.apply(func, list, null)); | 40 Expect.equals(res, Function.apply(func, list, null)); |
46 Expect.equals(res, Function.apply(func, list, new Map<Symbol, dynamic>())); | 41 Expect.equals(res, Function.apply(func, list, new Map<Symbol, dynamic>())); |
47 } | 42 } |
48 test(res, func, list, map) { | 43 test(res, func, list, map) { |
49 map = symbolMapToStringMap(map); | 44 map = symbolMapToStringMap(map); |
50 Expect.equals(res, Function.apply(func, list, map)); | 45 Expect.equals(res, Function.apply(func, list, map)); |
51 } | 46 } |
52 testList(42, test0, null); | 47 testList(42, test0, null); |
53 testList(42, test0, []); | 48 testList(42, test0, []); |
54 testMap(42, test0a, {"a": 5}); | 49 testMap(42, test0a, {"a": 5}); |
55 testList(42, test1, [41]); | 50 testList(42, test1, [41]); |
56 test(42, test1a, [20], {"a": 22}); | 51 test(42, test1a, [20], {"a": 22}); |
57 testList(42, test2, [20, 22]); | 52 testList(42, test2, [20, 22]); |
58 test(42, test2a, [10, 15], {"a" : 17}); | 53 test(42, test2a, [10, 15], {"a" : 17}); |
59 | 54 |
60 // Test that "this" is correct when calling closurized functions. | 55 // Test that "this" is correct when calling closurized functions. |
61 var cfoo = new C().foo; | 56 var cfoo = new C().foo; |
62 testList(42, cfoo, [32]); | 57 testList(42, cfoo, [32]); |
63 | 58 |
64 // Test that apply works even with a different name. | 59 // Test that apply works even with a different name. |
65 var app = confuse(); | 60 var app = confuse(Function.apply); |
66 Expect.equals(42, app(test2, [22, 20])); | 61 Expect.equals(42, app(test2, [22, 20])); |
67 | 62 |
68 // Test that apply can itself be applied. | 63 // Test that apply can itself be applied. |
69 Expect.equals(42, Function.apply(Function.apply, [test2, [17, 25]])); | 64 Expect.equals(42, Function.apply(Function.apply, [test2, [17, 25]])); |
70 | 65 |
71 // Test that apply works on callable objects. | 66 // Test that apply works on callable objects. |
72 testList(42, new Callable(), [13, 29]); | 67 testList(42, new Callable(), [13, 29]); |
73 } | 68 } |
OLD | NEW |