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 checkPossibleMember( |
| 135 Element resynthesized, Element original, String desc) { |
| 136 Element resynthesizedNonHandle = resynthesized is ElementHandle |
| 137 ? resynthesized.actualElement |
| 138 : resynthesized; |
| 139 if (original is Member) { |
| 140 expect(resynthesizedNonHandle, new isInstanceOf<Member>(), reason: desc); |
| 141 if (resynthesizedNonHandle is Member) { |
| 142 List<DartType> resynthesizedTypeArguments = |
| 143 resynthesizedNonHandle.definingType.typeArguments; |
| 144 List<DartType> originalTypeArguments = |
| 145 original.definingType.typeArguments; |
| 146 expect( |
| 147 resynthesizedTypeArguments, hasLength(originalTypeArguments.length), |
| 148 reason: desc); |
| 149 for (int i = 0; i < originalTypeArguments.length; i++) { |
| 150 compareTypeImpls(resynthesizedTypeArguments[i], |
| 151 originalTypeArguments[i], '$desc type argument $i'); |
| 152 } |
| 153 } |
| 154 } else { |
| 155 expect( |
| 156 resynthesizedNonHandle, isNot(new isInstanceOf<ConstructorMember>()), |
| 157 reason: desc); |
| 158 } |
| 159 } |
| 160 |
134 void compareClassElements( | 161 void compareClassElements( |
135 ClassElementImpl resynthesized, ClassElementImpl original, String desc) { | 162 ClassElementImpl resynthesized, ClassElementImpl original, String desc) { |
136 compareElements(resynthesized, original, desc); | 163 compareElements(resynthesized, original, desc); |
137 expect(resynthesized.fields.length, original.fields.length, | 164 expect(resynthesized.fields.length, original.fields.length, |
138 reason: '$desc fields.length'); | 165 reason: '$desc fields.length'); |
139 for (int i = 0; i < resynthesized.fields.length; i++) { | 166 for (int i = 0; i < resynthesized.fields.length; i++) { |
140 String name = original.fields[i].name; | 167 String name = original.fields[i].name; |
141 compareFieldElements( | 168 compareFieldElements( |
142 resynthesized.fields[i], original.fields[i], '$desc.field $name'); | 169 resynthesized.fields[i], original.fields[i], '$desc.field $name'); |
143 } | 170 } |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
365 compareConstAstLists( | 392 compareConstAstLists( |
366 r.typeArguments?.arguments, o.typeArguments?.arguments, desc); | 393 r.typeArguments?.arguments, o.typeArguments?.arguments, desc); |
367 compareConstAstLists(r.entries, o.entries, desc); | 394 compareConstAstLists(r.entries, o.entries, desc); |
368 } else if (o is InstanceCreationExpression && | 395 } else if (o is InstanceCreationExpression && |
369 r is InstanceCreationExpression) { | 396 r is InstanceCreationExpression) { |
370 compareElements(r.staticElement, o.staticElement, desc); | 397 compareElements(r.staticElement, o.staticElement, desc); |
371 ConstructorName oConstructor = o.constructorName; | 398 ConstructorName oConstructor = o.constructorName; |
372 ConstructorName rConstructor = r.constructorName; | 399 ConstructorName rConstructor = r.constructorName; |
373 expect(oConstructor, isNotNull, reason: desc); | 400 expect(oConstructor, isNotNull, reason: desc); |
374 expect(rConstructor, isNotNull, reason: desc); | 401 expect(rConstructor, isNotNull, reason: desc); |
375 compareElements( | 402 compareConstructorElements( |
376 rConstructor.staticElement, oConstructor.staticElement, desc); | 403 rConstructor.staticElement, oConstructor.staticElement, desc); |
377 TypeName oType = oConstructor.type; | 404 TypeName oType = oConstructor.type; |
378 TypeName rType = rConstructor.type; | 405 TypeName rType = rConstructor.type; |
379 expect(oType, isNotNull, reason: desc); | 406 expect(oType, isNotNull, reason: desc); |
380 expect(rType, isNotNull, reason: desc); | 407 expect(rType, isNotNull, reason: desc); |
381 compareConstAsts(rType.name, oType.name, desc); | 408 compareConstAsts(rType.name, oType.name, desc); |
382 compareConstAsts(rConstructor.name, oConstructor.name, desc); | 409 compareConstAsts(rConstructor.name, oConstructor.name, desc); |
383 compareConstAstLists(rType.typeArguments?.arguments, | 410 compareConstAstLists(rType.typeArguments?.arguments, |
384 oType.typeArguments?.arguments, desc); | 411 oType.typeArguments?.arguments, desc); |
385 compareConstAstLists( | 412 compareConstAstLists( |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
434 ConstructorElementImpl originalImpl = getActualElement(original, desc); | 461 ConstructorElementImpl originalImpl = getActualElement(original, desc); |
435 compareConstAstLists(resynthesizedImpl.constantInitializers, | 462 compareConstAstLists(resynthesizedImpl.constantInitializers, |
436 originalImpl.constantInitializers, desc); | 463 originalImpl.constantInitializers, desc); |
437 } | 464 } |
438 if (original.redirectedConstructor == null) { | 465 if (original.redirectedConstructor == null) { |
439 expect(resynthesized.redirectedConstructor, isNull, reason: desc); | 466 expect(resynthesized.redirectedConstructor, isNull, reason: desc); |
440 } else { | 467 } else { |
441 compareConstructorElements(resynthesized.redirectedConstructor, | 468 compareConstructorElements(resynthesized.redirectedConstructor, |
442 original.redirectedConstructor, '$desc redirectedConstructor'); | 469 original.redirectedConstructor, '$desc redirectedConstructor'); |
443 } | 470 } |
444 ConstructorElement resynthesizedNonHandle = | 471 checkPossibleMember(resynthesized, original, desc); |
445 resynthesized is ConstructorElementHandle | |
446 ? resynthesized.actualElement | |
447 : resynthesized; | |
448 if (original is ConstructorMember) { | |
449 expect(resynthesizedNonHandle, new isInstanceOf<ConstructorMember>(), | |
450 reason: desc); | |
451 if (resynthesizedNonHandle is ConstructorMember) { | |
452 List<DartType> resynthesizedTypeArguments = | |
453 resynthesizedNonHandle.definingType.typeArguments; | |
454 List<DartType> originalTypeArguments = | |
455 original.definingType.typeArguments; | |
456 expect( | |
457 resynthesizedTypeArguments, hasLength(originalTypeArguments.length), | |
458 reason: desc); | |
459 for (int i = 0; i < originalTypeArguments.length; i++) { | |
460 compareTypeImpls(resynthesizedTypeArguments[i], | |
461 originalTypeArguments[i], '$desc type argument $i'); | |
462 } | |
463 } | |
464 } else { | |
465 expect( | |
466 resynthesizedNonHandle, isNot(new isInstanceOf<ConstructorMember>()), | |
467 reason: desc); | |
468 } | |
469 } | 472 } |
470 | 473 |
471 void compareElementAnnotations(ElementAnnotationImpl resynthesized, | 474 void compareElementAnnotations(ElementAnnotationImpl resynthesized, |
472 ElementAnnotationImpl original, String desc) { | 475 ElementAnnotationImpl original, String desc) { |
473 expect(resynthesized.element, isNotNull, reason: desc); | 476 expect(resynthesized.element, isNotNull, reason: desc); |
474 expect(resynthesized.element.kind, original.element.kind, reason: desc); | 477 expect(resynthesized.element.kind, original.element.kind, reason: desc); |
475 expect(resynthesized.element.location, original.element.location, | 478 expect(resynthesized.element.location, original.element.location, |
476 reason: desc); | 479 reason: desc); |
477 expect(resynthesized.compilationUnit, isNotNull, reason: desc); | 480 expect(resynthesized.compilationUnit, isNotNull, reason: desc); |
478 expect(resynthesized.compilationUnit.location, | 481 expect(resynthesized.compilationUnit.location, |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
636 Map<String, Element> originalMap = original.definedNames; | 639 Map<String, Element> originalMap = original.definedNames; |
637 expect(resynthesizedMap.keys.toSet(), originalMap.keys.toSet(), | 640 expect(resynthesizedMap.keys.toSet(), originalMap.keys.toSet(), |
638 reason: desc); | 641 reason: desc); |
639 for (String key in originalMap.keys) { | 642 for (String key in originalMap.keys) { |
640 Element resynthesizedElement = resynthesizedMap[key]; | 643 Element resynthesizedElement = resynthesizedMap[key]; |
641 Element originalElement = originalMap[key]; | 644 Element originalElement = originalMap[key]; |
642 compareElements(resynthesizedElement, originalElement, key); | 645 compareElements(resynthesizedElement, originalElement, key); |
643 } | 646 } |
644 } | 647 } |
645 | 648 |
646 void compareParameterElements(ParameterElementImpl resynthesized, | 649 void compareParameterElements( |
647 ParameterElementImpl original, String desc) { | 650 ParameterElement resynthesized, ParameterElement original, String desc) { |
648 compareVariableElements(resynthesized, original, desc); | 651 compareVariableElements(resynthesized, original, desc); |
649 expect(resynthesized.parameters.length, original.parameters.length); | 652 expect(resynthesized.parameters.length, original.parameters.length); |
650 for (int i = 0; i < resynthesized.parameters.length; i++) { | 653 for (int i = 0; i < resynthesized.parameters.length; i++) { |
651 compareParameterElements( | 654 compareParameterElements( |
652 resynthesized.parameters[i], | 655 resynthesized.parameters[i], |
653 original.parameters[i], | 656 original.parameters[i], |
654 '$desc parameter ${original.parameters[i].name}'); | 657 '$desc parameter ${original.parameters[i].name}'); |
655 } | 658 } |
656 expect(resynthesized.parameterKind, original.parameterKind); | 659 expect(resynthesized.parameterKind, original.parameterKind); |
657 expect(resynthesized.isInitializingFormal, original.isInitializingFormal, | 660 expect(resynthesized.isInitializingFormal, original.isInitializingFormal, |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
793 } | 796 } |
794 | 797 |
795 void compareUriReferencedElements(UriReferencedElementImpl resynthesized, | 798 void compareUriReferencedElements(UriReferencedElementImpl resynthesized, |
796 UriReferencedElementImpl original, String desc) { | 799 UriReferencedElementImpl original, String desc) { |
797 compareElements(resynthesized, original, desc); | 800 compareElements(resynthesized, original, desc); |
798 expect(resynthesized.uri, original.uri); | 801 expect(resynthesized.uri, original.uri); |
799 expect(resynthesized.uriOffset, original.uriOffset, reason: desc); | 802 expect(resynthesized.uriOffset, original.uriOffset, reason: desc); |
800 expect(resynthesized.uriEnd, original.uriEnd, reason: desc); | 803 expect(resynthesized.uriEnd, original.uriEnd, reason: desc); |
801 } | 804 } |
802 | 805 |
803 void compareVariableElements(VariableElementImpl resynthesized, | 806 void compareVariableElements( |
804 VariableElementImpl original, String desc) { | 807 VariableElement resynthesized, VariableElement original, String desc) { |
805 compareElements(resynthesized, original, desc); | 808 compareElements(resynthesized, original, desc); |
806 compareTypes(resynthesized.type, original.type, desc); | 809 compareTypes(resynthesized.type, original.type, desc); |
807 if (original is ConstVariableElement) { | 810 VariableElementImpl originalActual = getActualElement(original, desc); |
808 Expression initializer = resynthesized.constantInitializer; | 811 if (originalActual is ConstVariableElement) { |
| 812 VariableElementImpl resynthesizedActual = |
| 813 getActualElement(resynthesized, desc); |
| 814 Expression initializer = resynthesizedActual.constantInitializer; |
809 if (constantInitializersAreInvalid) { | 815 if (constantInitializersAreInvalid) { |
810 _assertUnresolvedIdentifier(initializer, desc); | 816 _assertUnresolvedIdentifier(initializer, desc); |
811 } else { | 817 } else { |
812 compareConstAsts(initializer, original.constantInitializer, desc); | 818 compareConstAsts(initializer, originalActual.constantInitializer, desc); |
813 } | 819 } |
814 } | 820 } |
| 821 checkPossibleMember(resynthesized, original, desc); |
815 } | 822 } |
816 | 823 |
817 /** | 824 /** |
818 * Serialize the given [library] into a summary. Then create a | 825 * Serialize the given [library] into a summary. Then create a |
819 * [_TestSummaryResynthesizer] which can deserialize it, along with any | 826 * [_TestSummaryResynthesizer] which can deserialize it, along with any |
820 * references it makes to `dart:core`. | 827 * references it makes to `dart:core`. |
821 * | 828 * |
822 * Errors will lead to a test failure unless [allowErrors] is `true`. | 829 * Errors will lead to a test failure unless [allowErrors] is `true`. |
823 */ | 830 */ |
824 _TestSummaryResynthesizer encodeLibrary(LibraryElementImpl library, | 831 _TestSummaryResynthesizer encodeLibrary(LibraryElementImpl library, |
(...skipping 2439 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3264 fail('Unexpectedly tried to get unlinked summary for $uri'); | 3271 fail('Unexpectedly tried to get unlinked summary for $uri'); |
3265 } | 3272 } |
3266 return serializedUnit; | 3273 return serializedUnit; |
3267 } | 3274 } |
3268 | 3275 |
3269 @override | 3276 @override |
3270 bool hasLibrarySummary(String uri) { | 3277 bool hasLibrarySummary(String uri) { |
3271 return true; | 3278 return true; |
3272 } | 3279 } |
3273 } | 3280 } |
OLD | NEW |