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

Side by Side Diff: pkg/smoke/test/codegen/recorder_test.dart

Issue 204143002: Changes in smoke in preparation of polymer's codegen: (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 9 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
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 library smoke.test.codegen.recorder_test;
6
7 import 'package:analyzer/src/generated/element.dart';
8 import 'package:smoke/codegen/generator.dart';
9 import 'package:smoke/codegen/recorder.dart';
10 import 'package:unittest/unittest.dart';
11
12 import 'common.dart' show checkResults;
13 import 'testing_resolver_utils.dart' show initAnalyzer;
14
15 main() {
16 var provider = initAnalyzer(_SOURCES);
17 var generator;
18 var recorder;
19 setUp(() {
20 generator = new SmokeCodeGenerator();
21 recorder = new Recorder(generator, resolveImportUrl);
22 });
23
24 group('parents', () {
25 test('simple subclassing', () {
26 var lib = provider.libraryFor('/a.dart');
27 recorder.lookupParent(lib.getType('A'));
28 recorder.lookupParent(lib.getType('C'));
29
30 checkResults(generator,
31 imports: [
32 "import '/a.dart' as smoke_0;",
33 "import '/b.dart' as smoke_1;",
34 ],
35 initCall:
36 'useGeneratedCode(new StaticConfiguration(\n'
37 ' checkedMode: false,\n'
38 ' parents: {\n'
39 ' smoke_0.A: smoke_1.B,\n'
40 ' smoke_0.C: smoke_0.A,\n'
41 ' }));\n');
42 });
43
44 test('single mixin', () {
45 var lib = provider.libraryFor('/a.dart');
46 recorder.lookupParent(lib.getType('E'));
47
48 checkResults(generator,
49 imports: [
50 "import '/a.dart' as smoke_0;",
51 ],
52 topLevel: 'abstract class _M0 {} // A & D1\n',
53 initCall:
54 'useGeneratedCode(new StaticConfiguration(\n'
55 ' checkedMode: false,\n'
56 ' parents: {\n'
57 ' smoke_0.E: _M0,\n'
58 ' _M0: smoke_0.A,\n'
59 ' }));\n');
60 });
61
62 test('multiple mixins', () {
63 var lib = provider.libraryFor('/a.dart');
64 recorder.lookupParent(lib.getType('F'));
65
66 checkResults(generator,
67 imports: [
68 "import '/a.dart' as smoke_0;",
69 ],
70 topLevel:
71 'abstract class _M0 {} // A & D1\n'
72 'abstract class _M1 {} // _M0 & D2\n'
73 'abstract class _M2 {} // _M1 & D3\n',
74 initCall:
75 'useGeneratedCode(new StaticConfiguration(\n'
76 ' checkedMode: false,\n'
77 ' parents: {\n'
78 ' smoke_0.F: _M2,\n'
79 ' _M0: smoke_0.A,\n'
80 ' _M1: _M0,\n'
81 ' _M2: _M1,\n'
82 ' }));\n');
83 });
84
85 test('same as common_test', () {
86 var lib = provider.libraryFor('/common.dart');
87 recorder.lookupParent(lib.getType('Annot'));
88 recorder.lookupParent(lib.getType('AnnotB'));
89 recorder.lookupParent(lib.getType('A'));
90 recorder.lookupParent(lib.getType('B'));
91 recorder.lookupParent(lib.getType('C'));
92 recorder.lookupParent(lib.getType('D'));
93 recorder.lookupParent(lib.getType('E'));
94 recorder.lookupParent(lib.getType('E2'));
95 recorder.lookupParent(lib.getType('F'));
96 recorder.lookupParent(lib.getType('F2'));
97 recorder.lookupParent(lib.getType('G'));
98 recorder.lookupParent(lib.getType('H'));
99 var coreLib = lib.visibleLibraries.firstWhere(
100 (l) => l.displayName == 'dart.core');
101 recorder.lookupParent(coreLib.getType('int'));
102 recorder.lookupParent(coreLib.getType('num'));
103
104 checkResults(generator,
105 imports: [
106 "import '/common.dart' as smoke_0;",
107 ],
108 topLevel:
109 'abstract class _M0 {} // C & A\n',
110 initCall:
111 'useGeneratedCode(new StaticConfiguration(\n'
112 ' checkedMode: false,\n'
113 ' parents: {\n'
114 ' smoke_0.AnnotB: smoke_0.Annot,\n'
115 ' smoke_0.D: _M0,\n'
116 ' smoke_0.E2: smoke_0.E,\n'
117 ' smoke_0.F2: smoke_0.F,\n'
118 ' smoke_0.H: smoke_0.G,\n'
119 ' int: num,\n'
120 ' _M0: smoke_0.C,\n'
121 ' }));\n');
122 });
123 });
124
125 group('lookup member', () {
126 var lib;
127 setUp(() {
128 lib = provider.libraryFor('/common.dart');
129 });
130
131 test('missing declaration', () {
132 recorder.lookupMember(lib.getType('A'), 'q', includeAccessors: false);
133 checkResults(generator,
134 imports: [
135 "import '/common.dart' as smoke_0;",
136 ],
137 initCall:
138 'useGeneratedCode(new StaticConfiguration(\n'
139 ' checkedMode: false,\n'
140 ' declarations: {\n'
141 ' smoke_0.A: const {},\n'
142 ' }));\n');
143 });
144
145 test('field declaration', () {
146 recorder.lookupMember(lib.getType('A'), 'i', includeAccessors: false);
147 checkResults(generator,
148 imports: [
149 "import '/common.dart' as smoke_0;",
150 ],
151 initCall:
152 'useGeneratedCode(new StaticConfiguration(\n'
153 ' checkedMode: false,\n'
154 ' declarations: {\n'
155 ' smoke_0.A: {\n'
156 ' #i: const Declaration(#i, int),\n'
157 ' },\n'
158 ' }));\n');
159 });
160
161 test('property declaration', () {
162 recorder.lookupMember(lib.getType('A'), 'j2', includeAccessors: false);
163 checkResults(generator,
164 imports: [
165 "import '/common.dart' as smoke_0;",
166 ],
167 initCall:
168 'useGeneratedCode(new StaticConfiguration(\n'
169 ' checkedMode: false,\n'
170 ' declarations: {\n'
171 ' smoke_0.A: {\n'
172 ' #j2: const Declaration(#j2, int, kind: PROPERTY),\n'
173 ' },\n'
174 ' }));\n');
175 });
176
177 test('field and property of dynamic type', () {
178 recorder.lookupMember(lib.getType('I'), 'i1', includeAccessors: false);
179 recorder.lookupMember(lib.getType('I'), 'i2', includeAccessors: false);
180 checkResults(generator,
181 imports: [
182 "import '/common.dart' as smoke_0;",
183 ],
184 initCall:
185 'useGeneratedCode(new StaticConfiguration(\n'
186 ' checkedMode: false,\n'
187 ' declarations: {\n'
188 ' smoke_0.I: {\n'
189 ' #i1: const Declaration(#i1, dynamic),\n'
190 ' #i2: const Declaration(#i2, dynamic, kind: PROPERTY),\n'
191 ' },\n'
192 ' }));\n');
193 });
194
195 test('property of concrete type', () {
196 recorder.lookupMember(lib.getType('I'), 'i3', includeAccessors: false);
197 checkResults(generator,
198 imports: [
199 "import '/common.dart' as smoke_0;",
200 ],
201 initCall:
202 'useGeneratedCode(new StaticConfiguration(\n'
203 ' checkedMode: false,\n'
204 ' declarations: {\n'
205 ' smoke_0.I: {\n'
206 ' #i3: const Declaration(#i3, smoke_0.G, kind: PROPERTY, '
207 'isFinal: true),\n'
208 ' },\n'
209 ' }));\n');
210 });
211
212 test('method declaration', () {
213 recorder.lookupMember(lib.getType('A'), 'inc0', includeAccessors: false);
214 checkResults(generator,
215 imports: [
216 "import '/common.dart' as smoke_0;",
217 ],
218 initCall:
219 'useGeneratedCode(new StaticConfiguration(\n'
220 ' checkedMode: false,\n'
221 ' declarations: {\n'
222 ' smoke_0.A: {\n'
223 ' #inc0: const Declaration(#inc0, Function, kind: METHOD),\n'
224 ' },\n'
225 ' }));\n');
226 });
227
228 test('inherited field - not recursive', () {
229 recorder.lookupMember(lib.getType('D'), 'i', includeAccessors: false);
230 checkResults(generator,
231 imports: [
232 "import '/common.dart' as smoke_0;",
233 ],
234 initCall:
235 'useGeneratedCode(new StaticConfiguration(\n'
236 ' checkedMode: false,\n'
237 ' declarations: {\n'
238 ' smoke_0.D: const {},\n'
239 ' }));\n');
240 });
241
242 test('inherited field - recursive', () {
243 recorder.lookupMember(lib.getType('D'), 'i', recursive: true,
244 includeAccessors: false);
245 checkResults(generator,
246 imports: [
247 "import '/common.dart' as smoke_0;",
248 ],
249 topLevel: 'abstract class _M0 {} // C & A\n',
250 initCall:
251 'useGeneratedCode(new StaticConfiguration(\n'
252 ' checkedMode: false,\n'
253 ' parents: {\n'
254 ' smoke_0.D: _M0,\n'
255 ' _M0: smoke_0.C,\n'
256 ' },\n'
257 ' declarations: {\n'
258 ' smoke_0.D: const {},\n'
259 ' _M0: {\n'
260 ' #i: const Declaration(#i, int),\n'
261 ' },\n'
262 ' }));\n');
263 });
264 });
265
266 group('query', () {
267 test('default query', () {
268 var options = new QueryOptions();
269 var lib = provider.libraryFor('/common.dart');
270 recorder.runQuery(lib.getType('A'), options, includeAccessors: false);
271 checkResults(generator,
272 imports: [
273 "import '/common.dart' as smoke_0;",
274 ],
275 initCall:
276 'useGeneratedCode(new StaticConfiguration(\n'
277 ' checkedMode: false,\n'
278 ' declarations: {\n'
279 ' smoke_0.A: {\n'
280 ' #i: const Declaration(#i, int),\n'
281 ' #j: const Declaration(#j, int),\n'
282 ' #j2: const Declaration(#j2, int, kind: PROPERTY),\n'
283 ' },\n'
284 ' }));\n');
285
286 });
287
288 test('only fields', () {
289 var options = new QueryOptions(includeProperties: false);
290 var lib = provider.libraryFor('/common.dart');
291 recorder.runQuery(lib.getType('A'), options, includeAccessors: false);
292 checkResults(generator,
293 imports: [
294 "import '/common.dart' as smoke_0;",
295 ],
296 initCall:
297 'useGeneratedCode(new StaticConfiguration(\n'
298 ' checkedMode: false,\n'
299 ' declarations: {\n'
300 ' smoke_0.A: {\n'
301 ' #i: const Declaration(#i, int),\n'
302 ' #j: const Declaration(#j, int),\n'
303 ' },\n'
304 ' }));\n');
305
306 });
307
308 test('only properties', () {
309 var options = new QueryOptions(includeFields: false);
310 var lib = provider.libraryFor('/common.dart');
311 recorder.runQuery(lib.getType('A'), options, includeAccessors: false);
312 checkResults(generator,
313 imports: [
314 "import '/common.dart' as smoke_0;",
315 ],
316 initCall:
317 'useGeneratedCode(new StaticConfiguration(\n'
318 ' checkedMode: false,\n'
319 ' declarations: {\n'
320 ' smoke_0.A: {\n'
321 ' #j2: const Declaration(#j2, int, kind: PROPERTY),\n'
322 ' },\n'
323 ' }));\n');
324
325 });
326
327 test('fields, properties, and and methods', () {
328 var options = new QueryOptions(includeMethods: true);
329 var lib = provider.libraryFor('/common.dart');
330 recorder.runQuery(lib.getType('A'), options, includeAccessors: false);
331 checkResults(generator,
332 imports: [
333 "import '/common.dart' as smoke_0;",
334 ],
335 initCall:
336 'useGeneratedCode(new StaticConfiguration(\n'
337 ' checkedMode: false,\n'
338 ' declarations: {\n'
339 ' smoke_0.A: {\n'
340 ' #i: const Declaration(#i, int),\n'
341 ' #inc0: const Declaration(#inc0, Function, kind: METHOD),\n'
342 ' #inc1: const Declaration(#inc1, Function, kind: METHOD),\n'
343 ' #inc2: const Declaration(#inc2, Function, kind: METHOD),\n'
344 ' #j: const Declaration(#j, int),\n'
345 ' #j2: const Declaration(#j2, int, kind: PROPERTY),\n'
346 ' },\n'
347 ' }));\n');
348 });
349
350 test('exclude inherited', () {
351 var options = new QueryOptions(includeInherited: false);
352 var lib = provider.libraryFor('/common.dart');
353 recorder.runQuery(lib.getType('D'), options, includeAccessors: false);
354 checkResults(generator,
355 imports: [
356 "import '/common.dart' as smoke_0;",
357 ],
358 initCall:
359 'useGeneratedCode(new StaticConfiguration(\n'
360 ' checkedMode: false,\n'
361 ' declarations: {\n'
362 ' smoke_0.D: {\n'
363 ' #i2: const Declaration(#i2, int, kind: PROPERTY, '
364 'isFinal: true),\n'
365 ' #x2: const Declaration(#x2, int, kind: PROPERTY, '
366 'isFinal: true),\n'
367 ' },\n'
368 ' }));\n');
369 });
370
371 test('include inherited', () {
372 var options = new QueryOptions(includeInherited: true);
373 var lib = provider.libraryFor('/common.dart');
374 recorder.runQuery(lib.getType('D'), options, includeAccessors: false);
375 checkResults(generator,
376 imports: [
377 "import '/common.dart' as smoke_0;",
378 ],
379 topLevel: 'abstract class _M0 {} // C & A\n',
380 initCall:
381 'useGeneratedCode(new StaticConfiguration(\n'
382 ' checkedMode: false,\n'
383 ' parents: {\n'
384 ' smoke_0.D: _M0,\n'
385 ' _M0: smoke_0.C,\n'
386 ' },\n'
387 ' declarations: {\n'
388 ' smoke_0.C: {\n'
389 ' #b: const Declaration(#b, smoke_0.B),\n'
390 ' #x: const Declaration(#x, int),\n'
391 ' #y: const Declaration(#y, String),\n'
392 ' },\n'
393 ' smoke_0.D: {\n'
394 ' #i2: const Declaration(#i2, int, kind: PROPERTY, '
395 'isFinal: true),\n'
396 ' #x2: const Declaration(#x2, int, kind: PROPERTY, '
397 'isFinal: true),\n'
398 ' },\n'
399 ' _M0: {\n'
400 ' #i: const Declaration(#i, int),\n'
401 ' #j: const Declaration(#j, int),\n'
402 ' #j2: const Declaration(#j2, int, kind: PROPERTY),\n'
403 ' },\n'
404 ' }));\n');
405 });
406
407 test('exact annotation', () {
408 var lib = provider.libraryFor('/common.dart');
409 var vars = lib.definingCompilationUnit.topLevelVariables;
410 expect(vars[0].name, 'a1');
411 var options = new QueryOptions(includeInherited: true,
412 withAnnotations: [vars[0]]);
413 recorder.runQuery(lib.getType('H'), options, includeAccessors: false);
414 final annot = 'annotations: const [smoke_0.a1]';
415 checkResults(generator,
416 imports: [
417 "import '/common.dart' as smoke_0;",
418 ],
419 initCall:
420 'useGeneratedCode(new StaticConfiguration(\n'
421 ' checkedMode: false,\n'
422 ' parents: {\n'
423 ' smoke_0.H: smoke_0.G,\n'
424 ' },\n'
425 ' declarations: {\n'
426 ' smoke_0.G: {\n'
427 ' #b: const Declaration(#b, int, $annot),\n'
428 ' },\n'
429 ' smoke_0.H: {\n'
430 ' #f: const Declaration(#f, int, $annot),\n'
431 ' #g: const Declaration(#g, int, $annot),\n'
432 ' },\n'
433 ' }));\n');
434 });
435
436 test('type annotation', () {
437 var lib = provider.libraryFor('/common.dart');
438 var options = new QueryOptions(includeInherited: true,
439 withAnnotations: [lib.getType('Annot')]);
440 recorder.runQuery(lib.getType('H'), options, includeAccessors: false);
441 final a1Annot = 'annotations: const [smoke_0.a1]';
442 final a3Annot = 'annotations: const [smoke_0.a3]';
443 final exprAnnot = 'annotations: const [const smoke_0.Annot(1)]';
444 checkResults(generator,
445 imports: [
446 "import '/common.dart' as smoke_0;",
447 ],
448 initCall:
449 'useGeneratedCode(new StaticConfiguration(\n'
450 ' checkedMode: false,\n'
451 ' parents: {\n'
452 ' smoke_0.H: smoke_0.G,\n'
453 ' },\n'
454 ' declarations: {\n'
455 ' smoke_0.G: {\n'
456 ' #b: const Declaration(#b, int, $a1Annot),\n'
457 ' },\n'
458 ' smoke_0.H: {\n'
459 ' #f: const Declaration(#f, int, $a1Annot),\n'
460 ' #g: const Declaration(#g, int, $a1Annot),\n'
461 ' #i: const Declaration(#i, int, $a3Annot),\n'
462 ' #j: const Declaration(#j, int, $exprAnnot),\n'
463 ' },\n'
464 ' }));\n');
465 });
466 });
467
468 group('with accessors', () {
469 test('lookup member', () {
470 var lib = provider.libraryFor('/common.dart');
471 recorder.lookupMember(lib.getType('I'), 'i1');
472 recorder.lookupMember(lib.getType('I'), 'i2');
473 recorder.lookupMember(lib.getType('I'), 'i3');
474 checkResults(generator,
475 imports: [
476 "import '/common.dart' as smoke_0;",
477 ],
478 initCall:
479 'useGeneratedCode(new StaticConfiguration(\n'
480 ' checkedMode: false,\n'
481 ' getters: {\n'
482 ' #i1: (o) => o.i1,\n'
483 ' #i2: (o) => o.i2,\n'
484 ' #i3: (o) => o.i3,\n'
485 ' },\n'
486 ' setters: {\n' // #i3 is final
487 ' #i1: (o, v) { o.i1 = v; },\n'
488 ' #i2: (o, v) { o.i2 = v; },\n'
489 ' },\n'
490 ' declarations: {\n'
491 ' smoke_0.I: {\n'
492 ' #i1: const Declaration(#i1, dynamic),\n'
493 ' #i2: const Declaration(#i2, dynamic, kind: PROPERTY),\n'
494 ' #i3: const Declaration(#i3, smoke_0.G, kind: PROPERTY, '
495 'isFinal: true),\n'
496 ' },\n'
497 ' },\n'
498 ' names: {\n'
499 ' #i1: \'i1\',\n'
500 ' #i2: \'i2\',\n'
501 ' #i3: \'i3\',\n'
502 ' }));\n');
503 });
504
505 test('query', () {
506 var lib = provider.libraryFor('/common.dart');
507 var options = new QueryOptions(includeInherited: true,
508 withAnnotations: [lib.getType('Annot')]);
509 recorder.runQuery(lib.getType('H'), options);
510 final a1Annot = 'annotations: const [smoke_0.a1]';
511 final a3Annot = 'annotations: const [smoke_0.a3]';
512 final exprAnnot = 'annotations: const [const smoke_0.Annot(1)]';
513 checkResults(generator,
514 imports: [
515 "import '/common.dart' as smoke_0;",
516 ],
517 initCall:
518 'useGeneratedCode(new StaticConfiguration(\n'
519 ' checkedMode: false,\n'
520 ' getters: {\n'
521 ' #b: (o) => o.b,\n'
522 ' #f: (o) => o.f,\n'
523 ' #g: (o) => o.g,\n'
524 ' #i: (o) => o.i,\n'
525 ' #j: (o) => o.j,\n'
526 ' },\n'
527 ' setters: {\n' // #i3 is final
528 ' #b: (o, v) { o.b = v; },\n'
529 ' #f: (o, v) { o.f = v; },\n'
530 ' #g: (o, v) { o.g = v; },\n'
531 ' #i: (o, v) { o.i = v; },\n'
532 ' #j: (o, v) { o.j = v; },\n'
533 ' },\n'
534 ' parents: {\n'
535 ' smoke_0.H: smoke_0.G,\n'
536 ' },\n'
537 ' declarations: {\n'
538 ' smoke_0.G: {\n'
539 ' #b: const Declaration(#b, int, $a1Annot),\n'
540 ' },\n'
541 ' smoke_0.H: {\n'
542 ' #f: const Declaration(#f, int, $a1Annot),\n'
543 ' #g: const Declaration(#g, int, $a1Annot),\n'
544 ' #i: const Declaration(#i, int, $a3Annot),\n'
545 ' #j: const Declaration(#j, int, $exprAnnot),\n'
546 ' },\n'
547 ' },\n'
548 ' names: {\n'
549 ' #b: \'b\',\n'
550 ' #f: \'f\',\n'
551 ' #g: \'g\',\n'
552 ' #i: \'i\',\n'
553 ' #j: \'j\',\n'
554 ' }));\n');
555 });
556 });
557 }
558
559 const _SOURCES = const {
560 '/a.dart': '''
561 library a;
562 import '/b.dart';
563
564 class Annot { const Annot(); }
565 const annot = const Annot();
566
567 class A extends B {}
568 class C extends A {}
569 class D1 {
570 int d1;
571 }
572 class D2 {
573 int d2;
574 }
575 class D3 {
576 int d3;
577 }
578 class E extends A with D1 {
579 int e1;
580 }
581 class F extends A with D1, D2, D3 {
582 int f1;
583 }
584 ''',
585
586 '/b.dart': '''
587 library b;
588
589 class B {}
590 ''',
591 '/common.dart': '''
592 library common;
593
594 class A {
595 int i = 42;
596 int j = 44;
597 int get j2 => j;
598 void set j2(int v) { j = v; }
599 void inc0() { i++; }
600 void inc1(int v) { i = i + (v == null ? -10 : v); }
601 void inc2([int v]) { i = i + (v == null ? -10 : v); }
602 }
603
604 class B {
605 final int f = 3;
606 int _w;
607 int get w => _w;
608 set w(int v) { _w = v; }
609
610 String z;
611 A a;
612
613 B(this._w, this.z, this.a);
614 }
615
616 class C {
617 int x;
618 String y;
619 B b;
620
621 inc(int n) {
622 x = x + n;
623 }
624 dec(int n) {
625 x = x - n;
626 }
627
628 C(this.x, this.y, this.b);
629 }
630
631
632 class D extends C with A {
633 int get x2 => x;
634 int get i2 => i;
635
636 D(x, y, b) : super(x, y, b);
637 }
638
639 class E {
640 set x(int v) { }
641 int get y => 1;
642
643 noSuchMethod(i) => y;
644 }
645
646 class E2 extends E {}
647
648 class F {
649 static int staticMethod(A a) => a.i;
650 }
651
652 class F2 extends F {}
653
654 class Annot { const Annot(int ignore); }
655 class AnnotB extends Annot { const AnnotB(); }
656 const a1 = const Annot(0);
657 const a2 = 32;
658 const a3 = const AnnotB();
659
660
661 class G {
662 int a;
663 @a1 int b;
664 int c;
665 @a2 int d;
666 }
667
668 class H extends G {
669 int e;
670 @a1 int f;
671 @a1 int g;
672 @a2 int h;
673 @a3 int i;
674 @Annot(1) int j;
675 }
676
677 class I {
678 dynamic i1;
679 get i2 => null;
680 set i2(v) {}
681 G get i3;
682 }
683 '''
684 };
685
686 resolveImportUrl(LibraryElement lib) =>
687 lib.isDartCore ? 'dart:core' : '/${lib.displayName}.dart';
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698