OLD | NEW |
| (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('static invoke', () { | |
57 A.staticValue = 42; | |
58 smoke.invoke(A, #staticInc, []); | |
59 expect(A.staticValue, 43); | |
60 }); | |
61 | |
62 test('read and invoke function', () { | |
63 var a = new A(); | |
64 expect(a.i, 42); | |
65 var f = smoke.read(a, #inc1); | |
66 f(4); | |
67 expect(a.i, 46); | |
68 Function.apply(f, [4]); | |
69 expect(a.i, 50); | |
70 }); | |
71 | |
72 test('invoke with adjust', () { | |
73 var a = new A(); | |
74 smoke.invoke(a, #inc0, [], adjust: true); | |
75 expect(a.i, 43); | |
76 smoke.invoke(a, #inc0, [2], adjust: true); | |
77 expect(a.i, 44); | |
78 smoke.invoke(a, #inc0, [1, 2, 3], adjust: true); | |
79 expect(a.i, 45); | |
80 | |
81 smoke.invoke(a, #inc1, [], adjust: true); // treat as null (-10) | |
82 expect(a.i, 35); | |
83 smoke.invoke(a, #inc1, [4], adjust: true); | |
84 expect(a.i, 39); | |
85 | |
86 smoke.invoke(a, #inc2, [], adjust: true); // default is null (-10) | |
87 expect(a.i, 29); | |
88 smoke.invoke(a, #inc2, [4, 5], adjust: true); | |
89 expect(a.i, 33); | |
90 }); | |
91 | |
92 test('has getter', () { | |
93 expect(smoke.hasGetter(A, #i), isTrue); | |
94 expect(smoke.hasGetter(A, #j2), isTrue); | |
95 expect(smoke.hasGetter(A, #inc2), isTrue); | |
96 expect(smoke.hasGetter(B, #a), isTrue); | |
97 expect(smoke.hasGetter(B, #i), isFalse); | |
98 expect(smoke.hasGetter(B, #f), isTrue); | |
99 expect(smoke.hasGetter(D, #i), isTrue); | |
100 | |
101 expect(smoke.hasGetter(E, #x), isFalse); | |
102 expect(smoke.hasGetter(E, #y), isTrue); | |
103 expect(smoke.hasGetter(E, #z), isFalse); // don't consider noSuchMethod | |
104 }); | |
105 | |
106 test('has setter', () { | |
107 expect(smoke.hasSetter(A, #i), isTrue); | |
108 expect(smoke.hasSetter(A, #j2), isTrue); | |
109 expect(smoke.hasSetter(A, #inc2), isFalse); | |
110 expect(smoke.hasSetter(B, #a), isTrue); | |
111 expect(smoke.hasSetter(B, #i), isFalse); | |
112 expect(smoke.hasSetter(B, #f), isFalse); | |
113 expect(smoke.hasSetter(D, #i), isTrue); | |
114 | |
115 // TODO(sigmund): should we support declaring a setter with no getter? | |
116 // expect(smoke.hasSetter(E, #x), isTrue); | |
117 expect(smoke.hasSetter(E, #y), isFalse); | |
118 expect(smoke.hasSetter(E, #z), isFalse); // don't consider noSuchMethod | |
119 }); | |
120 | |
121 test('no such method', () { | |
122 expect(smoke.hasNoSuchMethod(A), isFalse); | |
123 expect(smoke.hasNoSuchMethod(E), isTrue); | |
124 expect(smoke.hasNoSuchMethod(E2), isTrue); | |
125 expect(smoke.hasNoSuchMethod(int), isFalse); | |
126 }); | |
127 | |
128 test('has instance method', () { | |
129 expect(smoke.hasInstanceMethod(A, #inc0), isTrue); | |
130 expect(smoke.hasInstanceMethod(A, #inc3), isFalse); | |
131 expect(smoke.hasInstanceMethod(C, #inc), isTrue); | |
132 expect(smoke.hasInstanceMethod(D, #inc), isTrue); | |
133 expect(smoke.hasInstanceMethod(D, #inc0), isTrue); | |
134 expect(smoke.hasInstanceMethod(F, #staticMethod), isFalse); | |
135 expect(smoke.hasInstanceMethod(F2, #staticMethod), isFalse); | |
136 }); | |
137 | |
138 test('has static method', () { | |
139 expect(smoke.hasStaticMethod(A, #inc0), isFalse); | |
140 expect(smoke.hasStaticMethod(C, #inc), isFalse); | |
141 expect(smoke.hasStaticMethod(D, #inc), isFalse); | |
142 expect(smoke.hasStaticMethod(D, #inc0), isFalse); | |
143 expect(smoke.hasStaticMethod(F, #staticMethod), isTrue); | |
144 expect(smoke.hasStaticMethod(F2, #staticMethod), isFalse); | |
145 }); | |
146 | |
147 test('get declaration', () { | |
148 var d = smoke.getDeclaration(B, #a); | |
149 expect(d.name, #a); | |
150 expect(d.isField, isTrue); | |
151 expect(d.isProperty, isFalse); | |
152 expect(d.isMethod, isFalse); | |
153 expect(d.isFinal, isFalse); | |
154 expect(d.isStatic, isFalse); | |
155 expect(d.annotations, []); | |
156 expect(d.type, A); | |
157 | |
158 d = smoke.getDeclaration(B, #w); | |
159 expect(d.name, #w); | |
160 expect(d.isField, isFalse); | |
161 expect(d.isProperty, isTrue); | |
162 expect(d.isMethod, isFalse); | |
163 expect(d.isFinal, isFalse); | |
164 expect(d.isStatic, isFalse); | |
165 expect(d.annotations, []); | |
166 expect(d.type, int); | |
167 | |
168 d = smoke.getDeclaration(A, #inc1); | |
169 expect(d.name, #inc1); | |
170 expect(d.isField, isFalse); | |
171 expect(d.isProperty, isFalse); | |
172 expect(d.isMethod, isTrue); | |
173 expect(d.isFinal, isFalse); | |
174 expect(d.isStatic, isFalse); | |
175 expect(d.annotations, []); | |
176 expect(d.type, Function); | |
177 | |
178 d = smoke.getDeclaration(F, #staticMethod); | |
179 expect(d.name, #staticMethod); | |
180 expect(d.isField, isFalse); | |
181 expect(d.isProperty, isFalse); | |
182 expect(d.isMethod, isTrue); | |
183 expect(d.isFinal, isFalse); | |
184 expect(d.isStatic, isTrue); | |
185 expect(d.annotations, []); | |
186 expect(d.type, Function); | |
187 | |
188 d = smoke.getDeclaration(G, #b); | |
189 expect(d.name, #b); | |
190 expect(d.isField, isTrue); | |
191 expect(d.isProperty, isFalse); | |
192 expect(d.isMethod, isFalse); | |
193 expect(d.isFinal, isFalse); | |
194 expect(d.isStatic, isFalse); | |
195 expect(d.annotations, [const Annot()]); | |
196 expect(d.type, int); | |
197 | |
198 d = smoke.getDeclaration(G, #d); | |
199 expect(d.name, #d); | |
200 expect(d.isField, isTrue); | |
201 expect(d.isProperty, isFalse); | |
202 expect(d.isMethod, isFalse); | |
203 expect(d.isFinal, isFalse); | |
204 expect(d.isStatic, isFalse); | |
205 expect(d.annotations, [32]); | |
206 expect(d.type, int); | |
207 }); | |
208 | |
209 test('isSuperclass', () { | |
210 expect(smoke.isSubclassOf(D, C), isTrue); | |
211 expect(smoke.isSubclassOf(H, G), isTrue); | |
212 expect(smoke.isSubclassOf(H, H), isTrue); | |
213 expect(smoke.isSubclassOf(H, Object), isTrue); | |
214 expect(smoke.isSubclassOf(B, Object), isTrue); | |
215 expect(smoke.isSubclassOf(A, Object), isTrue); | |
216 expect(smoke.isSubclassOf(AnnotB, Annot), isTrue); | |
217 | |
218 expect(smoke.isSubclassOf(D, A), isFalse); | |
219 expect(smoke.isSubclassOf(H, B), isFalse); | |
220 expect(smoke.isSubclassOf(B, A), isFalse); | |
221 expect(smoke.isSubclassOf(Object, A), isFalse); | |
222 }); | |
223 | |
224 group('query', () { | |
225 _checkQuery(result, names) { | |
226 expect(result.map((e) => e.name), unorderedEquals(names)); | |
227 } | |
228 | |
229 test('default', () { | |
230 var options = new smoke.QueryOptions(); | |
231 var res = smoke.query(A, options); | |
232 _checkQuery(res, [#i, #j, #j2]); | |
233 }); | |
234 | |
235 test('only fields', () { | |
236 var options = new smoke.QueryOptions(includeProperties: false); | |
237 var res = smoke.query(A, options); | |
238 _checkQuery(res, [#i, #j]); | |
239 }); | |
240 | |
241 test('only properties', () { | |
242 var options = new smoke.QueryOptions(includeFields: false); | |
243 var res = smoke.query(A, options); | |
244 _checkQuery(res, [#j2]); | |
245 }); | |
246 | |
247 test('properties and methods', () { | |
248 var options = new smoke.QueryOptions(includeMethods: true); | |
249 var res = smoke.query(A, options); | |
250 _checkQuery(res, [#i, #j, #j2, #inc0, #inc1, #inc2]); | |
251 }); | |
252 | |
253 test('inherited properties and fields', () { | |
254 var options = new smoke.QueryOptions(includeInherited: true); | |
255 var res = smoke.query(D, options); | |
256 _checkQuery(res, [#x, #y, #b, #i, #j, #j2, #x2, #i2]); | |
257 }); | |
258 | |
259 test('inherited fields only', () { | |
260 var options = new smoke.QueryOptions(includeInherited: true, | |
261 includeProperties: false); | |
262 var res = smoke.query(D, options); | |
263 _checkQuery(res, [#x, #y, #b, #i, #j]); | |
264 }); | |
265 | |
266 test('exact annotation', () { | |
267 var options = new smoke.QueryOptions(includeInherited: true, | |
268 withAnnotations: const [a1]); | |
269 var res = smoke.query(H, options); | |
270 _checkQuery(res, [#b, #f, #g]); | |
271 | |
272 options = new smoke.QueryOptions(includeInherited: true, | |
273 withAnnotations: const [a2]); | |
274 res = smoke.query(H, options); | |
275 _checkQuery(res, [#d, #h]); | |
276 | |
277 options = new smoke.QueryOptions(includeInherited: true, | |
278 withAnnotations: const [a1, a2]); | |
279 res = smoke.query(H, options); | |
280 _checkQuery(res, [#b, #d, #f, #g, #h]); | |
281 }); | |
282 | |
283 test('type annotation', () { | |
284 var options = new smoke.QueryOptions(includeInherited: true, | |
285 withAnnotations: const [Annot]); | |
286 var res = smoke.query(H, options); | |
287 _checkQuery(res, [#b, #f, #g, #i]); | |
288 }); | |
289 | |
290 test('mixed annotations (type and exact)', () { | |
291 var options = new smoke.QueryOptions(includeInherited: true, | |
292 withAnnotations: const [a2, Annot]); | |
293 var res = smoke.query(H, options); | |
294 _checkQuery(res, [#b, #d, #f, #g, #h, #i]); | |
295 }); | |
296 | |
297 test('symbol to name', () { | |
298 expect(smoke.symbolToName(#i), 'i'); | |
299 }); | |
300 | |
301 test('name to symbol', () { | |
302 expect(smoke.nameToSymbol('i'), #i); | |
303 }); | |
304 }); | |
305 | |
306 test('invoke Type instance methods', () { | |
307 var a = new A(); | |
308 expect( | |
309 smoke.invoke(a.runtimeType, #toString, []), a.runtimeType.toString()); | |
310 }); | |
311 } | |
312 | |
313 class A { | |
314 int i = 42; | |
315 int j = 44; | |
316 int get j2 => j; | |
317 void set j2(int v) { j = v; } | |
318 void inc0() { i++; } | |
319 void inc1(int v) { i = i + (v == null ? -10 : v); } | |
320 void inc2([int v]) { i = i + (v == null ? -10 : v); } | |
321 | |
322 static int staticValue = 42; | |
323 static void staticInc() { staticValue++; } | |
324 } | |
325 | |
326 class B { | |
327 final int f = 3; | |
328 int _w; | |
329 int get w => _w; | |
330 set w(int v) { _w = v; } | |
331 | |
332 String z; | |
333 A a; | |
334 | |
335 B(this._w, this.z, this.a); | |
336 } | |
337 | |
338 class C { | |
339 int x; | |
340 String y; | |
341 B b; | |
342 | |
343 inc(int n) { | |
344 x = x + n; | |
345 } | |
346 dec(int n) { | |
347 x = x - n; | |
348 } | |
349 | |
350 C(this.x, this.y, this.b); | |
351 } | |
352 | |
353 | |
354 class D extends C with A { | |
355 int get x2 => x; | |
356 int get i2 => i; | |
357 | |
358 D(x, y, b) : super(x, y, b); | |
359 } | |
360 | |
361 class E { | |
362 set x(int v) { } | |
363 int get y => 1; | |
364 | |
365 noSuchMethod(i) => y; | |
366 } | |
367 | |
368 class E2 extends E {} | |
369 | |
370 class F { | |
371 static int staticMethod(A a) => a.i; | |
372 } | |
373 | |
374 class F2 extends F {} | |
375 | |
376 class Annot { const Annot(); } | |
377 class AnnotB extends Annot { const AnnotB(); } | |
378 class AnnotC { const AnnotC({bool named: false}); } | |
379 const a1 = const Annot(); | |
380 const a2 = 32; | |
381 const a3 = const AnnotB(); | |
382 | |
383 | |
384 class G { | |
385 int a; | |
386 @a1 int b; | |
387 int c; | |
388 @a2 int d; | |
389 } | |
390 | |
391 class H extends G { | |
392 int e; | |
393 @a1 int f; | |
394 @a1 int g; | |
395 @a2 int h; | |
396 @a3 int i; | |
397 } | |
398 | |
399 class K { | |
400 @AnnotC(named: true) int k; | |
401 @AnnotC() int k2; | |
402 } | |
OLD | NEW |