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

Side by Side Diff: packages/analyzer/test/src/summary/resynthesize_ast_test.dart

Issue 2990843002: Removed fixed dependencies (Closed)
Patch Set: Created 3 years, 4 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
OLDNEW
(Empty)
1 // Copyright (c) 2016, 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 analyzer.test.src.summary.resynthesize_ast_test;
6
7 import 'package:analyzer/dart/ast/ast.dart';
8 import 'package:analyzer/dart/element/element.dart';
9 import 'package:analyzer/src/dart/element/element.dart';
10 import 'package:analyzer/src/generated/engine.dart'
11 show AnalysisContext, AnalysisOptionsImpl;
12 import 'package:analyzer/src/generated/sdk.dart';
13 import 'package:analyzer/src/generated/source.dart';
14 import 'package:analyzer/src/summary/format.dart';
15 import 'package:analyzer/src/summary/idl.dart';
16 import 'package:analyzer/src/summary/link.dart';
17 import 'package:analyzer/src/summary/prelink.dart';
18 import 'package:analyzer/src/summary/resynthesize.dart';
19 import 'package:analyzer/src/summary/summarize_ast.dart';
20 import 'package:analyzer/src/summary/summarize_elements.dart'
21 show PackageBundleAssembler;
22 import 'package:analyzer/task/dart.dart' show PARSED_UNIT;
23 import 'package:analyzer/task/general.dart';
24 import 'package:test_reflective_loader/test_reflective_loader.dart';
25 import 'package:unittest/unittest.dart';
26
27 import '../context/abstract_context.dart';
28 import '../task/strong/inferred_type_test.dart';
29 import 'resynthesize_common.dart';
30 import 'summary_common.dart';
31
32 main() {
33 groupSep = ' | ';
34 defineReflectiveTests(ResynthesizeAstSpecTest);
35 defineReflectiveTests(ResynthesizeAstStrongTest);
36 defineReflectiveTests(AstInferredTypeTest);
37 }
38
39 @reflectiveTest
40 class AstInferredTypeTest extends AbstractResynthesizeTest
41 with _AstResynthesizeTestMixin, InferredTypeMixin {
42 @override
43 bool get mayCheckTypesOfLocals => false;
44
45 @override
46 void addFile(String content, {String name: '/main.dart'}) {
47 addLibrarySource(name, content);
48 }
49
50 @override
51 CompilationUnitElement checkFile(String content) {
52 Source source = addSource('/main.dart', content);
53 SummaryResynthesizer resynthesizer = _encodeLibrary(source);
54 LibraryElementImpl resynthesized = _checkSource(resynthesizer, source);
55 for (Source otherSource in otherLibrarySources) {
56 _checkSource(resynthesizer, otherSource);
57 }
58 _reset();
59 return resynthesized.definingCompilationUnit;
60 }
61
62 @override
63 void compareLocalElementsOfExecutable(ExecutableElement resynthesized,
64 ExecutableElement original, String desc) {
65 // We don't resynthesize local elements during link.
66 // So, we should not compare them.
67 }
68
69 @override
70 DartSdk createDartSdk() => AbstractContextTest.SHARED_STRONG_MOCK_SDK;
71
72 @override
73 AnalysisOptionsImpl createOptions() => new AnalysisOptionsImpl()
74 ..enableGenericMethods = true
75 ..strongMode = true;
76
77 @override
78 @failingTest
79 void test_blockBodiedLambdas_async_allReturnsAreFutures_topLevel() {
80 super.test_blockBodiedLambdas_async_allReturnsAreFutures_topLevel();
81 }
82
83 @override
84 @failingTest
85 void test_blockBodiedLambdas_async_allReturnsAreValues_topLevel() {
86 super.test_blockBodiedLambdas_async_allReturnsAreValues_topLevel();
87 }
88
89 @override
90 @failingTest
91 void test_blockBodiedLambdas_async_mixOfValuesAndFutures_topLevel() {
92 super.test_blockBodiedLambdas_async_mixOfValuesAndFutures_topLevel();
93 }
94
95 @override
96 @failingTest
97 void test_blockBodiedLambdas_asyncStar_topLevel() {
98 super.test_blockBodiedLambdas_asyncStar_topLevel();
99 }
100
101 @override
102 @failingTest
103 void test_blockBodiedLambdas_basic_topLevel() {
104 super.test_blockBodiedLambdas_basic_topLevel();
105 }
106
107 @override
108 @failingTest
109 void test_blockBodiedLambdas_doesNotInferBottom_async_topLevel() {
110 super.test_blockBodiedLambdas_doesNotInferBottom_async_topLevel();
111 }
112
113 @override
114 @failingTest
115 void test_blockBodiedLambdas_doesNotInferBottom_asyncStar_topLevel() {
116 super.test_blockBodiedLambdas_doesNotInferBottom_asyncStar_topLevel();
117 }
118
119 @override
120 @failingTest
121 void test_blockBodiedLambdas_doesNotInferBottom_syncStar_topLevel() {
122 super.test_blockBodiedLambdas_doesNotInferBottom_syncStar_topLevel();
123 }
124
125 @override
126 @failingTest
127 void test_blockBodiedLambdas_LUB_topLevel() {
128 super.test_blockBodiedLambdas_LUB_topLevel();
129 }
130
131 @override
132 @failingTest
133 void test_blockBodiedLambdas_nestedLambdas_topLevel() {
134 super.test_blockBodiedLambdas_nestedLambdas_topLevel();
135 }
136
137 @override
138 @failingTest
139 void test_blockBodiedLambdas_syncStar_topLevel() {
140 super.test_blockBodiedLambdas_syncStar_topLevel();
141 }
142
143 @override
144 void test_canInferAlsoFromStaticAndInstanceFieldsFlagOn() {
145 variablesWithNotConstInitializers.add('a2');
146 super.test_canInferAlsoFromStaticAndInstanceFieldsFlagOn();
147 }
148
149 @override
150 @failingTest
151 void test_circularReference_viaClosures_initializerTypes() {
152 super.test_circularReference_viaClosures_initializerTypes();
153 }
154
155 @override
156 @failingTest
157 void test_constructors_inferFromArguments() {
158 // TODO(jmesserly): does this need to be implemented in AST summaries?
159 // The test might need a change as well to not be based on local variable
160 // types, which don't seem to be available.
161 super.test_constructors_inferFromArguments();
162 }
163
164 @override
165 @failingTest
166 void test_constructors_inferFromArguments_const() {
167 super.test_constructors_inferFromArguments_const();
168 }
169
170 @override
171 @failingTest
172 void test_constructors_inferFromArguments_factory() {
173 super.test_constructors_inferFromArguments_factory();
174 }
175
176 @override
177 @failingTest
178 void test_constructors_inferFromArguments_named() {
179 super.test_constructors_inferFromArguments_named();
180 }
181
182 @override
183 @failingTest
184 void test_constructors_inferFromArguments_namedFactory() {
185 super.test_constructors_inferFromArguments_namedFactory();
186 }
187
188 @override
189 @failingTest
190 void test_constructors_inferFromArguments_redirecting() {
191 super.test_constructors_inferFromArguments_redirecting();
192 }
193
194 @override
195 @failingTest
196 void test_constructors_inferFromArguments_redirectingFactory() {
197 super.test_constructors_inferFromArguments_redirectingFactory();
198 }
199
200 void test_infer_extractIndex_custom() {
201 var unit = checkFile('''
202 class A {
203 String operator [](_) => null;
204 }
205 var a = new A();
206 var b = a[0];
207 ''');
208 expect(unit.topLevelVariables[1].type.toString(), 'String');
209 }
210
211 void test_infer_extractIndex_fromList() {
212 var unit = checkFile('''
213 var a = <int>[1, 2, 3];
214 var b = a[0];
215 ''');
216 expect(unit.topLevelVariables[1].type.toString(), 'int');
217 }
218
219 void test_infer_extractIndex_fromMap() {
220 var unit = checkFile('''
221 var a = <int, double>{};
222 var b = a[0];
223 ''');
224 expect(unit.topLevelVariables[1].type.toString(), 'double');
225 }
226
227 void test_infer_extractProperty_getter() {
228 checkFile(r'''
229 var a = 1.isEven;
230 var b = 2.isNaN;
231 var c = 3.foo;
232 var d = foo.bar;
233 ''');
234 }
235
236 void test_infer_extractProperty_getter_sequence() {
237 var unit = checkFile(r'''
238 class A {
239 B b = new B();
240 }
241 class B {
242 C c = new C();
243 }
244 class C {
245 int d;
246 }
247 var a = new A();
248 var v = a.b.c.d;
249 ''');
250 expect(unit.topLevelVariables[1].type.toString(), 'int');
251 }
252
253 void test_infer_extractProperty_getter_sequence_generic() {
254 var unit = checkFile(r'''
255 class A<T> {
256 B<T> b = new B<T>();
257 }
258 class B<K> {
259 C<List<K>, int> c = new C<List<K>, int>();
260 }
261 class C<K, V> {
262 Map<K, V> d;
263 }
264 var a = new A<double>();
265 var v = a.b.c.d;
266 ''');
267 expect(unit.topLevelVariables[1].type.toString(), 'Map<List<double>, int>');
268 }
269
270 void test_infer_extractProperty_getter_sequence_withUnresolved() {
271 var unit = checkFile(r'''
272 class A {
273 B b = new B();
274 }
275 class B {
276 int c;
277 }
278 var a = new A();
279 var v = a.b.foo.c;
280 ''');
281 expect(unit.topLevelVariables[1].type.toString(), 'dynamic');
282 }
283
284 void test_infer_extractProperty_method() {
285 var unit = checkFile(r'''
286 class A {
287 int m(double p1, String p2) => 42;
288 }
289 var a = new A();
290 var v = a.m;
291 ''');
292 expect(unit.topLevelVariables[1].type.toString(), '(double, String) → int');
293 }
294
295 void test_infer_extractProperty_method2() {
296 var unit = checkFile(r'''
297 var a = 1.round;
298 ''');
299 expect(unit.topLevelVariables[0].type.toString(), '() → int');
300 }
301
302 void test_infer_extractProperty_method_sequence() {
303 var unit = checkFile(r'''
304 class A {
305 B b = new B();
306 }
307 class B {
308 C c = new C();
309 }
310 class C {
311 int m(double p1, String p2) => 42;
312 }
313 var a = new A();
314 var v = a.b.c.m;
315 ''');
316 expect(unit.topLevelVariables[1].type.toString(), '(double, String) → int');
317 }
318
319 void test_infer_invokeConstructor_factoryRedirected() {
320 checkFile(r'''
321 class A {
322 factory A() = B;
323 }
324 class B implements A {}
325 var a = new A();
326 ''');
327 }
328
329 void test_infer_invokeConstructor_named() {
330 checkFile(r'''
331 class A {
332 A.aaa();
333 }
334 class B<K, V> {
335 B.bbb();
336 }
337 var a = new A.aaa();
338 var b1 = new B.bbb();
339 var b2 = new B<int, String>.bbb();
340 var b3 = new B<List<int>, Map<List<int>, Set<String>>>.bbb();
341 ''');
342 }
343
344 void test_infer_invokeConstructor_named_importedWithPrefix() {
345 addFile(
346 r'''
347 class A {
348 A.aaa();
349 }
350 class B<K, V> {
351 B.bbb();
352 }
353 ''',
354 name: '/a.dart');
355 checkFile(r'''
356 import 'a.dart' as p;
357 var a = new p.A.aaa();
358 var b1 = new p.B.bbb();
359 var b2 = new p.B<int, String>.bbb();
360 ''');
361 }
362
363 void test_infer_invokeConstructor_unnamed() {
364 checkFile(r'''
365 class A {
366 A();
367 }
368 class B<T> {
369 B();
370 }
371 var a = new A();
372 var b1 = new B();
373 var b2 = new B<int>();
374 ''');
375 }
376
377 void test_infer_invokeConstructor_unnamed_synthetic() {
378 checkFile(r'''
379 class A {}
380 class B<T> {}
381 var a = new A();
382 var b1 = new B();
383 var b2 = new B<int>();
384 ''');
385 }
386
387 void test_infer_invokeMethodRef_function() {
388 var unit = checkFile(r'''
389 int m() => 0;
390 var a = m();
391 ''');
392 expect(unit.topLevelVariables[0].type.toString(), 'int');
393 }
394
395 void test_infer_invokeMethodRef_function_generic() {
396 var unit = checkFile(r'''
397 /*=Map<int, V>*/ m/*<V>*/(/*=V*/ a) => null;
398 var a = m(2.3);
399 ''');
400 expect(unit.topLevelVariables[0].type.toString(), 'Map<int, double>');
401 }
402
403 void test_infer_invokeMethodRef_function_importedWithPrefix() {
404 addFile(
405 r'''
406 int m() => 0;
407 ''',
408 name: '/a.dart');
409 var unit = checkFile(r'''
410 import 'a.dart' as p;
411 var a = p.m();
412 ''');
413 expect(unit.topLevelVariables[0].type.toString(), 'int');
414 }
415
416 void test_infer_invokeMethodRef_method() {
417 var unit = checkFile(r'''
418 class A {
419 int m() => 0;
420 }
421 var a = new A();
422 var b = a.m();
423 ''');
424 expect(unit.topLevelVariables[1].type.toString(), 'int');
425 }
426
427 void test_infer_invokeMethodRef_method_g() {
428 var unit = checkFile(r'''
429 class A {
430 /*=T*/ m/*<T>*/(/*=T*/ a) => null;
431 }
432 var a = new A();
433 var b = a.m(1.0);
434 ''');
435 expect(unit.topLevelVariables[1].type.toString(), 'double');
436 }
437
438 void test_infer_invokeMethodRef_method_genericSequence() {
439 var unit = checkFile(r'''
440 class A<T> {
441 B<T> b = new B<T>();
442 }
443 class B<K> {
444 C<List<K>, int> c = new C<List<K>, int>();
445 }
446 class C<K, V> {
447 Map<K, V> m() => null;
448 }
449 var a = new A<double>();
450 var v = a.b.c.m();
451 ''');
452 expect(unit.topLevelVariables[1].type.toString(), 'Map<List<double>, int>');
453 }
454
455 void test_infer_invokeMethodRef_method_gg() {
456 var unit = checkFile(r'''
457 class A<K> {
458 /*=Map<K, V>*/ m/*<V>*/(/*=V*/ a) => null;
459 }
460 var a = new A<int>();
461 var b = a.m(1.0);
462 ''');
463 expect(unit.topLevelVariables[1].type.toString(), 'Map<int, double>');
464 }
465
466 void test_infer_invokeMethodRef_method_importedWithPrefix() {
467 addFile(
468 r'''
469 class A {
470 int m() => 0;
471 }
472 var a = new A();
473 ''',
474 name: '/a.dart');
475 var unit = checkFile(r'''
476 import 'a.dart' as p;
477 var b = p.a.m();
478 ''');
479 expect(unit.topLevelVariables[0].type.toString(), 'int');
480 }
481
482 void test_infer_invokeMethodRef_method_importedWithPrefix2() {
483 addFile(
484 r'''
485 class A {
486 B b = new B();
487 }
488 class B {
489 int m() => 0;
490 }
491 var a = new A();
492 ''',
493 name: '/a.dart');
494 var unit = checkFile(r'''
495 import 'a.dart' as p;
496 var b = p.a.b.m();
497 ''');
498 expect(unit.topLevelVariables[0].type.toString(), 'int');
499 }
500
501 void test_infer_invokeMethodRef_method_withInferredTypeInLibraryCycle() {
502 var unit = checkFile('''
503 class Base {
504 int m() => 0;
505 }
506 class A extends Base {
507 m() => 0; // Inferred return type: int
508 }
509 var a = new A();
510 var b = a.m();
511 ''');
512 // Type inference operates on static and top level variables prior to
513 // instance members. So at the time `b` is inferred, `A.m` still has return
514 // type `dynamic`.
515 expect(unit.topLevelVariables[1].type.toString(), 'dynamic');
516 }
517
518 void test_infer_invokeMethodRef_method_withInferredTypeOutsideLibraryCycle() {
519 addFile(
520 '''
521 class Base {
522 int m() => 0;
523 }
524 class A extends Base {
525 m() => 0; // Inferred return type: int
526 }
527 ''',
528 name: '/a.dart');
529 var unit = checkFile('''
530 import 'a.dart';
531 var a = new A();
532 var b = a.m();
533 ''');
534 // Since a.dart is in a separate library file from the compilation unit
535 // containing `a` and `b`, its types are inferred first; then `a` and `b`'s
536 // types are inferred. So the inferred return type of `int` should be
537 // propagated to `b`.
538 expect(unit.topLevelVariables[1].type.toString(), 'int');
539 }
540
541 @override
542 @failingTest
543 void test_inferCorrectlyOnMultipleVariablesDeclaredTogether() {
544 super.test_inferCorrectlyOnMultipleVariablesDeclaredTogether();
545 }
546
547 @override
548 @failingTest
549 void test_inferenceInCyclesIsDeterministic() {
550 super.test_inferenceInCyclesIsDeterministic();
551 }
552
553 @override
554 @failingTest
555 void test_inferLocalFunctionReturnType() {
556 super.test_inferLocalFunctionReturnType();
557 }
558
559 @override
560 @failingTest
561 void test_inferredType_opAssignToProperty_prefixedIdentifier() {
562 super.test_inferredType_opAssignToProperty_prefixedIdentifier();
563 }
564
565 @override
566 @failingTest
567 void test_inferredType_opAssignToProperty_prefixedIdentifier_viaInterface() {
568 super
569 .test_inferredType_opAssignToProperty_prefixedIdentifier_viaInterface();
570 }
571
572 void test_invokeMethod_notGeneric_genericClass() {
573 var unit = checkFile(r'''
574 class C<T> {
575 T m(int a, {String b, T c}) => null;
576 }
577 var v = new C<double>().m(1, b: 'bbb', c: 2.0);
578 ''');
579 expect(unit.topLevelVariables[0].type.toString(), 'double');
580 }
581
582 void test_invokeMethod_notGeneric_notGenericClass() {
583 var unit = checkFile(r'''
584 class C {
585 int m(int a, {String b, int c}) => null;
586 }
587 var v = new C().m(1, b: 'bbb', c: 2.0);
588 ''');
589 expect(unit.topLevelVariables[0].type.toString(), 'int');
590 }
591
592 @override
593 @failingTest
594 void test_nullCoalescingOperator() {
595 super.test_nullCoalescingOperator();
596 }
597
598 @override
599 @failingTest
600 void test_unsafeBlockClosureInference_closureCall() {
601 super.test_unsafeBlockClosureInference_closureCall();
602 }
603
604 @override
605 @failingTest
606 void test_unsafeBlockClosureInference_constructorCall_implicitTypeParam() {
607 super.test_unsafeBlockClosureInference_constructorCall_implicitTypeParam();
608 }
609
610 @override
611 @failingTest
612 void
613 test_unsafeBlockClosureInference_functionCall_explicitDynamicParam_viaExpr 2() {
614 super
615 .test_unsafeBlockClosureInference_functionCall_explicitDynamicParam_viaE xpr2();
616 }
617
618 @override
619 @failingTest
620 void
621 test_unsafeBlockClosureInference_functionCall_explicitTypeParam_viaExpr2() {
622 super
623 .test_unsafeBlockClosureInference_functionCall_explicitTypeParam_viaExpr 2();
624 }
625
626 @override
627 @failingTest
628 void test_unsafeBlockClosureInference_functionCall_implicitTypeParam() {
629 super.test_unsafeBlockClosureInference_functionCall_implicitTypeParam();
630 }
631
632 @override
633 @failingTest
634 void
635 test_unsafeBlockClosureInference_functionCall_implicitTypeParam_viaExpr() {
636 super
637 .test_unsafeBlockClosureInference_functionCall_implicitTypeParam_viaExpr ();
638 }
639
640 @override
641 @failingTest
642 void test_unsafeBlockClosureInference_functionCall_noTypeParam_viaExpr() {
643 super.test_unsafeBlockClosureInference_functionCall_noTypeParam_viaExpr();
644 }
645
646 @override
647 @failingTest
648 void test_unsafeBlockClosureInference_inList_untyped() {
649 super.test_unsafeBlockClosureInference_inList_untyped();
650 }
651
652 @override
653 @failingTest
654 void test_unsafeBlockClosureInference_inMap_untyped() {
655 super.test_unsafeBlockClosureInference_inMap_untyped();
656 }
657
658 @override
659 @failingTest
660 void test_unsafeBlockClosureInference_methodCall_implicitTypeParam() {
661 super.test_unsafeBlockClosureInference_methodCall_implicitTypeParam();
662 }
663
664 LibraryElementImpl _checkSource(
665 SummaryResynthesizer resynthesizer, Source source) {
666 LibraryElementImpl resynthesized =
667 resynthesizer.getLibraryElement(source.uri.toString());
668 LibraryElementImpl original = context.computeLibraryElement(source);
669 checkLibraryElements(original, resynthesized);
670 return resynthesized;
671 }
672 }
673
674 @reflectiveTest
675 class ResynthesizeAstSpecTest extends _ResynthesizeAstTest {
676 @override
677 AnalysisOptionsImpl createOptions() =>
678 super.createOptions()..strongMode = false;
679 }
680
681 @reflectiveTest
682 class ResynthesizeAstStrongTest extends _ResynthesizeAstTest {
683 @override
684 AnalysisOptionsImpl createOptions() =>
685 super.createOptions()..strongMode = true;
686
687 @override
688 @failingTest
689 test_instantiateToBounds_boundRefersToLaterTypeArgument() {
690 // TODO(paulberry): this is failing due to dartbug.com/27072.
691 super.test_instantiateToBounds_boundRefersToLaterTypeArgument();
692 }
693
694 @override
695 @failingTest
696 test_syntheticFunctionType_genericClosure() {
697 super.test_syntheticFunctionType_genericClosure();
698 }
699
700 @override
701 @failingTest
702 test_syntheticFunctionType_inGenericClass() {
703 super.test_syntheticFunctionType_inGenericClass();
704 }
705
706 @override
707 @failingTest
708 test_syntheticFunctionType_noArguments() {
709 super.test_syntheticFunctionType_noArguments();
710 }
711
712 @override
713 @failingTest
714 test_syntheticFunctionType_withArguments() {
715 super.test_syntheticFunctionType_withArguments();
716 }
717 }
718
719 /**
720 * Abstract mixin for serializing ASTs and resynthesizing elements from it.
721 */
722 abstract class _AstResynthesizeTestMixin
723 implements _AstResynthesizeTestMixinInterface {
724 final Set<Source> serializedSources = new Set<Source>();
725 PackageBundleAssembler bundleAssembler = new PackageBundleAssembler();
726 final Map<String, UnlinkedUnitBuilder> uriToUnit =
727 <String, UnlinkedUnitBuilder>{};
728
729 AnalysisContext get context;
730
731 LibraryElementImpl _encodeDecodeLibraryElement(Source source) {
732 SummaryResynthesizer resynthesizer = _encodeLibrary(source);
733 return resynthesizer.getLibraryElement(source.uri.toString());
734 }
735
736 TestSummaryResynthesizer _encodeLibrary(Source source) {
737 _serializeLibrary(source);
738
739 PackageBundle bundle =
740 new PackageBundle.fromBuffer(bundleAssembler.assemble().toBuffer());
741
742 Map<String, UnlinkedUnit> unlinkedSummaries = <String, UnlinkedUnit>{};
743 for (int i = 0; i < bundle.unlinkedUnitUris.length; i++) {
744 String uri = bundle.unlinkedUnitUris[i];
745 unlinkedSummaries[uri] = bundle.unlinkedUnits[i];
746 }
747
748 LinkedLibrary getDependency(String absoluteUri) {
749 Map<String, LinkedLibrary> sdkLibraries =
750 SerializedMockSdk.instance.uriToLinkedLibrary;
751 LinkedLibrary linkedLibrary = sdkLibraries[absoluteUri];
752 if (linkedLibrary == null && !allowMissingFiles) {
753 fail('Linker unexpectedly requested LinkedLibrary for "$absoluteUri".'
754 ' Libraries available: ${sdkLibraries.keys}');
755 }
756 return linkedLibrary;
757 }
758
759 UnlinkedUnit getUnit(String absoluteUri) {
760 UnlinkedUnit unit = uriToUnit[absoluteUri] ??
761 SerializedMockSdk.instance.uriToUnlinkedUnit[absoluteUri];
762 if (unit == null && !allowMissingFiles) {
763 fail('Linker unexpectedly requested unit for "$absoluteUri".');
764 }
765 return unit;
766 }
767
768 Set<String> nonSdkLibraryUris = serializedSources
769 .where((Source source) =>
770 !source.isInSystemLibrary &&
771 context.computeKindOf(source) == SourceKind.LIBRARY)
772 .map((Source source) => source.uri.toString())
773 .toSet();
774
775 Map<String, LinkedLibrary> linkedSummaries = link(
776 nonSdkLibraryUris,
777 getDependency,
778 getUnit,
779 context.declaredVariables.get,
780 context.analysisOptions.strongMode);
781
782 return new TestSummaryResynthesizer(
783 null,
784 context,
785 new Map<String, UnlinkedUnit>()
786 ..addAll(SerializedMockSdk.instance.uriToUnlinkedUnit)
787 ..addAll(unlinkedSummaries),
788 new Map<String, LinkedLibrary>()
789 ..addAll(SerializedMockSdk.instance.uriToLinkedLibrary)
790 ..addAll(linkedSummaries),
791 allowMissingFiles);
792 }
793
794 UnlinkedUnit _getUnlinkedUnit(Source source) {
795 String uriStr = source.uri.toString();
796 {
797 UnlinkedUnit unlinkedUnitInSdk =
798 SerializedMockSdk.instance.uriToUnlinkedUnit[uriStr];
799 if (unlinkedUnitInSdk != null) {
800 return unlinkedUnitInSdk;
801 }
802 }
803 return uriToUnit.putIfAbsent(uriStr, () {
804 int modificationTime = context.computeResult(source, MODIFICATION_TIME);
805 if (modificationTime < 0) {
806 // Source does not exist.
807 if (!allowMissingFiles) {
808 fail('Unexpectedly tried to get unlinked summary for $source');
809 }
810 return null;
811 }
812 CompilationUnit unit = context.computeResult(source, PARSED_UNIT);
813 UnlinkedUnitBuilder unlinkedUnit = serializeAstUnlinked(unit);
814 bundleAssembler.addUnlinkedUnit(source, unlinkedUnit);
815 return unlinkedUnit;
816 });
817 }
818
819 void _reset() {
820 serializedSources.clear();
821 bundleAssembler = new PackageBundleAssembler();
822 uriToUnit.clear();
823 }
824
825 void _serializeLibrary(Source librarySource) {
826 if (librarySource.isInSystemLibrary) {
827 return;
828 }
829 if (!serializedSources.add(librarySource)) {
830 return;
831 }
832
833 Source resolveRelativeUri(String relativeUri) {
834 Source resolvedSource =
835 context.sourceFactory.resolveUri(librarySource, relativeUri);
836 if (resolvedSource == null) {
837 throw new StateError('Could not resolve $relativeUri in the context of '
838 '$librarySource (${librarySource.runtimeType})');
839 }
840 return resolvedSource;
841 }
842
843 UnlinkedUnit getPart(String relativeUri) {
844 return _getUnlinkedUnit(resolveRelativeUri(relativeUri));
845 }
846
847 UnlinkedPublicNamespace getImport(String relativeUri) {
848 return getPart(relativeUri)?.publicNamespace;
849 }
850
851 UnlinkedUnit definingUnit = _getUnlinkedUnit(librarySource);
852 if (definingUnit != null) {
853 LinkedLibraryBuilder linkedLibrary = prelink(
854 definingUnit, getPart, getImport, context.declaredVariables.get);
855 linkedLibrary.dependencies.skip(1).forEach((LinkedDependency d) {
856 _serializeLibrary(resolveRelativeUri(d.uri));
857 });
858 }
859 }
860 }
861
862 /**
863 * Interface that [_AstResynthesizeTestMixin] requires of classes it's mixed
864 * into. We can't place the getter below into [_AstResynthesizeTestMixin]
865 * directly, because then it would be overriding a field at the site where the
866 * mixin is instantiated.
867 */
868 abstract class _AstResynthesizeTestMixinInterface {
869 /**
870 * A test should return `true` to indicate that a missing file at the time of
871 * summary resynthesis shouldn't trigger an error.
872 */
873 bool get allowMissingFiles;
874 }
875
876 abstract class _ResynthesizeAstTest extends ResynthesizeTest
877 with _AstResynthesizeTestMixin {
878 @override
879 LibraryElementImpl checkLibrary(String text,
880 {bool allowErrors: false, bool dumpSummaries: false}) {
881 Source source = addTestSource(text);
882 LibraryElementImpl resynthesized = _encodeDecodeLibraryElement(source);
883 LibraryElementImpl original = context.computeLibraryElement(source);
884 checkLibraryElements(original, resynthesized);
885 return resynthesized;
886 }
887
888 @override
889 void compareLocalElementsOfExecutable(ExecutableElement resynthesized,
890 ExecutableElement original, String desc) {
891 // We don't resynthesize local elements during link.
892 // So, we should not compare them.
893 }
894
895 @override
896 DartSdk createDartSdk() => AbstractContextTest.SHARED_MOCK_SDK;
897
898 @override
899 TestSummaryResynthesizer encodeDecodeLibrarySource(Source source) {
900 return _encodeLibrary(source);
901 }
902 }
OLDNEW
« no previous file with comments | « packages/analyzer/test/src/summary/pub_summary_test.dart ('k') | packages/analyzer/test/src/summary/resynthesize_common.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698