| OLD | NEW |
| (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.summarize_elements_test; |
| 6 |
| 7 import 'package:analyzer/dart/element/element.dart'; |
| 8 import 'package:analyzer/src/generated/engine.dart'; |
| 9 import 'package:analyzer/src/generated/source.dart'; |
| 10 import 'package:analyzer/src/generated/source_io.dart'; |
| 11 import 'package:analyzer/src/summary/format.dart'; |
| 12 import 'package:analyzer/src/summary/public_namespace_computer.dart' |
| 13 as public_namespace; |
| 14 import 'package:analyzer/src/summary/summarize_elements.dart' |
| 15 as summarize_elements; |
| 16 import 'package:unittest/unittest.dart'; |
| 17 |
| 18 import '../../generated/resolver_test.dart'; |
| 19 import '../../reflective_tests.dart'; |
| 20 import 'summary_common.dart'; |
| 21 |
| 22 main() { |
| 23 groupSep = ' | '; |
| 24 runReflectiveTests(SummarizeElementsTest); |
| 25 } |
| 26 |
| 27 /** |
| 28 * Override of [SummaryTest] which creates summaries from the element model. |
| 29 */ |
| 30 @reflectiveTest |
| 31 class SummarizeElementsTest extends ResolverTestCase with SummaryTest { |
| 32 /** |
| 33 * The list of absolute unit URIs corresponding to the compilation units in |
| 34 * [unlinkedUnits]. |
| 35 */ |
| 36 List<String> unitUris; |
| 37 |
| 38 /** |
| 39 * Map containing all source files in this test, and their corresponding file |
| 40 * contents. |
| 41 */ |
| 42 final Map<Source, String> _fileContents = <Source, String>{}; |
| 43 |
| 44 @override |
| 45 LinkedLibrary linked; |
| 46 |
| 47 @override |
| 48 List<UnlinkedUnit> unlinkedUnits; |
| 49 |
| 50 @override |
| 51 bool get checkAstDerivedData => false; |
| 52 |
| 53 @override |
| 54 bool get expectAbsoluteUrisInDependencies => true; |
| 55 |
| 56 @override |
| 57 bool get skipFullyLinkedData => false; |
| 58 |
| 59 @override |
| 60 Source addNamedSource(String filePath, String contents) { |
| 61 Source source = super.addNamedSource(filePath, contents); |
| 62 _fileContents[source] = contents; |
| 63 return source; |
| 64 } |
| 65 |
| 66 /** |
| 67 * Serialize the library containing the given class [element], then |
| 68 * deserialize it and return the summary of the class. |
| 69 */ |
| 70 UnlinkedClass serializeClassElement(ClassElement element) { |
| 71 serializeLibraryElement(element.library); |
| 72 return findClass(element.name, failIfAbsent: true); |
| 73 } |
| 74 |
| 75 /** |
| 76 * Serialize the given [library] element, then deserialize it and store the |
| 77 * resulting summary in [linked] and [unlinkedUnits]. |
| 78 */ |
| 79 void serializeLibraryElement(LibraryElement library) { |
| 80 summarize_elements.LibrarySerializationResult serializedLib = |
| 81 summarize_elements.serializeLibrary(library, typeProvider); |
| 82 { |
| 83 List<int> buffer = serializedLib.linked.toBuffer(); |
| 84 linked = new LinkedLibrary.fromBuffer(buffer); |
| 85 } |
| 86 unlinkedUnits = serializedLib.unlinkedUnits.map((UnlinkedUnitBuilder b) { |
| 87 List<int> buffer = b.toBuffer(); |
| 88 return new UnlinkedUnit.fromBuffer(buffer); |
| 89 }).toList(); |
| 90 unitUris = serializedLib.unitUris; |
| 91 } |
| 92 |
| 93 @override |
| 94 void serializeLibraryText(String text, {bool allowErrors: false}) { |
| 95 Source source = addSource(text); |
| 96 _fileContents[source] = text; |
| 97 LibraryElement library = resolve2(source); |
| 98 if (!allowErrors) { |
| 99 assertNoErrors(source); |
| 100 } |
| 101 serializeLibraryElement(library); |
| 102 expect(unlinkedUnits[0].imports.length, linked.importDependencies.length); |
| 103 expect(linked.units.length, unlinkedUnits.length); |
| 104 for (int i = 0; i < linked.units.length; i++) { |
| 105 expect(unlinkedUnits[i].references.length, |
| 106 lessThanOrEqualTo(linked.units[i].references.length)); |
| 107 } |
| 108 verifyPublicNamespace(); |
| 109 } |
| 110 |
| 111 @override |
| 112 void setUp() { |
| 113 super.setUp(); |
| 114 AnalysisOptionsImpl options = new AnalysisOptionsImpl(); |
| 115 options.enableGenericMethods = true; |
| 116 resetWithOptions(options); |
| 117 } |
| 118 |
| 119 test_class_no_superclass() { |
| 120 UnlinkedClass cls = serializeClassElement(typeProvider.objectType.element); |
| 121 expect(cls.supertype, isNull); |
| 122 expect(cls.hasNoSupertype, isTrue); |
| 123 } |
| 124 |
| 125 /** |
| 126 * Verify that [public_namespace.computePublicNamespace] produces data that's |
| 127 * equivalent to that produced by [summarize_elements.serializeLibrary]. |
| 128 */ |
| 129 void verifyPublicNamespace() { |
| 130 for (int i = 0; i < unlinkedUnits.length; i++) { |
| 131 Source source = analysisContext.sourceFactory.forUri(unitUris[i]); |
| 132 String text = _fileContents[source]; |
| 133 if (text == null) { |
| 134 if (!allowMissingFiles) { |
| 135 fail('Could not find file while verifying public namespace: ' |
| 136 '${unitUris[i]}'); |
| 137 } |
| 138 } else { |
| 139 UnlinkedPublicNamespace namespace = |
| 140 computePublicNamespaceFromText(text, source); |
| 141 expect(canonicalize(namespace), |
| 142 canonicalize(unlinkedUnits[i].publicNamespace), |
| 143 reason: 'publicNamespace(${unitUris[i]})'); |
| 144 } |
| 145 } |
| 146 } |
| 147 } |
| OLD | NEW |