| // Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file | |
| // for details. All rights reserved. Use of this source code is governed by a | |
| // BSD-style license that can be found in the LICENSE file. | |
| import 'package:analyzer/dart/analysis/results.dart'; | |
| import 'package:analyzer/dart/analysis/session.dart'; | |
| import 'package:analyzer/dart/constant/value.dart'; | |
| import 'package:analyzer/dart/element/element.dart'; | |
| import 'package:analyzer/dart/element/type.dart'; | |
| import 'package:analyzer/diagnostic/diagnostic.dart'; | |
| import 'package:analyzer/file_system/file_system.dart'; | |
| import 'package:analyzer/src/dart/analysis/results.dart'; | |
| import 'package:analyzer/src/dart/ast/ast.dart'; | |
| import 'package:analyzer/src/dart/constant/value.dart'; | |
| import 'package:analyzer/src/dart/element/element.dart'; | |
| import 'package:analyzer/src/dart/element/inheritance_manager3.dart'; | |
| import 'package:analyzer/src/dart/element/member.dart'; | |
| import 'package:analyzer/src/dart/element/type.dart'; | |
| import 'package:analyzer/src/dart/element/type_algebra.dart'; | |
| import 'package:analyzer/src/dart/element/type_provider.dart'; | |
| import 'package:analyzer/src/dart/element/type_system.dart'; | |
| import 'package:analyzer/src/test_utilities/find_element.dart'; | |
| import 'package:analyzer/src/test_utilities/find_node.dart'; | |
| import 'package:analyzer_testing/resource_provider_mixin.dart'; | |
| import 'package:analyzer_testing/src/expected_diagnostics.dart'; | |
| import 'package:analyzer_utilities/testing/tree_string_sink.dart'; | |
| import 'package:test/test.dart'; | |
| import '../../../util/diff.dart'; | |
| import '../../../util/element_printer.dart'; | |
| import '../../../util/language_feature_directive_lowering.dart'; | |
| import '../../summary/resolved_ast_printer.dart'; | |
| import '../analysis/result_printer.dart'; | |
| import 'dart_object_printer.dart'; | |
| import 'node_text_expectations.dart'; | |
| final isDynamicType = TypeMatcher<DynamicTypeImpl>(); | |
| final isNeverType = TypeMatcher<NeverTypeImpl>(); | |
| final isVoidType = TypeMatcher<VoidTypeImpl>(); | |
| /// Base for resolution tests. | |
| mixin ResolutionTest implements ResourceProviderMixin { | |
| final ResolvedNodeTextConfiguration nodeTextConfiguration = | |
| ResolvedNodeTextConfiguration(); | |
| final DartObjectPrinterConfiguration dartObjectPrinterConfiguration = | |
| DartObjectPrinterConfiguration(); | |
| File get testFile; | |
| void addTestFile(String content) { | |
| newFile(testFile.path, content); | |
| } | |
| void assertDartObjectText(DartObject? object, String expected) { | |
| var buffer = StringBuffer(); | |
| var sink = TreeStringSink(sink: buffer, indent: ''); | |
| var elementPrinter = ElementPrinter( | |
| sink: sink, | |
| configuration: ElementPrinterConfiguration(), | |
| ); | |
| DartObjectPrinter( | |
| configuration: dartObjectPrinterConfiguration, | |
| sink: sink, | |
| elementPrinter: elementPrinter, | |
| ).write(object as DartObjectImpl?); | |
| var actual = buffer.toString(); | |
| if (actual != expected) { | |
| NodeTextExpectationsCollector.add(actual); | |
| if (NodeTextExpectationsCollector.shouldPrintFailureDetails) { | |
| printPrettyDiff(expected, actual); | |
| } | |
| fail('See the difference above.'); | |
| } | |
| } | |
| void assertElement( | |
| Element? element, { | |
| required Element declaration, | |
| Map<String, String> substitution = const {}, | |
| }) { | |
| var actualDeclaration = element?.baseElement; | |
| expect(actualDeclaration, same(declaration)); | |
| if (element is SubstitutedElementImpl) { | |
| assertSubstitution(element.substitution, substitution); | |
| } else if (substitution.isNotEmpty) { | |
| fail('Expected to be a Member: (${element.runtimeType}) $element'); | |
| } | |
| } | |
| void assertElementNull(Element? element) { | |
| expect(element, isNull); | |
| } | |
| void assertElementTypes( | |
| List<DartType>? types, | |
| List<String> expected, { | |
| bool ordered = false, | |
| }) { | |
| if (types == null) { | |
| fail('Expected types, actually null.'); | |
| } | |
| var typeStrList = types.map(typeString).toList(); | |
| if (ordered) { | |
| expect(typeStrList, expected); | |
| } else { | |
| expect(typeStrList, unorderedEquals(expected)); | |
| } | |
| } | |
| void assertParsedNodeText(AstNode node, String expected) { | |
| var buffer = StringBuffer(); | |
| var sink = TreeStringSink(sink: buffer, indent: ''); | |
| var elementPrinter = ElementPrinter( | |
| sink: sink, | |
| configuration: ElementPrinterConfiguration(), | |
| ); | |
| ResolvedAstPrinter( | |
| sink: sink, | |
| elementPrinter: elementPrinter, | |
| configuration: ResolvedNodeTextConfiguration(), | |
| withResolution: false, | |
| ).writeNodeWithV1Projection(node); | |
| var actual = buffer.toString(); | |
| if (actual != expected) { | |
| NodeTextExpectationsCollector.add(actual); | |
| if (NodeTextExpectationsCollector.shouldPrintFailureDetails) { | |
| printPrettyDiff(expected, actual); | |
| } | |
| fail('See the difference above.'); | |
| } | |
| } | |
| void assertResolvedLibraryResultText( | |
| SomeResolvedLibraryResult result, | |
| String expected, { | |
| void Function(ResolvedLibraryResultPrinterConfiguration)? configure, | |
| }) { | |
| var configuration = ResolvedLibraryResultPrinterConfiguration(); | |
| configure?.call(configuration); | |
| var buffer = StringBuffer(); | |
| var sink = TreeStringSink(sink: buffer, indent: ''); | |
| var idProvider = IdProvider(); | |
| ResolvedLibraryResultPrinter( | |
| configuration: configuration, | |
| sink: sink, | |
| idProvider: idProvider, | |
| elementPrinter: ElementPrinter( | |
| sink: sink, | |
| configuration: ElementPrinterConfiguration(), | |
| ), | |
| ).write(result); | |
| var actual = buffer.toString(); | |
| if (actual != expected) { | |
| NodeTextExpectationsCollector.add(actual); | |
| if (NodeTextExpectationsCollector.shouldPrintFailureDetails) { | |
| printPrettyDiff(expected, actual); | |
| } | |
| fail('See the difference above.'); | |
| } | |
| } | |
| void assertResolvedNodeText(AstNode node, String expected) { | |
| var actual = _resolvedNodeText(node); | |
| if (actual != expected) { | |
| NodeTextExpectationsCollector.add(actual); | |
| if (NodeTextExpectationsCollector.shouldPrintFailureDetails) { | |
| printPrettyDiff(expected, actual); | |
| } | |
| fail('See the difference above.'); | |
| } | |
| } | |
| void assertSubstitution( | |
| MapSubstitution substitution, | |
| Map<String, String> expected, | |
| ) { | |
| var actualMapString = Map.fromEntries( | |
| substitution.map.entries | |
| .where((entry) { | |
| return entry.key.enclosingElement is! ExecutableElement; | |
| }) | |
| .map((entry) { | |
| return MapEntry(entry.key.name, typeString(entry.value)); | |
| }), | |
| ); | |
| expect(actualMapString, expected); | |
| } | |
| void assertType(Object? typeOrNode, String? expected) { | |
| DartType? actual; | |
| if (typeOrNode is DartType) { | |
| actual = typeOrNode; | |
| } else if (typeOrNode is Expression) { | |
| actual = typeOrNode.staticType; | |
| } else if (typeOrNode is GenericFunctionType) { | |
| actual = typeOrNode.type; | |
| } else if (typeOrNode is NamedType) { | |
| actual = typeOrNode.type; | |
| } else { | |
| fail('Unsupported node: (${typeOrNode.runtimeType}) $typeOrNode'); | |
| } | |
| if (expected == null) { | |
| expect(actual, isNull); | |
| } else if (actual == null) { | |
| fail('Null, expected: $expected'); | |
| } else { | |
| expect(typeString(actual), expected); | |
| } | |
| } | |
| void assertTypeDynamic(Object? typeOrExpression) { | |
| DartType? actual; | |
| if (typeOrExpression case DartType? type) { | |
| actual = typeOrExpression; | |
| expect(type, isDynamicType); | |
| } else { | |
| actual = (typeOrExpression as Expression).staticType; | |
| } | |
| expect(actual, isDynamicType); | |
| } | |
| File newFileWithLanguageFeatureDirective(String path, String content) { | |
| var featureDirectiveLowering = LanguageFeatureDirectiveLowering(content); | |
| return newFile(path, featureDirectiveLowering.loweredCode); | |
| } | |
| Future<ResolvedUnitResultImpl> resolveFile(File file); | |
| /// Resolve [file] and return a test view of it. | |
| Future<TestResolvedUnitResult> resolveFile2(File file) async { | |
| var result = await resolveFile(file); | |
| return TestResolvedUnitResult(result); | |
| } | |
| /// Create a new file with the [path] and [content], and resolve it. | |
| Future<TestResolvedUnitResult> resolveFileCode(String path, String content) { | |
| var featureDirectiveLowering = LanguageFeatureDirectiveLowering(content); | |
| var file = newFile(path, featureDirectiveLowering.loweredCode); | |
| return resolveFile2(file); | |
| } | |
| /// Writes all [filesToCode], resolves each file, and checks that each file's | |
| /// inline diagnostic markers match its diagnostics. | |
| /// | |
| /// All files are written before any file is resolved. This supports tests | |
| /// where resolving one file cleanly requires related files to already exist, | |
| /// such as a library with its parts. | |
| Future<Map<File, TestResolvedUnitResult>> resolveFilesWithDiagnostics( | |
| Map<File, String> filesToCode, | |
| ) async { | |
| var files = <_DiagnosticTestFile>[]; | |
| for (var entry in filesToCode.entries) { | |
| var file = _DiagnosticTestFile(entry.key, entry.value); | |
| modifyFile2(file.file, file.loweredCode); | |
| files.add(file); | |
| } | |
| var results = <File, TestResolvedUnitResult>{}; | |
| var diagnosticsByFile = <File, List<Diagnostic>>{}; | |
| for (var file in files) { | |
| var result = await resolveFile2(file.file); | |
| results[file.file] = result; | |
| diagnosticsByFile[file.file] = result.diagnostics; | |
| } | |
| var actualCodeByFile = updateExpectedDiagnosticsForFiles( | |
| contentByFile: {for (var file in files) file.file: file.loweredCode}, | |
| actualDiagnosticsByFile: diagnosticsByFile, | |
| ); | |
| var hasMismatch = false; | |
| for (var index = 0; index < files.length; index++) { | |
| var file = files[index]; | |
| var actual = actualCodeByFile[file.file]!; | |
| actual = file.restoreDirective(actual); | |
| if (actual != file.expectedCode) { | |
| NodeTextExpectationsCollector.add(actual, intraInvocationId: '$index'); | |
| if (NodeTextExpectationsCollector.shouldPrintFailureDetails) { | |
| print('-------- ${file.file.path} --------'); | |
| printPrettyDiff(file.expectedCode, actual); | |
| } | |
| hasMismatch = true; | |
| } | |
| } | |
| if (hasMismatch) { | |
| fail('See the difference above.'); | |
| } | |
| return results; | |
| } | |
| /// Writes [code] to [file], resolves it, and checks that its inline | |
| /// diagnostic markers match its diagnostics. | |
| Future<TestResolvedUnitResult> resolveFileWithDiagnostics( | |
| File file, | |
| String code, | |
| ) async { | |
| return await _resolveFileWithDiagnostics(file, code); | |
| } | |
| /// Put the [code] into the test file, and resolve it. | |
| Future<TestResolvedUnitResult> resolveTestCode(String code) { | |
| var featureDirectiveLowering = LanguageFeatureDirectiveLowering(code); | |
| addTestFile(featureDirectiveLowering.loweredCode); | |
| return resolveTestFile(); | |
| } | |
| /// Resolves [code] and checks that its inline diagnostic markers match the | |
| /// diagnostics. Unmarked code is expected to have no diagnostics. | |
| Future<TestResolvedUnitResult> resolveTestCodeWithDiagnostics( | |
| String code, | |
| ) async { | |
| return await _resolveFileWithDiagnostics(testFile, code); | |
| } | |
| Future<TestResolvedUnitResult> resolveTestFile() { | |
| return resolveFile2(testFile); | |
| } | |
| /// Return a textual representation of the [type] that is appropriate for | |
| /// tests. | |
| String typeString(DartType type) => type.getDisplayString(); | |
| String _resolvedNodeText(AstNode node) { | |
| var buffer = StringBuffer(); | |
| var sink = TreeStringSink(sink: buffer, indent: ''); | |
| var elementPrinter = ElementPrinter( | |
| sink: sink, | |
| configuration: ElementPrinterConfiguration() | |
| ..withInterfaceTypeElements = | |
| nodeTextConfiguration.withInterfaceTypeElements | |
| ..withRedirectedConstructors = | |
| nodeTextConfiguration.withRedirectedConstructors | |
| ..withSuperConstructors = nodeTextConfiguration.withSuperConstructors | |
| ..withImportPrefixScopeLibraries = | |
| nodeTextConfiguration.withImportPrefixScopeLibraries, | |
| ); | |
| ResolvedAstPrinter( | |
| sink: sink, | |
| elementPrinter: elementPrinter, | |
| configuration: nodeTextConfiguration, | |
| ).writeNodeWithV1Projection(node); | |
| var unit = node is AstNodeImpl && node.astNodeApi == AstNodeApi.v1 | |
| // ignore: analyzer_to_be_deprecated_use | |
| ? node.thisOrAncestorOfType<CompilationUnitImpl>() | |
| : node.thisOrAncestorOfType2<CompilationUnitImpl>(); | |
| if (unit != null) { | |
| sink.writeElements('invalidNodes', unit.invalidNodes, (node) { | |
| var range = '[${node.offset}, ${node.end})'; | |
| sink.writelnWithIndent('${node.runtimeType} $range'); | |
| }); | |
| } | |
| return buffer.toString(); | |
| } | |
| Future<TestResolvedUnitResult> _resolveFileWithDiagnostics( | |
| File file, | |
| String code, | |
| ) async { | |
| var testFile = _DiagnosticTestFile(file, code); | |
| modifyFile2(file, testFile.loweredCode); | |
| var result = await resolveFile2(file); | |
| var actual = updateExpectedDiagnostics( | |
| content: testFile.loweredCode, | |
| actualDiagnostics: result.diagnostics, | |
| ); | |
| actual = testFile.restoreDirective(actual); | |
| if (actual != testFile.expectedCode) { | |
| NodeTextExpectationsCollector.add(actual); | |
| if (NodeTextExpectationsCollector.shouldPrintFailureDetails) { | |
| printPrettyDiff(testFile.expectedCode, actual); | |
| } | |
| fail('See the difference above.'); | |
| } | |
| return result; | |
| } | |
| } | |
| /// A test-facing view of a resolved unit, with utilities derived from it. | |
| final class TestResolvedUnitResult { | |
| final ResolvedUnitResultImpl analysisResult; | |
| late final FindElement findElement = FindElement(unit); | |
| late final FindNode2 findNode = FindNode2(content, unit); | |
| @ToBeDeprecated('Use findNode instead.') | |
| late final FindNode findNodeV1 = FindNode(content, unit); | |
| TestResolvedUnitResult(this.analysisResult); | |
| String get content => analysisResult.content; | |
| List<Diagnostic> get diagnostics => analysisResult.diagnostics; | |
| List<Diagnostic> get errors => analysisResult.errors; | |
| bool get exists => analysisResult.exists; | |
| File get file => analysisResult.file; | |
| InheritanceManager3 get inheritanceManager { | |
| return libraryElement.session.inheritanceManager; | |
| } | |
| bool get isLibrary => analysisResult.isLibrary; | |
| bool get isPart => analysisResult.isPart; | |
| LibraryElementImpl get libraryElement => analysisResult.libraryElement; | |
| LibraryFragmentImpl get libraryFragment => analysisResult.libraryFragment; | |
| String get path => analysisResult.path; | |
| AnalysisSession get session => analysisResult.session; | |
| TypeProviderImpl get typeProvider => analysisResult.typeProvider; | |
| TypeSystemImpl get typeSystem => analysisResult.typeSystem; | |
| CompilationUnitImpl get unit => analysisResult.unit; | |
| Uri get uri => analysisResult.uri; | |
| String get uriStr => '$uri'; | |
| } | |
| class _DiagnosticTestFile { | |
| final File file; | |
| final String expectedCode; | |
| final LanguageFeatureDirectiveLowering _featureDirectiveLowering; | |
| factory _DiagnosticTestFile(File file, String expectedCode) { | |
| var cleanCode = removeDiagnosticExpectations(expectedCode); | |
| var featureDirectiveLowering = LanguageFeatureDirectiveLowering(cleanCode); | |
| return _DiagnosticTestFile._(file, expectedCode, featureDirectiveLowering); | |
| } | |
| _DiagnosticTestFile._( | |
| this.file, | |
| this.expectedCode, | |
| this._featureDirectiveLowering, | |
| ); | |
| String get loweredCode => _featureDirectiveLowering.loweredCode; | |
| String restoreDirective(String code) { | |
| return _featureDirectiveLowering.restoreDirective(code); | |
| } | |
| } | |
| extension ResolvedUnitResultExtension on ResolvedUnitResult { | |
| FindElement get findElement { | |
| return FindElement(unit); | |
| } | |
| FindNode2 get findNode { | |
| return FindNode2(content, unit); | |
| } | |
| @ToBeDeprecated('Use findNode instead.') | |
| FindNode get findNodeV1 { | |
| return FindNode(content, unit); | |
| } | |
| InheritanceManager3 get inheritanceManager { | |
| var library = libraryElement as LibraryElementImpl; | |
| return library.session.inheritanceManager; | |
| } | |
| String get uriStr => '$uri'; | |
| } |