Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library test.src.serialization.elements_test; | 5 library test.src.serialization.elements_test; |
| 6 | 6 |
| 7 import 'dart:convert'; | 7 import 'dart:convert'; |
| 8 | 8 |
| 9 import 'package:analyzer/dart/ast/ast.dart'; | 9 import 'package:analyzer/dart/ast/ast.dart'; |
| 10 import 'package:analyzer/dart/element/element.dart'; | 10 import 'package:analyzer/dart/element/element.dart'; |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 124 // Check that the only unlinked summaries consulted were those for the | 124 // Check that the only unlinked summaries consulted were those for the |
| 125 // library in question. | 125 // library in question. |
| 126 Set<String> expectedCompilationUnitUris = library.units | 126 Set<String> expectedCompilationUnitUris = library.units |
| 127 .map((CompilationUnitElement unit) => unit.source.uri.toString()) | 127 .map((CompilationUnitElement unit) => unit.source.uri.toString()) |
| 128 .toSet(); | 128 .toSet(); |
| 129 for (String requestedUri in resynthesizer.unlinkedSummariesRequested) { | 129 for (String requestedUri in resynthesizer.unlinkedSummariesRequested) { |
| 130 expect(expectedCompilationUnitUris, contains(requestedUri)); | 130 expect(expectedCompilationUnitUris, contains(requestedUri)); |
| 131 } | 131 } |
| 132 } | 132 } |
| 133 | 133 |
| 134 void checkPossibleLocalElements(Element resynthesized, Element original) { | |
| 135 if (original is! LocalElement && resynthesized is! LocalElement) { | |
| 136 return; | |
| 137 } | |
| 138 // TODO(scheglov) add support for parameters | |
| 139 if (original is ParameterElement && resynthesized is ParameterElement) { | |
| 140 return; | |
| 141 } | |
| 142 if (original is LocalElement && resynthesized is LocalElement) { | |
| 143 expect(resynthesized.visibleRange, original.visibleRange); | |
| 144 } else { | |
| 145 fail('Incompatible local elements ' | |
| 146 '${resynthesized.runtimeType} vs. ${original.runtimeType}'); | |
| 147 } | |
| 148 } | |
| 149 | |
| 134 void checkPossibleMember( | 150 void checkPossibleMember( |
| 135 Element resynthesized, Element original, String desc) { | 151 Element resynthesized, Element original, String desc) { |
| 136 Element resynthesizedNonHandle = resynthesized is ElementHandle | 152 Element resynthesizedNonHandle = resynthesized is ElementHandle |
| 137 ? resynthesized.actualElement | 153 ? resynthesized.actualElement |
| 138 : resynthesized; | 154 : resynthesized; |
| 139 if (original is Member) { | 155 if (original is Member) { |
| 140 expect(resynthesizedNonHandle, new isInstanceOf<Member>(), reason: desc); | 156 expect(resynthesizedNonHandle, new isInstanceOf<Member>(), reason: desc); |
| 141 if (resynthesizedNonHandle is Member) { | 157 if (resynthesizedNonHandle is Member) { |
| 142 List<DartType> resynthesizedTypeArguments = | 158 List<DartType> resynthesizedTypeArguments = |
| 143 resynthesizedNonHandle.definingType.typeArguments; | 159 resynthesizedNonHandle.definingType.typeArguments; |
| (...skipping 383 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 527 compareTypes( | 543 compareTypes( |
| 528 resynthesized.returnType, original.returnType, '$desc return type'); | 544 resynthesized.returnType, original.returnType, '$desc return type'); |
| 529 compareTypes(resynthesized.type, original.type, desc); | 545 compareTypes(resynthesized.type, original.type, desc); |
| 530 expect(resynthesized.typeParameters.length, original.typeParameters.length); | 546 expect(resynthesized.typeParameters.length, original.typeParameters.length); |
| 531 for (int i = 0; i < resynthesized.typeParameters.length; i++) { | 547 for (int i = 0; i < resynthesized.typeParameters.length; i++) { |
| 532 compareTypeParameterElements( | 548 compareTypeParameterElements( |
| 533 resynthesized.typeParameters[i], | 549 resynthesized.typeParameters[i], |
| 534 original.typeParameters[i], | 550 original.typeParameters[i], |
| 535 '$desc type parameter ${original.typeParameters[i].name}'); | 551 '$desc type parameter ${original.typeParameters[i].name}'); |
| 536 } | 552 } |
| 553 if (original is! Member) { | |
| 554 List<FunctionElement> rFunctions = resynthesized.functions; | |
| 555 List<FunctionElement> oFunctions = original.functions; | |
| 556 expect(rFunctions, hasLength(oFunctions.length)); | |
| 557 for (int i = 0; i < oFunctions.length; i++) { | |
| 558 compareFunctionElements(rFunctions[i], oFunctions[i], | |
| 559 '$desc local function ${oFunctions[i].name}'); | |
| 560 } | |
| 561 } | |
| 562 if (original is! Member) { | |
| 563 List<LocalVariableElement> rVariables = resynthesized.localVariables; | |
| 564 List<LocalVariableElement> oVariables = original.localVariables; | |
| 565 expect(rVariables, hasLength(oVariables.length)); | |
| 566 for (int i = 0; i < oVariables.length; i++) { | |
| 567 compareVariableElements(rVariables[i], oVariables[i], | |
| 568 '$desc local variable ${oVariables[i].name}'); | |
| 569 } | |
| 570 } | |
| 537 } | 571 } |
| 538 | 572 |
| 539 void compareExportElements(ExportElementImpl resynthesized, | 573 void compareExportElements(ExportElementImpl resynthesized, |
| 540 ExportElementImpl original, String desc) { | 574 ExportElementImpl original, String desc) { |
| 541 compareUriReferencedElements(resynthesized, original, desc); | 575 compareUriReferencedElements(resynthesized, original, desc); |
| 542 expect(resynthesized.exportedLibrary.location, | 576 expect(resynthesized.exportedLibrary.location, |
| 543 original.exportedLibrary.location); | 577 original.exportedLibrary.location); |
| 544 expect(resynthesized.combinators.length, original.combinators.length); | 578 expect(resynthesized.combinators.length, original.combinators.length); |
| 545 for (int i = 0; i < resynthesized.combinators.length; i++) { | 579 for (int i = 0; i < resynthesized.combinators.length; i++) { |
| 546 compareNamespaceCombinators( | 580 compareNamespaceCombinators( |
| 547 resynthesized.combinators[i], original.combinators[i]); | 581 resynthesized.combinators[i], original.combinators[i]); |
| 548 } | 582 } |
| 549 } | 583 } |
| 550 | 584 |
| 551 void compareFieldElements( | 585 void compareFieldElements( |
| 552 FieldElementImpl resynthesized, FieldElementImpl original, String desc) { | 586 FieldElementImpl resynthesized, FieldElementImpl original, String desc) { |
| 553 comparePropertyInducingElements(resynthesized, original, desc); | 587 comparePropertyInducingElements(resynthesized, original, desc); |
| 554 } | 588 } |
| 555 | 589 |
| 556 void compareFunctionElements( | 590 void compareFunctionElements( |
| 557 FunctionElement resynthesized, FunctionElement original, String desc) { | 591 FunctionElement resynthesized, FunctionElement original, String desc) { |
| 558 compareExecutableElements(resynthesized, original, desc); | 592 compareExecutableElements(resynthesized, original, desc); |
| 593 checkPossibleLocalElements(resynthesized, original); | |
| 559 } | 594 } |
| 560 | 595 |
| 561 void compareFunctionTypeAliasElements( | 596 void compareFunctionTypeAliasElements( |
| 562 FunctionTypeAliasElementImpl resynthesized, | 597 FunctionTypeAliasElementImpl resynthesized, |
| 563 FunctionTypeAliasElementImpl original, | 598 FunctionTypeAliasElementImpl original, |
| 564 String desc) { | 599 String desc) { |
| 565 compareElements(resynthesized, original, desc); | 600 compareElements(resynthesized, original, desc); |
| 566 expect(resynthesized.parameters.length, original.parameters.length); | 601 expect(resynthesized.parameters.length, original.parameters.length); |
| 567 for (int i = 0; i < resynthesized.parameters.length; i++) { | 602 for (int i = 0; i < resynthesized.parameters.length; i++) { |
| 568 compareParameterElements( | 603 compareParameterElements( |
| (...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 813 getActualElement(resynthesized, desc); | 848 getActualElement(resynthesized, desc); |
| 814 Expression initializer = resynthesizedActual.constantInitializer; | 849 Expression initializer = resynthesizedActual.constantInitializer; |
| 815 if (constantInitializersAreInvalid) { | 850 if (constantInitializersAreInvalid) { |
| 816 _assertUnresolvedIdentifier(initializer, desc); | 851 _assertUnresolvedIdentifier(initializer, desc); |
| 817 } else { | 852 } else { |
| 818 compareConstAsts(initializer, originalActual.constantInitializer, | 853 compareConstAsts(initializer, originalActual.constantInitializer, |
| 819 '$desc initializer'); | 854 '$desc initializer'); |
| 820 } | 855 } |
| 821 } | 856 } |
| 822 checkPossibleMember(resynthesized, original, desc); | 857 checkPossibleMember(resynthesized, original, desc); |
| 858 checkPossibleLocalElements(resynthesized, original); | |
| 823 } | 859 } |
| 824 | 860 |
| 825 /** | 861 /** |
| 826 * Serialize the given [library] into a summary. Then create a | 862 * Serialize the given [library] into a summary. Then create a |
| 827 * [_TestSummaryResynthesizer] which can deserialize it, along with any | 863 * [_TestSummaryResynthesizer] which can deserialize it, along with any |
| 828 * references it makes to `dart:core`. | 864 * references it makes to `dart:core`. |
| 829 * | 865 * |
| 830 * Errors will lead to a test failure unless [allowErrors] is `true`. | 866 * Errors will lead to a test failure unless [allowErrors] is `true`. |
| 831 */ | 867 */ |
| 832 _TestSummaryResynthesizer encodeLibrary(LibraryElementImpl library, | 868 _TestSummaryResynthesizer encodeLibrary(LibraryElementImpl library, |
| (...skipping 1773 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2606 } | 2642 } |
| 2607 | 2643 |
| 2608 test_library_name_with_spaces() { | 2644 test_library_name_with_spaces() { |
| 2609 checkLibrary('library foo . bar ;'); | 2645 checkLibrary('library foo . bar ;'); |
| 2610 } | 2646 } |
| 2611 | 2647 |
| 2612 test_library_named() { | 2648 test_library_named() { |
| 2613 checkLibrary('library foo.bar;'); | 2649 checkLibrary('library foo.bar;'); |
| 2614 } | 2650 } |
| 2615 | 2651 |
| 2652 test_localFuncctions_inMethod() { | |
|
Paul Berry
2016/02/11 21:15:46
s/Funcctions/Functions/
| |
| 2653 checkLibrary(r''' | |
| 2654 class C { | |
| 2655 m() { | |
| 2656 f() {} | |
| 2657 } | |
| 2658 } | |
| 2659 '''); | |
| 2660 } | |
| 2661 | |
| 2662 test_localFunctions() { | |
| 2663 checkLibrary(r''' | |
| 2664 f() { | |
| 2665 f1() {} | |
| 2666 { | |
| 2667 f2() {} | |
| 2668 } | |
| 2669 } | |
| 2670 '''); | |
| 2671 } | |
| 2672 | |
| 2673 test_localFunctions_inConstructor() { | |
| 2674 checkLibrary(r''' | |
| 2675 class C { | |
| 2676 C() { | |
| 2677 f() {} | |
| 2678 } | |
| 2679 } | |
| 2680 '''); | |
| 2681 } | |
| 2682 | |
| 2683 test_localFunctions_inTopLevelGetter() { | |
| 2684 checkLibrary(r''' | |
| 2685 get g { | |
| 2686 f() {} | |
| 2687 } | |
| 2688 '''); | |
| 2689 } | |
| 2690 | |
| 2691 test_localVariables_inConstructor() { | |
| 2692 checkLibrary(r''' | |
| 2693 class C { | |
| 2694 C() { | |
| 2695 int v; | |
| 2696 f() {} | |
| 2697 } | |
| 2698 } | |
| 2699 '''); | |
| 2700 } | |
| 2701 | |
| 2702 test_localVariables_inLocalFunction() { | |
| 2703 checkLibrary(r''' | |
| 2704 f() { | |
| 2705 f1() { | |
| 2706 int v1 = 1; | |
| 2707 } // 2 | |
| 2708 f2() { | |
| 2709 int v1 = 1; | |
| 2710 f3() { | |
| 2711 int v2 = 1; | |
| 2712 } | |
| 2713 } | |
| 2714 } | |
| 2715 '''); | |
| 2716 } | |
| 2717 | |
| 2718 test_localVariables_inMethod() { | |
| 2719 checkLibrary(r''' | |
| 2720 class C { | |
| 2721 m() { | |
| 2722 int v; | |
| 2723 } | |
| 2724 } | |
| 2725 '''); | |
| 2726 } | |
| 2727 | |
| 2728 test_localVariables_inTopLevelFunction() { | |
| 2729 checkLibrary(r''' | |
| 2730 main() { | |
| 2731 int v1 = 1; | |
| 2732 { | |
| 2733 const String v2 = 'bbb'; | |
| 2734 } | |
| 2735 Map<int, List<double>> v3; | |
| 2736 } | |
| 2737 '''); | |
| 2738 } | |
| 2739 | |
| 2740 test_localVariables_inTopLevelGetter() { | |
| 2741 checkLibrary(r''' | |
| 2742 get g { | |
| 2743 int v; | |
| 2744 } | |
| 2745 '''); | |
| 2746 } | |
| 2747 | |
| 2616 test_main_class() { | 2748 test_main_class() { |
| 2617 checkLibrary('class main {}'); | 2749 checkLibrary('class main {}'); |
| 2618 } | 2750 } |
| 2619 | 2751 |
| 2620 test_main_class_alias() { | 2752 test_main_class_alias() { |
| 2621 checkLibrary('class main = C with D; class C {} class D {}'); | 2753 checkLibrary('class main = C with D; class C {} class D {}'); |
| 2622 } | 2754 } |
| 2623 | 2755 |
| 2624 test_main_class_alias_via_export() { | 2756 test_main_class_alias_via_export() { |
| 2625 addLibrarySource('/a.dart', 'class main = C with D; class C {} class D {}'); | 2757 addLibrarySource('/a.dart', 'class main = C with D; class C {} class D {}'); |
| (...skipping 650 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3276 fail('Unexpectedly tried to get unlinked summary for $uri'); | 3408 fail('Unexpectedly tried to get unlinked summary for $uri'); |
| 3277 } | 3409 } |
| 3278 return serializedUnit; | 3410 return serializedUnit; |
| 3279 } | 3411 } |
| 3280 | 3412 |
| 3281 @override | 3413 @override |
| 3282 bool hasLibrarySummary(String uri) { | 3414 bool hasLibrarySummary(String uri) { |
| 3283 return true; | 3415 return true; |
| 3284 } | 3416 } |
| 3285 } | 3417 } |
| OLD | NEW |