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

Side by Side Diff: pkg/analyzer/test/src/summary/resynthesize_test.dart

Issue 1647553002: Add the ability to resynthesize class members from summaries. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 10 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
« no previous file with comments | « pkg/analyzer/lib/src/summary/resynthesize.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 'package:analyzer/dart/element/element.dart'; 7 import 'package:analyzer/dart/element/element.dart';
8 import 'package:analyzer/dart/element/type.dart'; 8 import 'package:analyzer/dart/element/type.dart';
9 import 'package:analyzer/src/dart/element/element.dart'; 9 import 'package:analyzer/src/dart/element/element.dart';
10 import 'package:analyzer/src/dart/element/type.dart'; 10 import 'package:analyzer/src/dart/element/type.dart';
(...skipping 29 matching lines...) Expand all
40 otherLibrarySources.add(analysisContext2.sourceFactory.forUri(uri)); 40 otherLibrarySources.add(analysisContext2.sourceFactory.forUri(uri));
41 } 41 }
42 42
43 void addLibrarySource(String filePath, String contents) { 43 void addLibrarySource(String filePath, String contents) {
44 otherLibrarySources.add(addNamedSource(filePath, contents)); 44 otherLibrarySources.add(addNamedSource(filePath, contents));
45 } 45 }
46 46
47 void checkLibrary(String text, {bool allowErrors: false}) { 47 void checkLibrary(String text, {bool allowErrors: false}) {
48 Source source = addSource(text); 48 Source source = addSource(text);
49 LibraryElementImpl original = resolve2(source); 49 LibraryElementImpl original = resolve2(source);
50 LibraryElementImpl resynthesized = 50 LibraryElementImpl resynthesized = resynthesizeLibraryElement(
51 resynthesizeLibrary(source, original, allowErrors); 51 encodeLibrary(source, original, allowErrors: allowErrors),
52 source.uri.toString());
52 checkLibraryElements(original, resynthesized); 53 checkLibraryElements(original, resynthesized);
53 } 54 }
54 55
55 void checkLibraryElements( 56 void checkLibraryElements(
56 LibraryElementImpl original, LibraryElementImpl resynthesized) { 57 LibraryElementImpl original, LibraryElementImpl resynthesized) {
57 compareElements(resynthesized, original, '(library)'); 58 compareElements(resynthesized, original, '(library)');
58 expect(resynthesized.displayName, original.displayName); 59 expect(resynthesized.displayName, original.displayName);
59 expect(original.enclosingElement, isNull); 60 expect(original.enclosingElement, isNull);
60 expect(resynthesized.enclosingElement, isNull); 61 expect(resynthesized.enclosingElement, isNull);
61 expect(resynthesized.hasExtUri, original.hasExtUri); 62 expect(resynthesized.hasExtUri, original.hasExtUri);
(...skipping 442 matching lines...) Expand 10 before | Expand all | Expand 10 after
504 expect(resynthesized.uriEnd, original.uriEnd, reason: desc); 505 expect(resynthesized.uriEnd, original.uriEnd, reason: desc);
505 } 506 }
506 507
507 void compareVariableElements(VariableElementImpl resynthesized, 508 void compareVariableElements(VariableElementImpl resynthesized,
508 VariableElementImpl original, String desc) { 509 VariableElementImpl original, String desc) {
509 compareElements(resynthesized, original, desc); 510 compareElements(resynthesized, original, desc);
510 compareTypes(resynthesized.type, original.type, desc); 511 compareTypes(resynthesized.type, original.type, desc);
511 // TODO(paulberry): test initializer 512 // TODO(paulberry): test initializer
512 } 513 }
513 514
514 fail_library_hasExtUri() { 515 /**
515 checkLibrary('import "dart-ext:doesNotExist.dart";'); 516 * Convert the given [source] into an element model and then serialize it
scheglov 2016/01/27 19:34:54 I don't quite understand this comment or the code.
Paul Berry 2016/01/27 20:34:41 You're right, this doesn't make sense. I've rewri
516 } 517 * into a summary. Then create a [_TestSummaryResynthesizer] which can
517 518 * deserialize it.
518 ElementImpl getActualElement(Element element, String desc) { 519 */
519 if (element is ElementHandle) { 520 _TestSummaryResynthesizer encodeLibrary(
520 return element.actualElement; 521 Source source, LibraryElementImpl original,
521 } else if (element is ElementImpl) { 522 {bool allowErrors: false}) {
522 return element;
523 } else {
524 fail('Unexpected type for resynthesized ($desc):'
525 ' ${element.runtimeType}');
526 return null;
527 }
528 }
529
530 LibraryElementImpl resynthesizeLibrary(
531 Source source, LibraryElementImpl original, bool allowErrors) {
532 if (!allowErrors) { 523 if (!allowErrors) {
533 assertNoErrors(source); 524 assertNoErrors(source);
534 } 525 }
535 String uri = source.uri.toString(); 526 String uri = source.uri.toString();
536 addLibrary('dart:core'); 527 addLibrary('dart:core');
537 return resynthesizeLibraryElement(uri, original); 528 return encodeLibraryElement(uri, original);
538 } 529 }
539 530
540 LibraryElementImpl resynthesizeLibraryElement( 531 /**
532 * Convert the library element [original], which resides at [uri], into a
533 * summary, and then create a [_TestSummaryResynthesizer] which can
534 * deserialize it.
535 */
536 _TestSummaryResynthesizer encodeLibraryElement(
541 String uri, LibraryElementImpl original) { 537 String uri, LibraryElementImpl original) {
542 Map<String, UnlinkedUnit> unlinkedSummaries = <String, UnlinkedUnit>{}; 538 Map<String, UnlinkedUnit> unlinkedSummaries = <String, UnlinkedUnit>{};
543 LinkedLibrary getLinkedSummaryFor(LibraryElement lib) { 539 LinkedLibrary getLinkedSummaryFor(LibraryElement lib) {
544 LibrarySerializationResult serialized = serializeLibrary( 540 LibrarySerializationResult serialized = serializeLibrary(
545 lib, typeProvider, analysisContext.analysisOptions.strongMode); 541 lib, typeProvider, analysisContext.analysisOptions.strongMode);
546 for (int i = 0; i < serialized.unlinkedUnits.length; i++) { 542 for (int i = 0; i < serialized.unlinkedUnits.length; i++) {
547 unlinkedSummaries[serialized.unitUris[i]] = 543 unlinkedSummaries[serialized.unitUris[i]] =
548 new UnlinkedUnit.fromBuffer(serialized.unlinkedUnits[i].toBuffer()); 544 new UnlinkedUnit.fromBuffer(serialized.unlinkedUnits[i].toBuffer());
549 } 545 }
550 return new LinkedLibrary.fromBuffer(serialized.linked.toBuffer()); 546 return new LinkedLibrary.fromBuffer(serialized.linked.toBuffer());
551 } 547 }
552 Map<String, LinkedLibrary> linkedSummaries = <String, LinkedLibrary>{ 548 Map<String, LinkedLibrary> linkedSummaries = <String, LinkedLibrary>{
553 uri: getLinkedSummaryFor(original) 549 uri: getLinkedSummaryFor(original)
554 }; 550 };
555 for (Source source in otherLibrarySources) { 551 for (Source source in otherLibrarySources) {
556 LibraryElement original = resolve2(source); 552 LibraryElement original = resolve2(source);
557 String uri = source.uri.toString(); 553 String uri = source.uri.toString();
558 linkedSummaries[uri] = getLinkedSummaryFor(original); 554 linkedSummaries[uri] = getLinkedSummaryFor(original);
559 } 555 }
560 _TestSummaryResynthesizer resynthesizer = new _TestSummaryResynthesizer( 556 return new _TestSummaryResynthesizer(
561 null, 557 null,
562 analysisContext, 558 analysisContext,
563 analysisContext.typeProvider, 559 analysisContext.typeProvider,
564 analysisContext.sourceFactory, 560 analysisContext.sourceFactory,
565 unlinkedSummaries, 561 unlinkedSummaries,
566 linkedSummaries, 562 linkedSummaries,
567 options.strongMode); 563 options.strongMode);
564 }
565
566 fail_library_hasExtUri() {
567 checkLibrary('import "dart-ext:doesNotExist.dart";');
568 }
569
570 ElementImpl getActualElement(Element element, String desc) {
571 if (element is ElementHandle) {
572 return element.actualElement;
573 } else if (element is ElementImpl) {
574 return element;
575 } else {
576 fail('Unexpected type for resynthesized ($desc):'
577 ' ${element.runtimeType}');
578 return null;
579 }
580 }
581
582 /**
583 * Resynthesize the library element associated with [uri] using
584 * [resynthesizer], and verify that it only had to consult one summary in
585 * order to do so.
586 */
587 LibraryElementImpl resynthesizeLibraryElement(
588 _TestSummaryResynthesizer resynthesizer, String uri) {
568 LibraryElementImpl resynthesized = resynthesizer.getLibraryElement(uri); 589 LibraryElementImpl resynthesized = resynthesizer.getLibraryElement(uri);
569 // Check that no other summaries needed to be resynthesized to resynthesize 590 // Check that no other summaries needed to be resynthesized to resynthesize
570 // the library element. 591 // the library element.
571 expect(resynthesizer.resynthesisCount, 1); 592 expect(resynthesizer.resynthesisCount, 1);
572 return resynthesized; 593 return resynthesized;
573 } 594 }
574 595
575 @override 596 @override
576 void setUp() { 597 void setUp() {
577 super.setUp(); 598 super.setUp();
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after
898 */ 919 */
899 C(); 920 C();
900 }'''); 921 }''');
901 } 922 }
902 923
903 test_core() { 924 test_core() {
904 String uri = 'dart:core'; 925 String uri = 'dart:core';
905 LibraryElementImpl original = 926 LibraryElementImpl original =
906 resolve2(analysisContext2.sourceFactory.forUri(uri)); 927 resolve2(analysisContext2.sourceFactory.forUri(uri));
907 LibraryElementImpl resynthesized = 928 LibraryElementImpl resynthesized =
908 resynthesizeLibraryElement(uri, original); 929 resynthesizeLibraryElement(encodeLibraryElement(uri, original), uri);
909 checkLibraryElements(original, resynthesized); 930 checkLibraryElements(original, resynthesized);
910 } 931 }
911 932
912 test_enum_documented() { 933 test_enum_documented() {
913 checkLibrary(''' 934 checkLibrary('''
914 // Extra comment so doc comment offset != 0 935 // Extra comment so doc comment offset != 0
915 /** 936 /**
916 * Docs 937 * Docs
917 */ 938 */
918 enum E { v }'''); 939 enum E { v }''');
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after
1160 1181
1161 test_function_type_parameter_with_function_typed_parameter() { 1182 test_function_type_parameter_with_function_typed_parameter() {
1162 resetWithOptions(new AnalysisOptionsImpl()..enableGenericMethods = true); 1183 resetWithOptions(new AnalysisOptionsImpl()..enableGenericMethods = true);
1163 checkLibrary('void f<T, U>(T x(U u)) {}'); 1184 checkLibrary('void f<T, U>(T x(U u)) {}');
1164 } 1185 }
1165 1186
1166 test_functions() { 1187 test_functions() {
1167 checkLibrary('f() {} g() {}'); 1188 checkLibrary('f() {} g() {}');
1168 } 1189 }
1169 1190
1191 test_getElement_constructor_named() {
1192 ConstructorElement original = resolve2(addSource('class C { C.named(); }'))
1193 .getType('C')
1194 .getNamedConstructor('named');
1195 expect(original, isNotNull);
1196 ConstructorElement resynthesized = validateGetElement(original);
1197 compareConstructorElements(resynthesized, original, 'C.constructor named');
1198 }
1199
1200 test_getElement_constructor_unnamed() {
1201 ConstructorElement original =
1202 resolve2(addSource('class C { C(); }')).getType('C').unnamedConstructor;
1203 expect(original, isNotNull);
1204 ConstructorElement resynthesized = validateGetElement(original);
1205 compareConstructorElements(resynthesized, original, 'C.constructor');
1206 }
1207
1208 test_getElement_field() {
1209 FieldElement original =
1210 resolve2(addSource('class C { var f; }')).getType('C').getField('f');
1211 expect(original, isNotNull);
1212 FieldElement resynthesized = validateGetElement(original);
1213 compareFieldElements(resynthesized, original, 'C.field f');
1214 }
1215
1216 test_getElement_getter() {
1217 PropertyAccessorElement original =
1218 resolve2(addSource('class C { get f => null; }'))
1219 .getType('C')
1220 .getGetter('f');
1221 expect(original, isNotNull);
1222 PropertyAccessorElement resynthesized = validateGetElement(original);
1223 comparePropertyAccessorElements(resynthesized, original, 'C.getter f');
1224 }
1225
1226 test_getElement_method() {
1227 MethodElement original =
1228 resolve2(addSource('class C { f() {} }')).getType('C').getMethod('f');
1229 expect(original, isNotNull);
1230 MethodElement resynthesized = validateGetElement(original);
1231 compareMethodElements(resynthesized, original, 'C.method f');
1232 }
1233
1234 test_getElement_operator() {
1235 MethodElement original =
1236 resolve2(addSource('class C { operator+(x) => null; }'))
1237 .getType('C')
1238 .getMethod('+');
1239 expect(original, isNotNull);
1240 MethodElement resynthesized = validateGetElement(original);
1241 compareMethodElements(resynthesized, original, 'C.operator+');
1242 }
1243
1244 test_getElement_setter() {
1245 PropertyAccessorElement original =
1246 resolve2(addSource('class C { void set f(value) {} }'))
1247 .getType('C')
1248 .getSetter('f');
1249 expect(original, isNotNull);
1250 PropertyAccessorElement resynthesized = validateGetElement(original);
1251 comparePropertyAccessorElements(resynthesized, original, 'C.setter f');
1252 }
1253
1170 test_getter_documented() { 1254 test_getter_documented() {
1171 checkLibrary(''' 1255 checkLibrary('''
1172 // Extra comment so doc comment offset != 0 1256 // Extra comment so doc comment offset != 0
1173 /** 1257 /**
1174 * Docs 1258 * Docs
1175 */ 1259 */
1176 get x => null;'''); 1260 get x => null;''');
1177 } 1261 }
1178 1262
1179 test_getter_external() { 1263 test_getter_external() {
(...skipping 532 matching lines...) Expand 10 before | Expand all | Expand 10 after
1712 1796
1713 test_variable_setterInPart_getterInPart() { 1797 test_variable_setterInPart_getterInPart() {
1714 addNamedSource('/a.dart', 'part of my.lib; void set x(int _) {}'); 1798 addNamedSource('/a.dart', 'part of my.lib; void set x(int _) {}');
1715 addNamedSource('/b.dart', 'part of my.lib; int get x => 42;'); 1799 addNamedSource('/b.dart', 'part of my.lib; int get x => 42;');
1716 checkLibrary('library my.lib; part "a.dart"; part "b.dart";'); 1800 checkLibrary('library my.lib; part "a.dart"; part "b.dart";');
1717 } 1801 }
1718 1802
1719 test_variables() { 1803 test_variables() {
1720 checkLibrary('int i; int j;'); 1804 checkLibrary('int i; int j;');
1721 } 1805 }
1806
1807 /**
1808 * Encode the library containing [original] into a summary and then use
1809 * [_TestSummaryResynthesizer.getElement] to retrieve just the original
1810 * element from the resynthesized summary.
1811 */
1812 Element validateGetElement(Element original) {
1813 LibraryElement originalLibrary = original.library;
1814 Source source = originalLibrary.source;
1815 _TestSummaryResynthesizer resynthesizer =
1816 encodeLibrary(source, originalLibrary);
scheglov 2016/01/27 19:34:54 Do we expect passing other Source than library.sou
Paul Berry 2016/01/27 20:34:41 Acknowledged.
1817 ElementLocationImpl location = original.location;
1818 Element result = resynthesizer.getElement(location);
1819 // Check that no other summaries needed to be resynthesized to resynthesize
1820 // the library element.
1821 expect(resynthesizer.resynthesisCount, 1);
1822 expect(result.location, location);
1823 return result;
1824 }
1722 } 1825 }
1723 1826
1724 class _TestSummaryResynthesizer extends SummaryResynthesizer { 1827 class _TestSummaryResynthesizer extends SummaryResynthesizer {
1725 final Map<String, UnlinkedUnit> unlinkedSummaries; 1828 final Map<String, UnlinkedUnit> unlinkedSummaries;
1726 final Map<String, LinkedLibrary> linkedSummaries; 1829 final Map<String, LinkedLibrary> linkedSummaries;
1727 1830
1728 _TestSummaryResynthesizer( 1831 _TestSummaryResynthesizer(
1729 SummaryResynthesizer parent, 1832 SummaryResynthesizer parent,
1730 AnalysisContext context, 1833 AnalysisContext context,
1731 TypeProvider typeProvider, 1834 TypeProvider typeProvider,
(...skipping 19 matching lines...) Expand all
1751 fail('Unexpectedly tried to get unlinked summary for $uri'); 1854 fail('Unexpectedly tried to get unlinked summary for $uri');
1752 } 1855 }
1753 return serializedUnit; 1856 return serializedUnit;
1754 } 1857 }
1755 1858
1756 @override 1859 @override
1757 bool hasLibrarySummary(String uri) { 1860 bool hasLibrarySummary(String uri) {
1758 return true; 1861 return true;
1759 } 1862 }
1760 } 1863 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/summary/resynthesize.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698