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

Side by Side Diff: pkg/smoke/test/common.dart

Issue 169913004: Adding package:smoke. This CL includes the intial APIs, a mirror-based (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 10 months 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 | « pkg/smoke/test/args_test.dart ('k') | pkg/smoke/test/mirrors_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 /// Common test code that is run by 3 tests: mirrors_test.dart,
6 /// mirrors_used_test.dart, and static_test.dart.
7 library smoke.test.common;
8
9 import 'package:smoke/smoke.dart' as smoke;
10 import 'package:unittest/unittest.dart';
11
12 main() {
13 test('read value', () {
14 var a = new A();
15 expect(smoke.read(a, #i), 42);
16 expect(smoke.read(a, #j), 44);
17 expect(smoke.read(a, #j2), 44);
18 });
19
20 test('write value', () {
21 var a = new A();
22 smoke.write(a, #i, 43);
23 expect(a.i, 43);
24 smoke.write(a, #j2, 46);
25 expect(a.j, 46);
26 expect(a.j2, 46);
27 expect(smoke.read(a, #j), 46);
28 expect(smoke.read(a, #j2), 46);
29 });
30
31 test('invoke', () {
32 var a = new A();
33
34 smoke.invoke(a, #inc0, []);
35 expect(a.i, 43);
36 expect(smoke.read(a, #i), 43);
37 expect(() => smoke.invoke(a, #inc0, [2]), throws);
38 expect(a.i, 43);
39 expect(() => smoke.invoke(a, #inc0, [1, 2, 3]), throws);
40 expect(a.i, 43);
41
42 expect(() => smoke.invoke(a, #inc1, []), throws);
43 expect(a.i, 43);
44 smoke.invoke(a, #inc1, [4]);
45 expect(a.i, 47);
46
47 smoke.invoke(a, #inc2, []);
48 expect(a.i, 37);
49 smoke.invoke(a, #inc2, [4]);
50 expect(a.i, 41);
51
52 expect(() => smoke.invoke(a, #inc1, [4, 5]), throws);
53 expect(a.i, 41);
54 });
55
56 test('read and invoke function', () {
57 var a = new A();
58 expect(a.i, 42);
59 var f = smoke.read(a, #inc1);
60 f(4);
61 expect(a.i, 46);
62 Function.apply(f, [4]);
63 expect(a.i, 50);
64 });
65
66 test('invoke with adjust', () {
67 var a = new A();
68 smoke.invoke(a, #inc0, [], adjust: true);
69 expect(a.i, 43);
70 smoke.invoke(a, #inc0, [2], adjust: true);
71 expect(a.i, 44);
72 smoke.invoke(a, #inc0, [1, 2, 3], adjust: true);
73 expect(a.i, 45);
74
75 smoke.invoke(a, #inc1, [], adjust: true); // treat as null (-10)
76 expect(a.i, 35);
77 smoke.invoke(a, #inc1, [4], adjust: true);
78 expect(a.i, 39);
79
80 smoke.invoke(a, #inc2, [], adjust: true); // default is null (-10)
81 expect(a.i, 29);
82 smoke.invoke(a, #inc2, [4, 5], adjust: true);
83 expect(a.i, 33);
84 });
85
86 test('has getter', () {
87 expect(smoke.hasGetter(A, #i), isTrue);
88 expect(smoke.hasGetter(A, #j2), isTrue);
89 expect(smoke.hasGetter(A, #inc2), isTrue);
90 expect(smoke.hasGetter(B, #a), isTrue);
91 expect(smoke.hasGetter(B, #i), isFalse);
92 expect(smoke.hasGetter(B, #f), isTrue);
93 expect(smoke.hasGetter(D, #i), isTrue);
94
95 expect(smoke.hasGetter(E, #x), isFalse);
96 expect(smoke.hasGetter(E, #y), isTrue);
97 expect(smoke.hasGetter(E, #z), isFalse); // don't consider noSuchMethod
98 });
99
100 test('has setter', () {
101 expect(smoke.hasSetter(A, #i), isTrue);
102 expect(smoke.hasSetter(A, #j2), isTrue);
103 expect(smoke.hasSetter(A, #inc2), isFalse);
104 expect(smoke.hasSetter(B, #a), isTrue);
105 expect(smoke.hasSetter(B, #i), isFalse);
106 expect(smoke.hasSetter(B, #f), isFalse);
107 expect(smoke.hasSetter(D, #i), isTrue);
108
109 // TODO(sigmund): should we support declaring a setter with no getter?
110 // expect(smoke.hasSetter(E, #x), isTrue);
111 expect(smoke.hasSetter(E, #y), isFalse);
112 expect(smoke.hasSetter(E, #z), isFalse); // don't consider noSuchMethod
113 });
114
115 test('no such method', () {
116 expect(smoke.hasNoSuchMethod(A), isFalse);
117 expect(smoke.hasNoSuchMethod(E), isTrue);
118 expect(smoke.hasNoSuchMethod(E2), isTrue);
119 });
120
121 test('has instance method', () {
122 expect(smoke.hasInstanceMethod(A, #inc0), isTrue);
123 expect(smoke.hasInstanceMethod(A, #inc3), isFalse);
124 expect(smoke.hasInstanceMethod(C, #inc), isTrue);
125 expect(smoke.hasInstanceMethod(D, #inc), isTrue);
126 expect(smoke.hasInstanceMethod(D, #inc0), isTrue);
127 expect(smoke.hasInstanceMethod(F, #staticMethod), isFalse);
128 expect(smoke.hasInstanceMethod(F2, #staticMethod), isFalse);
129 });
130
131 test('has static method', () {
132 expect(smoke.hasStaticMethod(A, #inc0), isFalse);
133 expect(smoke.hasStaticMethod(C, #inc), isFalse);
134 expect(smoke.hasStaticMethod(D, #inc), isFalse);
135 expect(smoke.hasStaticMethod(D, #inc0), isFalse);
136 expect(smoke.hasStaticMethod(F, #staticMethod), isTrue);
137 expect(smoke.hasStaticMethod(F2, #staticMethod), isFalse);
138 });
139
140 test('get declaration', () {
141 var d = smoke.getDeclaration(B, #a);
142 expect(d.name, #a);
143 expect(d.isProperty, isTrue);
144 expect(d.isMethod, isFalse);
145 expect(d.isFinal, isFalse);
146 expect(d.isStatic, isFalse);
147 expect(d.annotations, []);
148 expect(d.type, A);
149
150 d = smoke.getDeclaration(B, #w);
151 expect(d.name, #w);
152 expect(d.isProperty, isTrue);
153 expect(d.isMethod, isFalse);
154 expect(d.isFinal, isFalse);
155 expect(d.isStatic, isFalse);
156 expect(d.annotations, []);
157 expect(d.type, int);
158
159 d = smoke.getDeclaration(A, #inc1);
160 expect(d.name, #inc1);
161 expect(d.isProperty, isFalse);
162 expect(d.isMethod, isTrue);
163 expect(d.isFinal, isFalse);
164 expect(d.isStatic, isFalse);
165 expect(d.annotations, []);
166 expect(d.type, Function);
167
168 d = smoke.getDeclaration(F, #staticMethod);
169 expect(d.name, #staticMethod);
170 expect(d.isProperty, isFalse);
171 expect(d.isMethod, isTrue);
172 expect(d.isFinal, isFalse);
173 expect(d.isStatic, isTrue);
174 expect(d.annotations, []);
175 expect(d.type, Function);
176
177 d = smoke.getDeclaration(G, #b);
178 expect(d.name, #b);
179 expect(d.isProperty, isTrue);
180 expect(d.isMethod, isFalse);
181 expect(d.isFinal, isFalse);
182 expect(d.isStatic, isFalse);
183 expect(d.annotations, [const Annot()]);
184 expect(d.type, int);
185
186 d = smoke.getDeclaration(G, #d);
187 expect(d.name, #d);
188 expect(d.isProperty, isTrue);
189 expect(d.isMethod, isFalse);
190 expect(d.isFinal, isFalse);
191 expect(d.isStatic, isFalse);
192 expect(d.annotations, [32]);
193 expect(d.type, int);
194 });
195
196 group('query', () {
197 test('default', () {
198 var options = new smoke.QueryOptions();
199 var res = smoke.query(A, options);
200 expect(res.map((e) => e.name), [#i, #j, #j2]);
201 });
202
203 test('properties and methods', () {
204 var options = new smoke.QueryOptions(includeMethods: true);
205 var res = smoke.query(A, options);
206 expect(res.map((e) => e.name), [#i, #j, #j2, #inc0, #inc1, #inc2]);
207 });
208
209 test('inherited properties', () {
210 var options = new smoke.QueryOptions(includeInherited: true);
211 var res = smoke.query(D, options);
212 expect(res.map((e) => e.name), [#x, #y, #b, #i, #j, #j2, #x2, #i2]);
213 });
214
215 test('exact annotation', () {
216 var options = new smoke.QueryOptions(includeInherited: true,
217 withAnnotations: const [a1]);
218 var res = smoke.query(H, options);
219 expect(res.map((e) => e.name), [#b, #f, #g]);
220
221 options = new smoke.QueryOptions(includeInherited: true,
222 withAnnotations: const [a2]);
223 res = smoke.query(H, options);
224 expect(res.map((e) => e.name), [#d, #h]);
225
226 options = new smoke.QueryOptions(includeInherited: true,
227 withAnnotations: const [a1, a2]);
228 res = smoke.query(H, options);
229 expect(res.map((e) => e.name), [#b, #d, #f, #g, #h]);
230 });
231
232 test('type annotation', () {
233 var options = new smoke.QueryOptions(includeInherited: true,
234 withAnnotations: const [Annot]);
235 var res = smoke.query(H, options);
236 expect(res.map((e) => e.name), [#b, #f, #g]);
237 });
238
239 test('mixed annotations (type and exact)', () {
240 var options = new smoke.QueryOptions(includeInherited: true,
241 withAnnotations: const [a2, Annot]);
242 var res = smoke.query(H, options);
243 expect(res.map((e) => e.name), [#b, #d, #f, #g, #h]);
244 });
245
246 test('symbol to name', () {
247 expect(smoke.symbolToName(#i), 'i');
248 });
249
250 test('name to symbol', () {
251 expect(smoke.nameToSymbol('i'), #i);
252 });
253 });
254 }
255
256 class A {
257 int i = 42;
258 int j = 44;
259 int get j2 => j;
260 void set j2(int v) { j = v; }
261 void inc0() { i++; }
262 void inc1(int v) { i = i + (v == null ? -10 : v); }
263 void inc2([int v]) { i = i + (v == null ? -10 : v); }
264 }
265
266 class B {
267 final int f = 3;
268 int _w;
269 int get w => _w;
270 set w(int v) { _w = v; }
271
272 String z;
273 A a;
274
275 B(this._w, this.z, this.a);
276 }
277
278 class C {
279 int x;
280 String y;
281 B b;
282
283 inc(int n) {
284 x = x + n;
285 }
286 dec(int n) {
287 x = x - n;
288 }
289
290 C(this.x, this.y, this.b);
291 }
292
293
294 class D extends C with A {
295 int get x2 => x;
296 int get i2 => i;
297
298 D(x, y, b) : super(x, y, b);
299 }
300
301 class E {
302 set x(int v) { }
303 int get y => 1;
304
305 noSuchMethod(i) => y;
306 }
307
308 class E2 extends E {}
309
310 class F {
311 static int staticMethod(A a) => a.i;
312 }
313
314 class F2 extends F {}
315
316 class Annot { const Annot(); }
317 const a1 = const Annot();
318 const a2 = 32;
319
320
321 class G {
322 int a;
323 @a1 int b;
324 int c;
325 @a2 int d;
326 }
327
328 class H extends G {
329 int e;
330 @a1 int f;
331 @a1 int g;
332 @a2 int h;
333 }
OLDNEW
« no previous file with comments | « pkg/smoke/test/args_test.dart ('k') | pkg/smoke/test/mirrors_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698