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