|
| 1 | +// Copyright (c) 2019, 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 | +import 'dart:io'; |
| 6 | + |
| 7 | +import 'package:analyzer/dart/analysis/features.dart'; |
| 8 | +import 'package:analyzer/dart/ast/ast.dart'; |
| 9 | +import 'package:analyzer/dart/element/element.dart'; |
| 10 | +import 'package:analyzer/dart/element/type_system.dart'; |
| 11 | +import 'package:analyzer/src/dart/element/type.dart'; |
| 12 | +import 'package:analyzer/src/dart/resolver/flow_analysis_visitor.dart'; |
| 13 | +import 'package:analyzer/src/util/ast_data_extractor.dart'; |
| 14 | +import 'package:front_end/src/testing/id.dart' show ActualData, Id; |
| 15 | +import 'package:front_end/src/testing/id_testing.dart'; |
| 16 | +import 'package:test/test.dart'; |
| 17 | + |
| 18 | +import '../util/id_testing_helper.dart'; |
| 19 | + |
| 20 | +main(List<String> args) async { |
| 21 | + Directory dataDir = new Directory.fromUri(Platform.script.resolve( |
| 22 | + '../../../front_end/test/flow_analysis/nullability_and_reachability/data')); |
| 23 | + await runTests(dataDir, |
| 24 | + args: args, |
| 25 | + supportedMarkers: sharedMarkers, |
| 26 | + createUriForFileName: createUriForFileName, |
| 27 | + onFailure: onFailure, |
| 28 | + runTest: |
| 29 | + runTestFor(const _FlowAnalysisDataComputer(), [analyzerNnbdConfig])); |
| 30 | +} |
| 31 | + |
| 32 | +class FlowTestBase { |
| 33 | + FlowAnalysisResult flowResult; |
| 34 | + |
| 35 | + /// Resolve the given [code] and track nullability in the unit. |
| 36 | + Future<void> trackCode(String code) async { |
| 37 | + if (await checkTests( |
| 38 | + code, |
| 39 | + const _FlowAnalysisDataComputer(), |
| 40 | + FeatureSet.forTesting( |
| 41 | + sdkVersion: '2.2.2', additionalFeatures: [Feature.non_nullable]))) { |
| 42 | + fail('Failure(s)'); |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +class _FlowAnalysisDataComputer extends DataComputer<Set<_FlowAssertion>> { |
| 48 | + const _FlowAnalysisDataComputer(); |
| 49 | + |
| 50 | + @override |
| 51 | + DataInterpreter<Set<_FlowAssertion>> get dataValidator => |
| 52 | + const _FlowAnalysisDataInterpreter(); |
| 53 | + |
| 54 | + @override |
| 55 | + void computeUnitData(CompilationUnit unit, |
| 56 | + Map<Id, ActualData<Set<_FlowAssertion>>> actualMap) { |
| 57 | + var flowResult = FlowAnalysisResult.getFromNode(unit); |
| 58 | + _FlowAnalysisDataExtractor(unit.declaredElement.source.uri, actualMap, |
| 59 | + flowResult, unit.declaredElement.context.typeSystem) |
| 60 | + .run(unit); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +class _FlowAnalysisDataExtractor extends AstDataExtractor<Set<_FlowAssertion>> { |
| 65 | + final FlowAnalysisResult _flowResult; |
| 66 | + |
| 67 | + final TypeSystem _typeSystem; |
| 68 | + |
| 69 | + _FlowAnalysisDataExtractor( |
| 70 | + Uri uri, |
| 71 | + Map<Id, ActualData<Set<_FlowAssertion>>> actualMap, |
| 72 | + this._flowResult, |
| 73 | + this._typeSystem) |
| 74 | + : super(uri, actualMap); |
| 75 | + |
| 76 | + @override |
| 77 | + Set<_FlowAssertion> computeNodeValue(Id id, AstNode node) { |
| 78 | + Set<_FlowAssertion> result = {}; |
| 79 | + if (node is SimpleIdentifier && node.inGetterContext()) { |
| 80 | + var element = node.staticElement; |
| 81 | + if (element is LocalVariableElement || element is ParameterElement) { |
| 82 | + TypeImpl promotedType = node.staticType; |
| 83 | + TypeImpl declaredType = (element as VariableElement).type; |
| 84 | + // TODO(paulberry): once type equality has been updated to account for |
| 85 | + // nullability, isPromoted should just be |
| 86 | + // `promotedType != declaredType`. See dartbug.com/37587. |
| 87 | + var isPromoted = promotedType != declaredType || |
| 88 | + promotedType.nullabilitySuffix != declaredType.nullabilitySuffix; |
| 89 | + if (isPromoted && |
| 90 | + _typeSystem.isNullable(declaredType) && |
| 91 | + !_typeSystem.isNullable(promotedType)) { |
| 92 | + result.add(_FlowAssertion.nonNullable); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + if (_flowResult.unreachableNodes.contains(node)) { |
| 97 | + result.add(_FlowAssertion.unreachable); |
| 98 | + } |
| 99 | + if (node is FunctionDeclaration) { |
| 100 | + var body = node.functionExpression.body; |
| 101 | + if (body != null && |
| 102 | + _flowResult.functionBodiesThatDontComplete.contains(body)) { |
| 103 | + result.add(_FlowAssertion.doesNotComplete); |
| 104 | + } |
| 105 | + } |
| 106 | + return result.isEmpty ? null : result; |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +class _FlowAnalysisDataInterpreter |
| 111 | + implements DataInterpreter<Set<_FlowAssertion>> { |
| 112 | + const _FlowAnalysisDataInterpreter(); |
| 113 | + |
| 114 | + @override |
| 115 | + String getText(Set<_FlowAssertion> actualData) => |
| 116 | + _sortedRepresentation(_toStrings(actualData)); |
| 117 | + |
| 118 | + @override |
| 119 | + String isAsExpected(Set<_FlowAssertion> actualData, String expectedData) { |
| 120 | + var actualStrings = _toStrings(actualData); |
| 121 | + var actualSorted = _sortedRepresentation(actualStrings); |
| 122 | + var expectedSorted = _sortedRepresentation(expectedData?.split(',')); |
| 123 | + if (actualSorted == expectedSorted) { |
| 124 | + return null; |
| 125 | + } else { |
| 126 | + return 'Expected $expectedData, got $actualSorted'; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + @override |
| 131 | + bool isEmpty(Set<_FlowAssertion> actualData) => actualData.isEmpty; |
| 132 | + |
| 133 | + String _sortedRepresentation(Iterable<String> values) { |
| 134 | + var list = values == null || values.isEmpty ? ['none'] : values.toList(); |
| 135 | + list.sort(); |
| 136 | + return list.join(','); |
| 137 | + } |
| 138 | + |
| 139 | + List<String> _toStrings(Set<_FlowAssertion> actualData) => actualData |
| 140 | + .map((flowAssertion) => flowAssertion.toString().split('.')[1]) |
| 141 | + .toList(); |
| 142 | +} |
| 143 | + |
| 144 | +enum _FlowAssertion { |
| 145 | + doesNotComplete, |
| 146 | + nonNullable, |
| 147 | + nullable, |
| 148 | + unreachable, |
| 149 | +} |
0 commit comments