| 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 analyzer.test.src.summary.summary_test; | 5 library analyzer.test.src.summary.summary_test; |
| 6 | 6 |
| 7 import 'package:analyzer/dart/element/element.dart'; | 7 import 'package:analyzer/dart/element/element.dart'; |
| 8 import 'package:analyzer/src/generated/ast.dart'; |
| 8 import 'package:analyzer/src/generated/engine.dart'; | 9 import 'package:analyzer/src/generated/engine.dart'; |
| 10 import 'package:analyzer/src/generated/error.dart'; |
| 9 import 'package:analyzer/src/generated/java_engine_io.dart'; | 11 import 'package:analyzer/src/generated/java_engine_io.dart'; |
| 12 import 'package:analyzer/src/generated/parser.dart'; |
| 13 import 'package:analyzer/src/generated/scanner.dart'; |
| 10 import 'package:analyzer/src/generated/source.dart'; | 14 import 'package:analyzer/src/generated/source.dart'; |
| 11 import 'package:analyzer/src/summary/builder.dart'; | 15 import 'package:analyzer/src/generated/source_io.dart'; |
| 16 import 'package:analyzer/src/summary/base.dart'; |
| 12 import 'package:analyzer/src/summary/format.dart'; | 17 import 'package:analyzer/src/summary/format.dart'; |
| 18 import 'package:analyzer/src/summary/public_namespace_computer.dart' |
| 19 as public_namespace; |
| 13 import 'package:analyzer/src/summary/summarize_elements.dart' | 20 import 'package:analyzer/src/summary/summarize_elements.dart' |
| 14 as summarize_elements; | 21 as summarize_elements; |
| 15 import 'package:unittest/unittest.dart'; | 22 import 'package:unittest/unittest.dart'; |
| 16 | 23 |
| 17 import '../../generated/resolver_test.dart'; | 24 import '../../generated/resolver_test.dart'; |
| 18 import '../../reflective_tests.dart'; | 25 import '../../reflective_tests.dart'; |
| 19 | 26 |
| 20 main() { | 27 main() { |
| 21 groupSep = ' | '; | 28 groupSep = ' | '; |
| 22 runReflectiveTests(SummarizeElementsTest); | 29 runReflectiveTests(SummarizeElementsTest); |
| 23 } | 30 } |
| 24 | 31 |
| 25 /** | 32 /** |
| 26 * Override of [SummaryTest] which creates summaries from the element model. | 33 * Override of [SummaryTest] which creates summaries from the element model. |
| 27 */ | 34 */ |
| 28 @reflectiveTest | 35 @reflectiveTest |
| 29 class SummarizeElementsTest extends ResolverTestCase with SummaryTest { | 36 class SummarizeElementsTest extends ResolverTestCase with SummaryTest { |
| 37 final BuilderContext builderContext = new BuilderContext(); |
| 38 |
| 39 /** |
| 40 * The list of absolute unit URIs corresponding to the compilation units in |
| 41 * [unlinkedUnits]. |
| 42 */ |
| 43 List<String> unitUris; |
| 44 |
| 30 @override | 45 @override |
| 31 bool get checkAstDerivedData => false; | 46 bool get checkAstDerivedData => false; |
| 32 | 47 |
| 33 /** | 48 /** |
| 49 * Convert a summary object (or a portion of one) into a canonical form that |
| 50 * can be easily compared using [expect]. If [orderByName] is true, and the |
| 51 * object is a [List], it is sorted by the `name` field of its elements. |
| 52 */ |
| 53 Object canonicalize(Object obj, {bool orderByName: false}) { |
| 54 if (obj is SummaryClass) { |
| 55 Map<String, Object> result = <String, Object>{}; |
| 56 obj.toMap().forEach((String key, Object value) { |
| 57 bool orderByName = false; |
| 58 if (obj is UnlinkedPublicNamespace && key == 'names') { |
| 59 orderByName = true; |
| 60 } |
| 61 result[key] = canonicalize(value, orderByName: orderByName); |
| 62 }); |
| 63 return result; |
| 64 } else if (obj is List) { |
| 65 List<Object> result = <Object>[]; |
| 66 for (Object item in obj) { |
| 67 result.add(canonicalize(item)); |
| 68 } |
| 69 if (orderByName) { |
| 70 result.sort((Object a, Object b) { |
| 71 if (a is Map && b is Map) { |
| 72 return Comparable.compare(a['name'], b['name']); |
| 73 } else { |
| 74 return 0; |
| 75 } |
| 76 }); |
| 77 } |
| 78 return result; |
| 79 } else { |
| 80 return obj; |
| 81 } |
| 82 } |
| 83 |
| 84 /** |
| 34 * Serialize the library containing the given class [element], then | 85 * Serialize the library containing the given class [element], then |
| 35 * deserialize it and return the summary of the class. | 86 * deserialize it and return the summary of the class. |
| 36 */ | 87 */ |
| 37 UnlinkedClass serializeClassElement(ClassElement element) { | 88 UnlinkedClass serializeClassElement(ClassElement element) { |
| 38 serializeLibraryElement(element.library); | 89 serializeLibraryElement(element.library); |
| 39 return findClass(element.name, failIfAbsent: true); | 90 return findClass(element.name, failIfAbsent: true); |
| 40 } | 91 } |
| 41 | 92 |
| 42 /** | 93 /** |
| 43 * Serialize the given [library] element, then deserialize it and store the | 94 * Serialize the given [library] element, then deserialize it and store the |
| 44 * resulting summary in [lib]. | 95 * resulting summary in [prelinked] and [unlinkedUnits]. |
| 45 */ | 96 */ |
| 46 void serializeLibraryElement(LibraryElement library) { | 97 void serializeLibraryElement(LibraryElement library) { |
| 47 BuilderContext builderContext = new BuilderContext(); | |
| 48 summarize_elements.LibrarySerializationResult serializedLib = | 98 summarize_elements.LibrarySerializationResult serializedLib = |
| 49 summarize_elements.serializeLibrary( | 99 summarize_elements.serializeLibrary( |
| 50 builderContext, library, typeProvider); | 100 builderContext, library, typeProvider); |
| 51 prelinked = | 101 prelinked = |
| 52 new PrelinkedLibrary.fromBuffer(serializedLib.prelinked.toBuffer()); | 102 new PrelinkedLibrary.fromBuffer(serializedLib.prelinked.toBuffer()); |
| 53 unlinkedUnits = serializedLib.unlinkedUnits | 103 unlinkedUnits = serializedLib.unlinkedUnits |
| 54 .map((UnlinkedUnitBuilder b) => | 104 .map((UnlinkedUnitBuilder b) => |
| 55 new UnlinkedUnit.fromBuffer(b.toBuffer())) | 105 new UnlinkedUnit.fromBuffer(b.toBuffer())) |
| 56 .toList(); | 106 .toList(); |
| 107 unitUris = serializedLib.unitUris; |
| 57 } | 108 } |
| 58 | 109 |
| 59 @override | 110 @override |
| 60 void serializeLibraryText(String text, {bool allowErrors: false}) { | 111 void serializeLibraryText(String text, {bool allowErrors: false}) { |
| 61 Source source = addSource(text); | 112 Source source = addSource(text); |
| 62 LibraryElement library = resolve2(source); | 113 LibraryElement library = resolve2(source); |
| 63 if (!allowErrors) { | 114 if (!allowErrors) { |
| 64 assertNoErrors(source); | 115 assertNoErrors(source); |
| 65 } | 116 } |
| 66 serializeLibraryElement(library); | 117 serializeLibraryElement(library); |
| 67 expect( | 118 expect( |
| 68 unlinkedUnits[0].imports.length, prelinked.importDependencies.length); | 119 unlinkedUnits[0].imports.length, prelinked.importDependencies.length); |
| 69 expect(prelinked.units.length, unlinkedUnits.length); | 120 expect(prelinked.units.length, unlinkedUnits.length); |
| 70 for (int i = 0; i < prelinked.units.length; i++) { | 121 for (int i = 0; i < prelinked.units.length; i++) { |
| 71 expect(unlinkedUnits[i].references.length, | 122 expect(unlinkedUnits[i].references.length, |
| 72 prelinked.units[i].references.length); | 123 prelinked.units[i].references.length); |
| 73 } | 124 } |
| 125 verifyPublicNamespace(); |
| 74 } | 126 } |
| 75 | 127 |
| 76 @override | 128 @override |
| 77 void setUp() { | 129 void setUp() { |
| 78 super.setUp(); | 130 super.setUp(); |
| 79 AnalysisOptionsImpl options = new AnalysisOptionsImpl(); | 131 AnalysisOptionsImpl options = new AnalysisOptionsImpl(); |
| 80 options.enableGenericMethods = true; | 132 options.enableGenericMethods = true; |
| 81 resetWithOptions(options); | 133 resetWithOptions(options); |
| 82 } | 134 } |
| 83 | 135 |
| 84 test_class_no_superclass() { | 136 test_class_no_superclass() { |
| 85 UnlinkedClass cls = serializeClassElement(typeProvider.objectType.element); | 137 UnlinkedClass cls = serializeClassElement(typeProvider.objectType.element); |
| 86 expect(cls.supertype, isNull); | 138 expect(cls.supertype, isNull); |
| 87 expect(cls.hasNoSupertype, isTrue); | 139 expect(cls.hasNoSupertype, isTrue); |
| 88 } | 140 } |
| 141 |
| 142 /** |
| 143 * Verify that [public_namespace.computePublicNamespace] produces data that's |
| 144 * equivalent to that produced by [summarize_elements.serializeLibrary]. |
| 145 */ |
| 146 void verifyPublicNamespace() { |
| 147 for (int i = 0; i < unlinkedUnits.length; i++) { |
| 148 Source source = analysisContext.sourceFactory.forUri(unitUris[i]); |
| 149 String text = analysisContext.getContents(source).data; |
| 150 CharacterReader reader = new CharSequenceReader(text); |
| 151 Scanner scanner = |
| 152 new Scanner(source, reader, AnalysisErrorListener.NULL_LISTENER); |
| 153 Parser parser = new Parser(source, AnalysisErrorListener.NULL_LISTENER); |
| 154 CompilationUnit unit = parser.parseCompilationUnit(scanner.tokenize()); |
| 155 UnlinkedPublicNamespace namespace = |
| 156 new UnlinkedPublicNamespace.fromBuffer(public_namespace |
| 157 .computePublicNamespace(builderContext, unit) |
| 158 .toBuffer()); |
| 159 expect(canonicalize(namespace), |
| 160 canonicalize(unlinkedUnits[i].publicNamespace), |
| 161 reason: 'publicNamespace(${unitUris[i]})'); |
| 162 } |
| 163 } |
| 89 } | 164 } |
| 90 | 165 |
| 91 /** | 166 /** |
| 92 * Base class containing most summary tests. This allows summary tests to be | 167 * Base class containing most summary tests. This allows summary tests to be |
| 93 * re-used to exercise all the different ways in which summaries can be | 168 * re-used to exercise all the different ways in which summaries can be |
| 94 * generated (e.g. direct from the AST, from the element model, from a | 169 * generated (e.g. direct from the AST, from the element model, from a |
| 95 * "relinking" process, etc.) | 170 * "relinking" process, etc.) |
| 96 */ | 171 */ |
| 97 abstract class SummaryTest { | 172 abstract class SummaryTest { |
| 98 /** | 173 /** |
| (...skipping 2154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2253 UnlinkedVariable variable = | 2328 UnlinkedVariable variable = |
| 2254 serializeVariableText('int i;', variableName: 'i'); | 2329 serializeVariableText('int i;', variableName: 'i'); |
| 2255 checkTypeRef(variable.type, 'dart:core', 'dart:core', 'int'); | 2330 checkTypeRef(variable.type, 'dart:core', 'dart:core', 'int'); |
| 2256 } | 2331 } |
| 2257 | 2332 |
| 2258 test_varible_private() { | 2333 test_varible_private() { |
| 2259 serializeVariableText('int _i;', variableName: '_i'); | 2334 serializeVariableText('int _i;', variableName: '_i'); |
| 2260 expect(unlinkedUnits[0].publicNamespace.names, isEmpty); | 2335 expect(unlinkedUnits[0].publicNamespace.names, isEmpty); |
| 2261 } | 2336 } |
| 2262 } | 2337 } |
| OLD | NEW |