Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(253)

Side by Side Diff: tests/language/invocation_mirror_test.dart

Issue 11269041: Added test for the behavior of InvocationMirror and noSuchMethod. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Oh, and just one more thing. Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | tests/language/language.status » ('j') | tests/language/language.status » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 // InvocationMirror and noSuchMethod testing.
6
7 /** Class with noSuchMethod that returns the mirror */
8 class N {
9 noSuchMethod(InvocationMirror m) => m;
10
11 get wut => this;
12
13 flif(int x) { Expect.fail("never get here"); }
14 flaf([int x]) { Expect.fail("never get here"); }
15 flof({int y}) { Expect.fail("never get here"); }
16
17 final int plif = 99;
18 int get plaf { Expect.fail("never get here"); return 0; }
19 }
20
21 /** As [N] but also implements 'call', so we can call it with wrong arguments.*/
22 class C extends N {
23 call(int x) { Expect.fail("never get here"); }
24 }
25
26 /**
27 * Checks the data of an InvocationMirror.
28 *
29 * Call without optionals for getters, with only positional for setters,
30 * and with both optionals for everything else.
31 */
32 testIM(InvocationMirror im, String name, [List positional, Map named]) {
Mads Ager (google) 2012/10/25 12:32:19 Write it out: testInvocationMirror?
Lasse Reichstein Nielsen 2012/10/25 14:12:01 Done.
33 Expect.isTrue(im is InvocationMirror);
34 Expect.equals(name, im.methodName);
35 if (named == null) {
36 Expect.isTrue(im.isAccessor);
37 Expect.isFalse(im.isMethod);
38 if (positional == null) {
39 Expect.isTrue(im.isGetter);
40 Expect.isFalse(im.isSetter);
41 Expect.equals(null, im.positionalArguments);
42 Expect.equals(null, im.positionalArguments);
43 return;
44 }
45 Expect.isTrue(im.isSetter);
46 Expect.isFalse(im.isGetter);
47 Expect.equals(1, im.positionalArguments.length);
48 Expect.equals(positional[0], im.positionalArguments[0]);
49 Expect.equals(null, im.namedArguments);
50 return;
51 }
52 Expect.isTrue(im.isMethod);
53 Expect.isFalse(im.isAccessor);
54 Expect.isFalse(im.isSetter);
55 Expect.isFalse(im.isGetter);
56
57 Expect.equals(positional.length, im.positionalArguments.length);
58 for (int i = 0; i < positional.length; i++) {
59 Expect.equals(positional[i], im.positionalArguments[i]);
60 }
61 Expect.equals(named.length, im.namedArguments.length);
62 named.forEach((k,v) {u
Mads Ager (google) 2012/10/25 12:32:19 Could you add a space? (k, v) { Also, the 'u' is
Lasse Reichstein Nielsen 2012/10/25 14:12:01 Done.
63 Expect.isTrue(im.namedArguments.containsKey(k));
64 Expect.equals(v, im.namedArguments[k]);
65 });
66 }
67
68 isNSM(e) => e is noSuchMethodError;
Mads Ager (google) 2012/10/25 12:32:19 Write it out.
Lasse Reichstein Nielsen 2012/10/25 14:12:01 That would be almost as long as the body, so I inl
69
70 // Test different ways that noSuchMethod can be called.
71 testInvocationMirrors() {
72 var n = new N();
73 var c = new C();
74
75 // Missing property/method access.
76 testIM(n.bar, 'bar');
77 testIM(n.bar = 42, 'bar=', [42]);
78 testIM(n.bar(), 'bar', [], {});
79 testIM(n.bar(42), 'bar', [42], {});
80 testIM(n.bar(x: 42), 'bar', [], {"x": 42});
81 testIM(n.bar(37, x: 42), 'bar', [37], {"x": 42});
82 testIM((n.bar)(), 'bar', [], {});
83 testIM((n.bar)(42), 'bar', [42]);
84 testIM((n.bar)(x: 42), 'bar', [], {"x": 42});
85 testIM((n.bar)(37, x: 42), 'bar', [37], {"x": 42});
86
87 // Missing operator access.
88 testIM(n + 4, '+', [4], {});
89 testIM(n - 4, '-', [4], {});
90 testIM(-n, '+', [], {});
91 testIM(n[42], '[]', [42], {});
92 testIM(n[37] = 42, '[]=', [37, 42], {});
93
94 // Calling as function when it's not.
95 testIM(n(), 'call', [], {});
96 testIM(n(42), 'call', [42], {});
97 testIM(n(x: 42), 'call', [], {"x": 42});
98 testIM(n(37, x: 42), 'call', [37], {"x": 42});
99
100 // Calling with arguments not matching existing call method.
101 testIM(c(), 'call', [], {});
102 testIM(c(37, 42), 'call', [37, 42], {});
103 testIM(c(x: 42), 'call', [], {"x": 42});
104 testIM(c(37, x: 42), 'call', [37], {"x": 42});
105
106 // Wrong arguments to existing function.
107 testIM(n.flif(), "flif", [], {});
108 testIM(n.flif(37, 42), "flif", [37, 42], {});
109 testIM(n.flif(x: 42), "flif", [], {"x": 42});
110 testIM(n.flif(37, x: 42), "flif", [37], {"x": 42});
111 testIM(n.flif = 42, "flif=", [42]);
112
113 testIM(n.flaf(37, 42), "flaf", [37, 42], {});
114 testIM(n.flaf(x: 42), "flaf", [], {"x": 42});
115 testIM(n.flaf(37, x: 42), "flaf", [37], {"x": 42});
116 testIM(n.flaf = 42, "flaf=", [42]);
117
118 testIM(n.flof(37, 42), "flof", [37, 42], {});
119 testIM(n.flof(x: 42), "flof", [], {"x": 42});
120 testIM(n.flof(37, y: 42), "flof", [37], {"y": 42});
121 testIM(n.flof = 42, "flof=", [42]);
122
123 // Reading works.
124 Expect.isTrue(n.flif is Function);
125 Expect.isTrue(n.flaf is Function);
126 Expect.isTrue(n.flof is Function);
127
128 // Writing to read-only fields.
129 testIM(n.wut = 42, "wut=", [42]);
130 testIM(n.plif = 42, "plif=", [42]);
131 testIM(n.plaf = 42, "plaf=", [42]);
132
133 // Trick call to n.call - wut is a getter returning n again.
134 testIM(n.wut(42), "call", [42]);
135
136 // Closurizing a method means that calling it badly will not hit the
137 // original receivers noSuchMethod, only the one inherited from Object
138 // by the closure object.
139 Expect.throws(() { var x = n.flif; x(37,42); }, isNSM);
Mads Ager (google) 2012/10/25 12:32:19 Add space x(37, 42)?
Lasse Reichstein Nielsen 2012/10/25 14:12:01 Done.
140 Expect.throws(() { var x = c.call; x(37,42); }, isNSM);
Mads Ager (google) 2012/10/25 12:32:19 ditto
Lasse Reichstein Nielsen 2012/10/25 14:12:01 Done.
141 }
142
143 // Test the NoSuchMethodError thrown by different incorrect calls.
144 testNoSuchMethodErrors() {
145 test(Function block) {
146 Expect.throws(block, isNSM);
147 }
148
149 var o = new Object();
150 test(() => o.bar);
151 test(() => o.bar = 42);
152 test(() => o.bar());
153 test(() => o.toString = 42);
154 test(() => o.toString(42));
155 test(() => o.toString(x: 37));
156 test(() => o.hashCode = 42);
157 test(() => o.hashCode()); // Thrown by int.noSuchMethod.
158 test(() => o());
159 }
160
161 main() {
162 testInvocationMirrors();
163 testNoSuchMethodErrors();
164 }
OLDNEW
« no previous file with comments | « no previous file | tests/language/language.status » ('j') | tests/language/language.status » ('J')

Powered by Google App Engine
This is Rietveld 408576698