OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 library test.invoke_call_through_implicit_getter; | 5 library test.invoke_call_through_implicit_getter; |
6 | 6 |
7 @MirrorsUsed(targets: "test.invoke_call_through_implicit_getter") | 7 @MirrorsUsed(targets: "test.invoke_call_through_implicit_getter") |
8 import 'dart:mirrors'; | 8 import 'dart:mirrors'; |
9 | 9 |
10 import 'package:expect/expect.dart'; | 10 import 'package:expect/expect.dart'; |
11 | 11 |
12 class FakeFunctionCall { | 12 class FakeFunctionCall { |
13 call(x, y) => '1 $x $y'; | 13 call(x, y) => '1 $x $y'; |
14 } | 14 } |
| 15 |
15 class FakeFunctionNSM { | 16 class FakeFunctionNSM { |
16 noSuchMethod(msg) => msg.positionalArguments.join(', '); | 17 noSuchMethod(msg) => msg.positionalArguments.join(', '); |
17 } | 18 } |
18 | 19 |
19 class C { | 20 class C { |
20 var fakeFunctionCall = new FakeFunctionCall(); | 21 var fakeFunctionCall = new FakeFunctionCall(); |
21 var fakeFunctionNSM = new FakeFunctionNSM(); | 22 var fakeFunctionNSM = new FakeFunctionNSM(); |
22 var closure; // = (x, y) => '2 $this $x $y'; | 23 var closure; // = (x, y) => '2 $this $x $y'; |
23 var closureOpt; // = (x, y, [z, w]) => '3 $this $x $y $z $w'; | 24 var closureOpt; // = (x, y, [z, w]) => '3 $this $x $y $z $w'; |
24 var closureNamed; // = (x, y, {z, w}) => '4 $this $x $y $z $w'; | 25 var closureNamed; // = (x, y, {z, w}) => '4 $this $x $y $z $w'; |
25 var notAClosure = 'Not a closure'; | 26 var notAClosure = 'Not a closure'; |
26 noSuchMethod(msg) => 'DNU'; | 27 noSuchMethod(msg) => 'DNU'; |
27 | 28 |
28 C() { | 29 C() { |
29 closure = (x, y) => '2 $this $x $y'; | 30 closure = (x, y) => '2 $this $x $y'; |
30 closureOpt = (x, y, [z, w]) => '3 $this $x $y $z $w'; | 31 closureOpt = (x, y, [z, w]) => '3 $this $x $y $z $w'; |
31 closureNamed = (x, y, {z, w}) => '4 $this $x $y $z $w'; | 32 closureNamed = (x, y, {z, w}) => '4 $this $x $y $z $w'; |
32 } | 33 } |
33 | 34 |
34 toString() => 'C'; | 35 toString() => 'C'; |
35 } | 36 } |
36 | 37 |
37 testInstanceBase() { | 38 testInstanceBase() { |
38 var c = new C(); | 39 var c = new C(); |
39 | 40 |
40 Expect.equals('1 5 6', c.fakeFunctionCall(5, 6)); | 41 Expect.equals('1 5 6', c.fakeFunctionCall(5, 6)); |
41 Expect.equals('7, 8', c.fakeFunctionNSM(7, 8)); | 42 Expect.equals('7, 8', c.fakeFunctionNSM(7, 8)); |
42 Expect.equals('2 C 9 10', c.closure(9, 10)); | 43 Expect.equals('2 C 9 10', c.closure(9, 10)); |
43 Expect.equals('3 C 11 12 13 null', c.closureOpt(11, 12, 13)); | 44 Expect.equals('3 C 11 12 13 null', c.closureOpt(11, 12, 13)); |
44 Expect.equals('4 C 14 15 null 16', c.closureNamed(14, 15, w: 16)); | 45 Expect.equals('4 C 14 15 null 16', c.closureNamed(14, 15, w: 16)); |
45 Expect.equals('DNU', c.doesNotExist(17, 18)); | 46 Expect.equals('DNU', c.doesNotExist(17, 18)); |
46 Expect.throws(() => c.closure('wrong arity'), (e) => e is NoSuchMethodError); | 47 Expect.throws(() => c.closure('wrong arity'), (e) => e is NoSuchMethodError); |
47 Expect.throws(() => c.notAClosure(), (e) => e is NoSuchMethodError); | 48 Expect.throws(() => c.notAClosure(), (e) => e is NoSuchMethodError); |
48 } | 49 } |
49 | 50 |
50 testInstanceReflective() { | 51 testInstanceReflective() { |
51 InstanceMirror im = reflect(new C()); | 52 InstanceMirror im = reflect(new C()); |
52 | 53 |
53 Expect.equals('1 5 6', | 54 Expect.equals('1 5 6', im.invoke(#fakeFunctionCall, [5, 6]).reflectee); |
54 im.invoke(#fakeFunctionCall, [5, 6]).reflectee); | 55 Expect.equals('7, 8', im.invoke(#fakeFunctionNSM, [7, 8]).reflectee); |
55 Expect.equals('7, 8', | 56 Expect.equals('2 C 9 10', im.invoke(#closure, [9, 10]).reflectee); |
56 im.invoke(#fakeFunctionNSM, [7, 8]).reflectee); | 57 Expect.equals( |
57 Expect.equals('2 C 9 10', | 58 '3 C 11 12 13 null', im.invoke(#closureOpt, [11, 12, 13]).reflectee); |
58 im.invoke(#closure, [9, 10]).reflectee); | |
59 Expect.equals('3 C 11 12 13 null', | |
60 im.invoke(#closureOpt, [11, 12, 13]).reflectee); | |
61 Expect.equals('4 C 14 15 null 16', | 59 Expect.equals('4 C 14 15 null 16', |
62 im.invoke(#closureNamed, [14, 15], {#w: 16}).reflectee); | 60 im.invoke(#closureNamed, [14, 15], {#w: 16}).reflectee); |
63 Expect.equals('DNU', | 61 Expect.equals('DNU', im.invoke(#doesNotExist, [17, 18]).reflectee); |
64 im.invoke(#doesNotExist, [17, 18]).reflectee); | |
65 Expect.throws(() => im.invoke(#closure, ['wrong arity']), | 62 Expect.throws(() => im.invoke(#closure, ['wrong arity']), |
66 (e) => e is NoSuchMethodError); | 63 (e) => e is NoSuchMethodError); |
67 Expect.throws(() => im.invoke(#notAClosure, []), | 64 Expect.throws( |
68 (e) => e is NoSuchMethodError); | 65 () => im.invoke(#notAClosure, []), (e) => e is NoSuchMethodError); |
69 } | 66 } |
70 | 67 |
71 class D { | 68 class D { |
72 static var fakeFunctionCall = new FakeFunctionCall(); | 69 static var fakeFunctionCall = new FakeFunctionCall(); |
73 static var fakeFunctionNSM = new FakeFunctionNSM(); | 70 static var fakeFunctionNSM = new FakeFunctionNSM(); |
74 static var closure = (x, y) => '2 $x $y'; | 71 static var closure = (x, y) => '2 $x $y'; |
75 static var closureOpt = (x, y, [z, w]) => '3 $x $y $z $w'; | 72 static var closureOpt = (x, y, [z, w]) => '3 $x $y $z $w'; |
76 static var closureNamed = (x, y, {z, w}) => '4 $x $y $z $w'; | 73 static var closureNamed = (x, y, {z, w}) => '4 $x $y $z $w'; |
77 static var notAClosure = 'Not a closure'; | 74 static var notAClosure = 'Not a closure'; |
78 } | 75 } |
79 | 76 |
80 testClassBase() { | 77 testClassBase() { |
81 Expect.equals('1 5 6', D.fakeFunctionCall(5, 6)); | 78 Expect.equals('1 5 6', D.fakeFunctionCall(5, 6)); |
82 Expect.equals('7, 8', D.fakeFunctionNSM(7, 8)); | 79 Expect.equals('7, 8', D.fakeFunctionNSM(7, 8)); |
83 Expect.equals('2 9 10', D.closure(9, 10)); | 80 Expect.equals('2 9 10', D.closure(9, 10)); |
84 Expect.equals('3 11 12 13 null', D.closureOpt(11, 12, 13)); | 81 Expect.equals('3 11 12 13 null', D.closureOpt(11, 12, 13)); |
85 Expect.equals('4 14 15 null 16', D.closureNamed(14, 15, w: 16)); | 82 Expect.equals('4 14 15 null 16', D.closureNamed(14, 15, w: 16)); |
86 Expect.throws(() => D.closure('wrong arity'), (e) => e is NoSuchMethodError); | 83 Expect.throws(() => D.closure('wrong arity'), (e) => e is NoSuchMethodError); |
87 } | 84 } |
88 | 85 |
89 testClassReflective() { | 86 testClassReflective() { |
90 ClassMirror cm = reflectClass(D); | 87 ClassMirror cm = reflectClass(D); |
91 | 88 |
92 Expect.equals('1 5 6', | 89 Expect.equals('1 5 6', cm.invoke(#fakeFunctionCall, [5, 6]).reflectee); |
93 cm.invoke(#fakeFunctionCall, [5, 6]).reflectee); | 90 Expect.equals('7, 8', cm.invoke(#fakeFunctionNSM, [7, 8]).reflectee); |
94 Expect.equals('7, 8', | 91 Expect.equals('2 9 10', cm.invoke(#closure, [9, 10]).reflectee); |
95 cm.invoke(#fakeFunctionNSM, [7, 8]).reflectee); | 92 Expect.equals( |
96 Expect.equals('2 9 10', | 93 '3 11 12 13 null', cm.invoke(#closureOpt, [11, 12, 13]).reflectee); |
97 cm.invoke(#closure, [9, 10]).reflectee); | |
98 Expect.equals('3 11 12 13 null', | |
99 cm.invoke(#closureOpt, [11, 12, 13]).reflectee); | |
100 Expect.equals('4 14 15 null 16', | 94 Expect.equals('4 14 15 null 16', |
101 cm.invoke(#closureNamed, [14, 15], {#w: 16}).reflectee); | 95 cm.invoke(#closureNamed, [14, 15], {#w: 16}).reflectee); |
102 Expect.throws(() => cm.invoke(#closure, ['wrong arity']), | 96 Expect.throws(() => cm.invoke(#closure, ['wrong arity']), |
103 (e) => e is NoSuchMethodError); | 97 (e) => e is NoSuchMethodError); |
104 } | 98 } |
105 | 99 |
106 var fakeFunctionCall = new FakeFunctionCall(); | 100 var fakeFunctionCall = new FakeFunctionCall(); |
107 var fakeFunctionNSM = new FakeFunctionNSM(); | 101 var fakeFunctionNSM = new FakeFunctionNSM(); |
108 var closure = (x, y) => '2 $x $y'; | 102 var closure = (x, y) => '2 $x $y'; |
109 var closureOpt = (x, y, [z, w]) => '3 $x $y $z $w'; | 103 var closureOpt = (x, y, [z, w]) => '3 $x $y $z $w'; |
110 var closureNamed = (x, y, {z, w}) => '4 $x $y $z $w'; | 104 var closureNamed = (x, y, {z, w}) => '4 $x $y $z $w'; |
111 var notAClosure = 'Not a closure'; | 105 var notAClosure = 'Not a closure'; |
112 | 106 |
113 testLibraryBase() { | 107 testLibraryBase() { |
114 Expect.equals('1 5 6', fakeFunctionCall(5, 6)); | 108 Expect.equals('1 5 6', fakeFunctionCall(5, 6)); |
115 Expect.equals('7, 8', fakeFunctionNSM(7, 8)); | 109 Expect.equals('7, 8', fakeFunctionNSM(7, 8)); |
116 Expect.equals('2 9 10', closure(9, 10)); | 110 Expect.equals('2 9 10', closure(9, 10)); |
117 Expect.equals('3 11 12 13 null', closureOpt(11, 12, 13)); | 111 Expect.equals('3 11 12 13 null', closureOpt(11, 12, 13)); |
118 Expect.equals('4 14 15 null 16', closureNamed(14, 15, w: 16)); | 112 Expect.equals('4 14 15 null 16', closureNamed(14, 15, w: 16)); |
119 Expect.throws(() => closure('wrong arity'), (e) => e is NoSuchMethodError); | 113 Expect.throws(() => closure('wrong arity'), (e) => e is NoSuchMethodError); |
120 } | 114 } |
121 | 115 |
122 testLibraryReflective() { | 116 testLibraryReflective() { |
123 LibraryMirror lm = reflectClass(D).owner; | 117 LibraryMirror lm = reflectClass(D).owner; |
124 | 118 |
125 Expect.equals('1 5 6', | 119 Expect.equals('1 5 6', lm.invoke(#fakeFunctionCall, [5, 6]).reflectee); |
126 lm.invoke(#fakeFunctionCall, [5, 6]).reflectee); | 120 Expect.equals('7, 8', lm.invoke(#fakeFunctionNSM, [7, 8]).reflectee); |
127 Expect.equals('7, 8', | 121 Expect.equals('2 9 10', lm.invoke(#closure, [9, 10]).reflectee); |
128 lm.invoke(#fakeFunctionNSM, [7, 8]).reflectee); | 122 Expect.equals( |
129 Expect.equals('2 9 10', | 123 '3 11 12 13 null', lm.invoke(#closureOpt, [11, 12, 13]).reflectee); |
130 lm.invoke(#closure, [9, 10]).reflectee); | |
131 Expect.equals('3 11 12 13 null', | |
132 lm.invoke(#closureOpt, [11, 12, 13]).reflectee); | |
133 Expect.equals('4 14 15 null 16', | 124 Expect.equals('4 14 15 null 16', |
134 lm.invoke(#closureNamed, [14, 15], {#w: 16}).reflectee); | 125 lm.invoke(#closureNamed, [14, 15], {#w: 16}).reflectee); |
135 Expect.throws(() => lm.invoke(#closure, ['wrong arity']), | 126 Expect.throws(() => lm.invoke(#closure, ['wrong arity']), |
136 (e) => e is NoSuchMethodError); | 127 (e) => e is NoSuchMethodError); |
137 } | 128 } |
138 | 129 |
139 main() { | 130 main() { |
140 // Do not access the getters/closures at the base level in this variant. | 131 // Do not access the getters/closures at the base level in this variant. |
141 //testInstanceBase(); | 132 //testInstanceBase(); |
142 testInstanceReflective(); | 133 testInstanceReflective(); |
143 //testClassBase(); | 134 //testClassBase(); |
144 testClassReflective(); | 135 testClassReflective(); |
145 //testLibraryBase(); | 136 //testLibraryBase(); |
146 testLibraryReflective(); | 137 testLibraryReflective(); |
147 } | 138 } |
OLD | NEW |