Remove many uses of EngineTestCase.findNode()

R=brianwilkerson@google.com

Change-Id: I60c71f55f1d5e01a645b41fd0bd82282ca2eae20
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/118521
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
This commit is contained in:
Konstantin Shcheglov 2019-09-24 14:21:19 +00:00 committed by commit-bot@chromium.org
parent 5a84f09a26
commit c195896c84
7 changed files with 581 additions and 893 deletions

View file

@ -6,7 +6,6 @@ import 'dart:async';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/src/dart/ast/ast.dart';
import 'package:analyzer/src/dart/ast/utilities.dart';
import 'package:analyzer/src/dart/element/element.dart';
import 'package:analyzer/src/generated/declaration_resolver.dart';
@ -17,7 +16,6 @@ import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../src/dart/resolution/driver_resolution.dart';
import '../util/element_type_matchers.dart';
import 'test_support.dart';
main() {
defineReflectiveSuite(() {
@ -245,9 +243,8 @@ const b = null;
await setupCode('f() { @a g() {} }');
// Note: metadata on local function declarations is ignored by the
// analyzer. TODO(paulberry): is this a bug?
FunctionDeclaration node = EngineTestCase.findNode(
unit, code, 'g', (AstNode n) => n is FunctionDeclaration);
NodeList<Annotation> metadata = (node as FunctionDeclarationImpl).metadata;
var node = FindNode(code, unit).functionDeclaration('g()');
NodeList<Annotation> metadata = node.metadata;
if (Parser.useFasta) {
expect(metadata, hasLength(1));
} else {
@ -354,8 +351,7 @@ const b = null;
}
NodeList<Annotation> _findMetadata(CompilationUnit unit, String search) {
AstNode node =
EngineTestCase.findNode(unit, code, search, (AstNode _) => true);
var node = FindNode(code, unit).any(search);
while (node != null) {
if (node is AnnotatedNode && node.metadata.isNotEmpty) {
return node.metadata;

View file

@ -2,7 +2,6 @@
// 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 'dart:async';
import 'dart:collection';
import 'package:analyzer/dart/analysis/features.dart';
@ -189,7 +188,7 @@ class GenericMethodResolverTest extends StaticTypeAnalyzer2TestShared {
// therefore discard the propagated type.
//
// So this test does not use strong mode.
await resolveTestUnit(r'''
await assertNoErrorsInCode(r'''
abstract class Iter {
List<S> map<S>(S f(x));
}
@ -867,7 +866,7 @@ int f() {
}
@reflectiveTest
class TypePropagationTest extends ResolverTestCase {
class TypePropagationTest extends DriverResolutionTest {
test_assignment_null() async {
String code = r'''
main() {
@ -875,144 +874,81 @@ main() {
v = null;
return v; // return
}''';
CompilationUnit unit;
{
Source source = addSource(code);
TestAnalysisResult analysisResult = await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
unit = analysisResult.unit;
}
{
SimpleIdentifier identifier = EngineTestCase.findNode(
unit, code, "v; // declare", (node) => node is SimpleIdentifier);
expect(identifier.staticType, typeProvider.intType);
}
{
SimpleIdentifier identifier = EngineTestCase.findNode(
unit, code, "v = null;", (node) => node is SimpleIdentifier);
expect(identifier.staticType, typeProvider.intType);
}
{
SimpleIdentifier identifier = EngineTestCase.findNode(
unit, code, "v; // return", (node) => node is SimpleIdentifier);
expect(identifier.staticType, typeProvider.intType);
}
addTestFile(code);
await resolveTestFile();
assertType(findNode.simple('v; // declare'), 'int');
assertType(findNode.simple('v = null;'), 'int');
assertType(findNode.simple('v; // return'), 'int');
}
test_functionExpression_asInvocationArgument_notSubtypeOfStaticType() async {
String code = r'''
await assertErrorsInCode(r'''
class A {
m(void f(int i)) {}
}
x() {
A a = new A();
a.m(() => 0);
}''';
Source source = addSource(code);
CompilationUnit unit = await _computeResolvedUnit(source, noErrors: false);
assertErrors(source, [StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE]);
// () => 0
FunctionExpression functionExpression = EngineTestCase.findNode(
unit, code, "() => 0)", (node) => node is FunctionExpression);
expect((functionExpression.staticType as FunctionType).parameters.length,
same(0));
}''', [
error(StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE, 63, 7),
]);
assertType(findNode.functionExpression('() => 0'), 'int Function()');
}
test_initializer_hasStaticType() async {
Source source = addSource(r'''
addTestFile(r'''
f() {
int v = 0;
return v;
}''');
CompilationUnit unit = await _computeResolvedUnit(source);
FunctionDeclaration function = unit.declarations[0] as FunctionDeclaration;
BlockFunctionBody body =
function.functionExpression.body as BlockFunctionBody;
NodeList<Statement> statements = body.block.statements;
// Type of 'v' in declaration.
{
VariableDeclarationStatement statement =
statements[0] as VariableDeclarationStatement;
SimpleIdentifier variableName = statement.variables.variables[0].name;
expect(variableName.staticType, typeProvider.intType);
}
// Type of 'v' in reference.
{
ReturnStatement statement = statements[1] as ReturnStatement;
SimpleIdentifier variableName = statement.expression as SimpleIdentifier;
expect(variableName.staticType, typeProvider.intType);
}
await resolveTestFile();
assertType(findNode.simple('v = 0;'), 'int');
assertType(findNode.simple('v;'), 'int');
}
test_initializer_hasStaticType_parameterized() async {
Source source = addSource(r'''
addTestFile(r'''
f() {
List<int> v = <int>[];
return v;
}''');
CompilationUnit unit = await _computeResolvedUnit(source);
FunctionDeclaration function = unit.declarations[0] as FunctionDeclaration;
BlockFunctionBody body =
function.functionExpression.body as BlockFunctionBody;
NodeList<Statement> statements = body.block.statements;
// Type of 'v' in declaration.
{
VariableDeclarationStatement statement =
statements[0] as VariableDeclarationStatement;
SimpleIdentifier variableName = statement.variables.variables[0].name;
expect(variableName.staticType, isNotNull);
}
// Type of 'v' in reference.
{
ReturnStatement statement = statements[1] as ReturnStatement;
SimpleIdentifier variableName = statement.expression as SimpleIdentifier;
expect(variableName.staticType, isNotNull);
}
await resolveTestFile();
assertType(findNode.simple('v ='), 'List<int>');
assertType(findNode.simple('v;'), 'List<int>');
}
test_initializer_null() async {
String code = r'''
addTestFile(r'''
main() {
int v = null;
return v; // marker
}''';
CompilationUnit unit;
{
Source source = addSource(code);
unit = await _computeResolvedUnit(source);
}
{
SimpleIdentifier identifier = EngineTestCase.findNode(
unit, code, "v = null;", (node) => node is SimpleIdentifier);
expect(identifier.staticType, typeProvider.intType);
}
{
SimpleIdentifier identifier = EngineTestCase.findNode(
unit, code, "v; // marker", (node) => node is SimpleIdentifier);
expect(identifier.staticType, typeProvider.intType);
}
return v;
}''');
await resolveTestFile();
assertType(findNode.simple('v ='), 'int');
assertType(findNode.simple('v;'), 'int');
}
test_invocation_target_prefixed() async {
addNamedSource('/helper.dart', '''
library helper;
newFile('/test/lib/a.dart', content: r'''
int max(int x, int y) => 0;
''');
String code = '''
import 'helper.dart' as helper;
addTestFile('''
import 'a.dart' as helper;
main() {
helper.max(10, 10); // marker
}''';
CompilationUnit unit = await resolveSource(code);
SimpleIdentifier methodName =
findMarkedIdentifier(code, unit, "(10, 10); // marker");
MethodInvocation methodInvoke = methodName.parent;
expect(methodInvoke.methodName.staticElement, isNotNull);
}''');
await resolveTestFile();
assertElement(
findNode.simple('max(10, 10)'),
findElement.importFind('package:test/a.dart').topFunction('max'),
);
}
test_is_subclass() async {
Source source = addSource(r'''
addTestFile(r'''
class A {}
class B extends A {
B m() => this;
@ -1023,20 +959,17 @@ A f(A p) {
}
return p;
}''');
CompilationUnit unit = await _computeResolvedUnit(source);
FunctionDeclaration function = unit.declarations[2] as FunctionDeclaration;
BlockFunctionBody body =
function.functionExpression.body as BlockFunctionBody;
IfStatement ifStatement = body.block.statements[0] as IfStatement;
ReturnStatement statement =
(ifStatement.thenStatement as Block).statements[0] as ReturnStatement;
MethodInvocation invocation = statement.expression as MethodInvocation;
expect(invocation.methodName.staticElement, isNotNull);
await resolveTestFile();
assertElement(
findNode.methodInvocation('p.m()'),
findElement.method('m', of: 'B'),
);
}
test_mutatedOutsideScope() async {
// https://code.google.com/p/dart/issues/detail?id=22732
Source source = addSource(r'''
await assertNoErrorsInCode(r'''
class Base {
}
@ -1061,104 +994,62 @@ void g() {
}
x = null;
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
}
test_objectAccessInference_disabled_for_library_prefix() async {
String name = 'hashCode';
addNamedSource('/helper.dart', '''
library helper;
dynamic get $name => 42;
newFile('/test/lib/a.dart', content: '''
dynamic get hashCode => 42;
''');
String code = '''
import 'helper.dart' as helper;
await assertNoErrorsInCode('''
import 'a.dart' as helper;
main() {
helper.$name; // marker
}''';
CompilationUnit unit = await resolveSource(code);
SimpleIdentifier id = findMarkedIdentifier(code, unit, "; // marker");
PrefixedIdentifier prefixedId = id.parent;
expect(id.staticType, typeProvider.dynamicType);
expect(prefixedId.staticType, typeProvider.dynamicType);
helper.hashCode;
}''');
assertTypeDynamic(findNode.prefixed('helper.hashCode'));
}
test_objectAccessInference_disabled_for_local_getter() async {
String name = 'hashCode';
String code = '''
dynamic get $name => null;
await assertNoErrorsInCode('''
dynamic get hashCode => null;
main() {
$name; // marker
}''';
CompilationUnit unit = await resolveSource(code);
SimpleIdentifier getter = findMarkedIdentifier(code, unit, "; // marker");
expect(getter.staticType, typeProvider.dynamicType);
hashCode; // marker
}''');
assertTypeDynamic(findNode.simple('hashCode; // marker'));
}
test_objectMethodInference_disabled_for_library_prefix() async {
String name = 'toString';
addNamedSource('/helper.dart', '''
library helper;
newFile('/test/lib/a.dart', content: '''
dynamic toString = (int x) => x + 42;
''');
String code = '''
import 'helper.dart' as helper;
await assertNoErrorsInCode('''
import 'a.dart' as helper;
main() {
helper.$name(); // marker
}''';
CompilationUnit unit = await resolveSource(code);
SimpleIdentifier methodName =
findMarkedIdentifier(code, unit, "(); // marker");
MethodInvocation methodInvoke = methodName.parent;
expect(methodName.staticType, typeProvider.dynamicType);
expect(methodInvoke.staticType, typeProvider.dynamicType);
helper.toString();
}''');
assertTypeDynamic(findNode.methodInvocation('helper.toString()'));
}
test_objectMethodInference_disabled_for_local_function() async {
String name = 'toString';
String code = '''
addTestFile('''
main() {
dynamic $name = () => null;
$name(); // marker
}''';
CompilationUnit unit = await resolveSource(code);
SimpleIdentifier identifier = findMarkedIdentifier(code, unit, "$name = ");
expect(identifier.staticType, typeProvider.dynamicType);
SimpleIdentifier methodName =
findMarkedIdentifier(code, unit, "(); // marker");
MethodInvocation methodInvoke = methodName.parent;
expect(methodName.staticType, typeProvider.dynamicType);
expect(methodInvoke.staticType, typeProvider.dynamicType);
dynamic toString = () => null;
toString(); // marker
}''');
await resolveTestFile();
assertTypeDynamic(findNode.simple('toString ='));
assertTypeDynamic(findNode.simple('toString(); // marker'));
}
@failingTest
test_propagatedReturnType_functionExpression() async {
// TODO(scheglov) disabled because we don't resolve function expression
String code = r'''
addTestFile(r'''
main() {
var v = (() {return 42;})();
}''';
CompilationUnit unit = await resolveSource(code);
assertAssignedType(code, unit, typeProvider.dynamicType);
}
}''');
await resolveTestFile();
/**
* Return the resolved unit for the given [source].
*
* If [noErrors] is not specified or is not `true`, [assertNoErrors].
*/
Future<CompilationUnit> _computeResolvedUnit(Source source,
{bool noErrors: true}) async {
TestAnalysisResult analysisResult = await computeAnalysisResult(source);
if (noErrors) {
assertNoErrors(source);
verify([source]);
}
return analysisResult.unit;
assertTypeDynamic(findNode.simple('v = '));
}
}

View file

@ -32,6 +32,7 @@ import 'package:analyzer/src/test_utilities/mock_sdk.dart';
import 'package:analyzer/src/test_utilities/resource_provider_mixin.dart';
import 'package:test/test.dart';
import '../src/dart/resolution/driver_resolution.dart';
import 'test_analysis_context.dart';
import 'test_support.dart';
@ -738,11 +739,7 @@ class ResolverTestCase extends EngineTestCase with ResourceProviderMixin {
* Shared infrastructure for [StaticTypeAnalyzer2Test] and
* [StrongModeStaticTypeAnalyzer2Test].
*/
class StaticTypeAnalyzer2TestShared extends ResolverTestCase {
String testCode;
Source testSource;
CompilationUnit testUnit;
class StaticTypeAnalyzer2TestShared extends DriverResolutionTest {
/**
* Find the expression that starts at the offset of [search] and validate its
* that its static type matches the given [type].
@ -752,7 +749,7 @@ class StaticTypeAnalyzer2TestShared extends ResolverTestCase {
* to match the type.
*/
void expectExpressionType(String search, type) {
Expression expression = findExpression(search);
Expression expression = findNode.expression(search);
_expectType(expression.staticType, type);
}
@ -778,7 +775,7 @@ class StaticTypeAnalyzer2TestShared extends ResolverTestCase {
fail('Wrong element type: ${element.runtimeType}');
}
SimpleIdentifier identifier = findIdentifier(name);
SimpleIdentifier identifier = findNode.simple(name);
// Element is either ExecutableElement or ParameterElement.
var element = identifier.staticElement;
FunctionTypeImpl functionType = (element as dynamic).type;
@ -797,7 +794,7 @@ class StaticTypeAnalyzer2TestShared extends ResolverTestCase {
* output.
*/
FunctionTypeImpl expectFunctionType2(String name, String type) {
SimpleIdentifier identifier = findIdentifier(name);
SimpleIdentifier identifier = findNode.simple(name);
FunctionTypeImpl functionType = identifier.staticType;
expect('$functionType', type);
return functionType;
@ -811,7 +808,7 @@ class StaticTypeAnalyzer2TestShared extends ResolverTestCase {
* to match the type.
*/
void expectIdentifierType(String name, type) {
SimpleIdentifier identifier = findIdentifier(name);
SimpleIdentifier identifier = findNode.simple(name);
_expectType(identifier.staticType, type);
}
@ -824,34 +821,13 @@ class StaticTypeAnalyzer2TestShared extends ResolverTestCase {
* to match the type.
*/
void expectInitializerType(String name, type) {
SimpleIdentifier identifier = findIdentifier(name);
SimpleIdentifier identifier = findNode.simple(name);
VariableDeclaration declaration =
identifier.thisOrAncestorOfType<VariableDeclaration>();
Expression initializer = declaration.initializer;
_expectType(initializer.staticType, type);
}
Expression findExpression(String search) {
return EngineTestCase.findNode(
testUnit, testCode, search, (node) => node is Expression);
}
SimpleIdentifier findIdentifier(String search) {
return EngineTestCase.findNode(
testUnit, testCode, search, (node) => node is SimpleIdentifier);
}
Future<void> resolveTestUnit(String code, {bool noErrors: true}) async {
testCode = code;
testSource = addSource(testCode);
TestAnalysisResult analysisResult = await computeAnalysisResult(testSource);
if (noErrors) {
assertNoErrors(testSource);
}
verify([testSource]);
testUnit = analysisResult.unit;
}
/**
* Validates that [type] matches [expected].
*

View file

@ -53,14 +53,13 @@ void _fail(String message) {
@reflectiveTest
class SetLiteralsTest extends StaticTypeAnalyzer2TestShared {
test_emptySetLiteral_parameter_typed() async {
String code = r'''
await assertNoErrorsInCode(r'''
main() {
useSet({});
}
void useSet(Set<int> s) {
}
''';
await resolveTestUnit(code);
''');
expectExpressionType('{}', 'Set<int>');
}
}
@ -71,90 +70,83 @@ void useSet(Set<int> s) {
@reflectiveTest
class StaticTypeAnalyzer2Test extends StaticTypeAnalyzer2TestShared {
test_FunctionExpressionInvocation_block() async {
String code = r'''
await assertNoErrorsInCode(r'''
main() {
var foo = (() { return 1; })();
}
''';
await resolveTestUnit(code);
''');
expectInitializerType('foo', 'int');
}
test_FunctionExpressionInvocation_curried() async {
String code = r'''
await assertNoErrorsInCode(r'''
typedef int F();
F f() => null;
main() {
var foo = f()();
}
''';
await resolveTestUnit(code);
''');
expectInitializerType('foo', 'int');
}
test_FunctionExpressionInvocation_expression() async {
String code = r'''
await assertNoErrorsInCode(r'''
main() {
var foo = (() => 1)();
}
''';
await resolveTestUnit(code);
''');
expectInitializerType('foo', 'int');
}
test_MethodInvocation_nameType_localVariable() async {
String code = r"""
await assertNoErrorsInCode(r"""
typedef Foo();
main() {
Foo foo;
foo();
}
""";
await resolveTestUnit(code);
""");
// "foo" should be resolved to the "Foo" type
expectIdentifierType("foo();", new TypeMatcher<FunctionType>());
}
test_MethodInvocation_nameType_parameter_FunctionTypeAlias() async {
String code = r"""
await assertNoErrorsInCode(r"""
typedef Foo();
main(Foo foo) {
foo();
}
""";
await resolveTestUnit(code);
""");
// "foo" should be resolved to the "Foo" type
expectIdentifierType("foo();", new TypeMatcher<FunctionType>());
}
test_MethodInvocation_nameType_parameter_propagatedType() async {
String code = r"""
await assertNoErrorsInCode(r"""
typedef Foo();
main(p) {
if (p is Foo) {
p();
}
}
""";
await resolveTestUnit(code);
""");
expectIdentifierType("p()", 'dynamic Function()');
}
test_staticMethods_classTypeParameters() async {
String code = r'''
await assertNoErrorsInCode(r'''
class C<T> {
static void m() => null;
}
main() {
print(C.m);
}
''';
await resolveTestUnit(code);
''');
expectFunctionType('m);', 'void Function()');
}
test_staticMethods_classTypeParameters_genericMethod() async {
String code = r'''
await assertNoErrorsInCode(r'''
class C<T> {
static void m<S>(S s) {
void f<U>(S s, U u) {}
@ -164,8 +156,7 @@ class C<T> {
main() {
print(C.m);
}
''';
await resolveTestUnit(code);
''');
// C - m
TypeParameterType typeS;
{
@ -208,24 +199,22 @@ main() {
@reflectiveTest
class StaticTypeAnalyzer3Test extends StaticTypeAnalyzer2TestShared {
test_emptyMapLiteral_initializer_var() async {
String code = r'''
await assertNoErrorsInCode(r'''
main() {
var v = {};
}
''';
await resolveTestUnit(code);
''');
expectExpressionType('{}', 'Map<dynamic, dynamic>');
}
test_emptyMapLiteral_parameter_typed() async {
String code = r'''
await assertNoErrorsInCode(r'''
main() {
useMap({});
}
void useMap(Map<int, int> m) {
}
''';
await resolveTestUnit(code);
''');
expectExpressionType('{}', 'Map<int, int>');
}
}
@ -1490,20 +1479,19 @@ class StaticTypeAnalyzerTest extends EngineTestCase
class StaticTypeAnalyzerWithSetLiteralsTest
extends StaticTypeAnalyzer2TestShared {
test_emptySetLiteral_inferredFromLinkedHashSet() async {
String code = r'''
await assertErrorsInCode(r'''
import 'dart:collection';
LinkedHashSet<int> test4() => {};
''';
await resolveTestUnit(code, noErrors: false);
''', [
error(StrongModeCode.INVALID_CAST_LITERAL_SET, 56, 2),
]);
expectExpressionType('{}', 'Set<dynamic>');
await assertErrorsInCode(code, [StrongModeCode.INVALID_CAST_LITERAL_SET]);
}
test_emptySetLiteral_initializer_typed_nested() async {
String code = r'''
await assertNoErrorsInCode(r'''
Set<Set<int>> ints = {{}};
''';
await resolveTestUnit(code);
''');
expectExpressionType('{}', 'Set<int>');
expectExpressionType('{{}}', 'Set<Set<int>>');
}

File diff suppressed because it is too large Load diff

View file

@ -20,111 +20,21 @@ main() {
class InferenceFailureOnUninitializedVariableTest
extends StaticTypeAnalyzer2TestShared {
@override
void setUp() {
super.setUp();
AnalysisOptionsImpl options = new AnalysisOptionsImpl();
options.strictInference = true;
resetWith(options: options);
}
test_localVariable() async {
String code = r'''
void f() {
var a;
}
''';
await resolveTestUnit(code, noErrors: false);
await assertErrorsInCode(
code, [HintCode.INFERENCE_FAILURE_ON_UNINITIALIZED_VARIABLE]);
}
test_localVariable_withInitializer() async {
String code = r'''
void f() {
var a = 7;
}
''';
await resolveTestUnit(code, noErrors: false);
await assertNoErrorsInCode(code);
}
test_localVariable_withType() async {
String code = r'''
void f() {
int a;
dynamic b;
Object c;
Null d;
}
''';
await resolveTestUnit(code, noErrors: false);
await assertNoErrorsInCode(code);
}
test_topLevelVariable() async {
String code = r'''
var a;
''';
await resolveTestUnit(code, noErrors: false);
await assertErrorsInCode(
code, [HintCode.INFERENCE_FAILURE_ON_UNINITIALIZED_VARIABLE]);
}
test_topLevelVariable_withInitializer() async {
String code = r'''
var a = 7;
''';
await resolveTestUnit(code, noErrors: false);
await assertNoErrorsInCode(code);
}
test_topLevelVariable_withType() async {
String code = r'''
int a;
dynamic b;
Object c;
Null d;
''';
await resolveTestUnit(code, noErrors: false);
await assertNoErrorsInCode(code);
}
AnalysisOptionsImpl get analysisOptions =>
AnalysisOptionsImpl()..strictInference = true;
test_field() async {
String code = r'''
await assertErrorsInCode(r'''
class C {
var a;
}
''';
await resolveTestUnit(code, noErrors: false);
await assertErrorsInCode(
code, [HintCode.INFERENCE_FAILURE_ON_UNINITIALIZED_VARIABLE]);
}
test_finalField() async {
String code = r'''
class C {
final a;
C(this.a);
}
''';
await resolveTestUnit(code, noErrors: false);
await assertErrorsInCode(
code, [HintCode.INFERENCE_FAILURE_ON_UNINITIALIZED_VARIABLE]);
}
test_staticField() async {
String code = r'''
class C {
static var a;
}
''';
await resolveTestUnit(code, noErrors: false);
await assertErrorsInCode(
code, [HintCode.INFERENCE_FAILURE_ON_UNINITIALIZED_VARIABLE]);
''', [
error(HintCode.INFERENCE_FAILURE_ON_UNINITIALIZED_VARIABLE, 16, 1),
]);
}
test_field_withInitializer() async {
String code = r'''
await assertNoErrorsInCode(r'''
class C {
static var c = 3;
static final d = 5;
@ -132,13 +42,11 @@ class C {
var a = 7;
final b = 9;
}
''';
await resolveTestUnit(code, noErrors: false);
await assertNoErrorsInCode(code);
''');
}
test_field_withType() async {
String code = r'''
await assertNoErrorsInCode(r'''
class C {
static int c;
static final int d = 5;
@ -148,8 +56,87 @@ class C {
C(this.b);
}
''';
await resolveTestUnit(code, noErrors: false);
await assertNoErrorsInCode(code);
''');
}
test_finalField() async {
await assertErrorsInCode(r'''
class C {
final a;
C(this.a);
}
''', [
error(HintCode.INFERENCE_FAILURE_ON_UNINITIALIZED_VARIABLE, 18, 1),
]);
}
test_localVariable() async {
await assertErrorsInCode(r'''
void f() {
var a;
}
''', [
error(HintCode.UNUSED_LOCAL_VARIABLE, 17, 1),
error(HintCode.INFERENCE_FAILURE_ON_UNINITIALIZED_VARIABLE, 17, 1),
]);
}
test_localVariable_withInitializer() async {
await assertErrorsInCode(r'''
void f() {
var a = 7;
}
''', [
error(HintCode.UNUSED_LOCAL_VARIABLE, 17, 1),
]);
}
test_localVariable_withType() async {
await assertErrorsInCode(r'''
void f() {
int a;
dynamic b;
Object c;
Null d;
}
''', [
error(HintCode.UNUSED_LOCAL_VARIABLE, 17, 1),
error(HintCode.UNUSED_LOCAL_VARIABLE, 30, 1),
error(HintCode.UNUSED_LOCAL_VARIABLE, 42, 1),
error(HintCode.UNUSED_LOCAL_VARIABLE, 52, 1),
]);
}
test_staticField() async {
await assertErrorsInCode(r'''
class C {
static var a;
}
''', [
error(HintCode.INFERENCE_FAILURE_ON_UNINITIALIZED_VARIABLE, 23, 1),
]);
}
test_topLevelVariable() async {
await assertErrorsInCode(r'''
var a;
''', [
error(HintCode.INFERENCE_FAILURE_ON_UNINITIALIZED_VARIABLE, 4, 1),
]);
}
test_topLevelVariable_withInitializer() async {
await assertNoErrorsInCode(r'''
var a = 7;
''');
}
test_topLevelVariable_withType() async {
await assertNoErrorsInCode(r'''
int a;
dynamic b;
Object c;
Null d;
''');
}
}

View file

@ -20,104 +20,117 @@ main() {
class InferenceFailureOnUntypedParameterTest
extends StaticTypeAnalyzer2TestShared {
@override
void setUp() {
super.setUp();
AnalysisOptionsImpl options = new AnalysisOptionsImpl();
options.strictInference = true;
resetWith(options: options);
AnalysisOptionsImpl get analysisOptions =>
AnalysisOptionsImpl()..strictInference = true;
test_fieldParameter() async {
await assertNoErrorsInCode(r'''
class C {
int a;
C(this.a) {}
}
''');
}
test_parameter() async {
String code = r'''
void fn(a) => print(a);
''';
await resolveTestUnit(code, noErrors: false);
await assertErrorsInCode(
code, [HintCode.INFERENCE_FAILURE_ON_UNTYPED_PARAMETER]);
test_functionTypeParameter_withType() async {
await assertNoErrorsInCode(r'''
void fn(String cb(int x)) => print(cb(7));
''');
}
test_parameter_withVar() async {
String code = r'''
void fn(var a) => print(a);
''';
await resolveTestUnit(code, noErrors: false);
await assertErrorsInCode(
code, [HintCode.INFERENCE_FAILURE_ON_UNTYPED_PARAMETER]);
}
test_parameter_withType() async {
String code = r'''
void fn(int a) => print(a);
''';
await resolveTestUnit(code, noErrors: false);
await assertNoErrorsInCode(code);
}
test_parameter_inGenericFunction_withType() async {
String code = r'''
void fn<T>(T a) => print(a);
''';
await resolveTestUnit(code, noErrors: false);
await assertNoErrorsInCode(code);
}
test_parameter_withVarAndDefault() async {
String code = r'''
void fn([var a = 7]) => print(a);
''';
await resolveTestUnit(code, noErrors: false);
await assertErrorsInCode(
code, [HintCode.INFERENCE_FAILURE_ON_UNTYPED_PARAMETER]);
}
test_parameter_withTypeAndDefault() async {
String code = r'''
void fn([int a = 7]) => print(a);
''';
await resolveTestUnit(code, noErrors: false);
await assertNoErrorsInCode(code);
}
test_namedParameter_withVar() async {
String code = r'''
void fn({var a}) => print(a);
''';
await resolveTestUnit(code, noErrors: false);
await assertErrorsInCode(
code, [HintCode.INFERENCE_FAILURE_ON_UNTYPED_PARAMETER]);
test_functionTypeParameter_withVar() async {
await assertErrorsInCode(r'''
void fn(String cb(var x)) => print(cb(7));
''', [
error(HintCode.INFERENCE_FAILURE_ON_UNTYPED_PARAMETER, 18, 5),
]);
}
test_namedParameter_withType() async {
String code = r'''
await assertNoErrorsInCode(r'''
void fn({int a}) => print(a);
''';
await resolveTestUnit(code, noErrors: false);
await assertNoErrorsInCode(code);
''');
}
test_namedParameter_withVar() async {
await assertErrorsInCode(r'''
void fn({var a}) => print(a);
''', [
error(HintCode.INFERENCE_FAILURE_ON_UNTYPED_PARAMETER, 9, 5),
]);
}
test_parameter() async {
await assertErrorsInCode(r'''
void fn(a) => print(a);
''', [
error(HintCode.INFERENCE_FAILURE_ON_UNTYPED_PARAMETER, 8, 1),
]);
}
test_parameter_inConstructor() async {
await assertErrorsInCode(r'''
class C {
C(var a) {}
}
''', [
error(HintCode.INFERENCE_FAILURE_ON_UNTYPED_PARAMETER, 14, 5),
]);
}
test_parameter_inConstructor_withType() async {
await assertNoErrorsInCode(r'''
class C {
C(int a) {}
}
''');
}
test_parameter_inFunctionLiteral() async {
await assertErrorsInCode(r'''
void fn() {
var f = (var a) {};
}
''', [
error(HintCode.UNUSED_LOCAL_VARIABLE, 18, 1),
error(HintCode.INFERENCE_FAILURE_ON_UNTYPED_PARAMETER, 23, 5),
]);
}
test_parameter_inFunctionLiteral_withType() async {
await assertNoErrorsInCode(r'''
void fn() {
var f = (int a) {};
}
''');
}
test_parameter_inGenericFunction_withType() async {
await assertNoErrorsInCode(r'''
void fn<T>(T a) => print(a);
''');
}
test_parameter_inMethod() async {
String code = r'''
await assertErrorsInCode(r'''
class C {
void fn(var a) => print(a);
}
''';
await resolveTestUnit(code, noErrors: false);
await assertErrorsInCode(
code, [HintCode.INFERENCE_FAILURE_ON_UNTYPED_PARAMETER]);
''', [
error(HintCode.INFERENCE_FAILURE_ON_UNTYPED_PARAMETER, 20, 5),
]);
}
test_parameter_inMethod_withType() async {
String code = r'''
await assertNoErrorsInCode(r'''
class C {
void fn(String a) => print(a);
}
''';
await resolveTestUnit(code, noErrors: false);
await assertNoErrorsInCode(code);
''');
}
test_parameter_inOverridingMethod() async {
String code = r'''
await assertErrorsInCode(r'''
class C {
void fn(int a) => print(a);
}
@ -126,29 +139,13 @@ class D extends C {
@override
void fn(var a) => print(a);
}
''';
await resolveTestUnit(code, noErrors: false);
await assertErrorsInCode(
code, [HintCode.INFERENCE_FAILURE_ON_UNTYPED_PARAMETER]);
}
test_parameter_inOverridingMethod_withType() async {
String code = r'''
class C {
void fn(int a) => print(a);
}
class D extends C {
@override
void fn(num a) => print(a);
}
''';
await resolveTestUnit(code, noErrors: false);
await assertNoErrorsInCode(code);
''', [
error(HintCode.INFERENCE_FAILURE_ON_UNTYPED_PARAMETER, 85, 5),
]);
}
test_parameter_inOverridingMethod_withDefault() async {
String code = r'''
await assertErrorsInCode(r'''
class C {
void fn([int a = 7]) => print(a);
}
@ -157,14 +154,13 @@ class D extends C {
@override
void fn([var a = 7]) => print(a);
}
''';
await resolveTestUnit(code, noErrors: false);
await assertErrorsInCode(
code, [HintCode.INFERENCE_FAILURE_ON_UNTYPED_PARAMETER]);
''', [
error(HintCode.INFERENCE_FAILURE_ON_UNTYPED_PARAMETER, 92, 5),
]);
}
test_parameter_inOverridingMethod_withDefaultAndType() async {
String code = r'''
await assertNoErrorsInCode(r'''
class C {
void fn([int a = 7]) => print(a);
}
@ -173,86 +169,53 @@ class D extends C {
@override
void fn([num a = 7]) => print(a);
}
''';
await resolveTestUnit(code, noErrors: false);
await assertNoErrorsInCode(code);
''');
}
test_parameter_inConstructor() async {
String code = r'''
test_parameter_inOverridingMethod_withType() async {
await assertNoErrorsInCode(r'''
class C {
C(var a) {}
void fn(int a) => print(a);
}
''';
await resolveTestUnit(code, noErrors: false);
await assertErrorsInCode(
code, [HintCode.INFERENCE_FAILURE_ON_UNTYPED_PARAMETER]);
}
test_parameter_inConstructor_withType() async {
String code = r'''
class C {
C(int a) {}
class D extends C {
@override
void fn(num a) => print(a);
}
''';
await resolveTestUnit(code, noErrors: false);
await assertNoErrorsInCode(code);
}
test_fieldParameter() async {
String code = r'''
class C {
int a;
C(this.a) {}
}
''';
await resolveTestUnit(code, noErrors: false);
await assertNoErrorsInCode(code);
}
test_parameter_inFunctionLiteral() async {
String code = r'''
void fn() {
var f = (var a) {};
}
''';
await resolveTestUnit(code, noErrors: false);
await assertErrorsInCode(
code, [HintCode.INFERENCE_FAILURE_ON_UNTYPED_PARAMETER]);
}
test_parameter_inFunctionLiteral_withType() async {
String code = r'''
void fn() {
var f = (int a) {};
}
''';
await resolveTestUnit(code, noErrors: false);
await assertNoErrorsInCode(code);
}
test_functionTypeParameter_withVar() async {
String code = r'''
void fn(String cb(var x)) => print(cb(7));
''';
await resolveTestUnit(code, noErrors: false);
await assertErrorsInCode(
code, [HintCode.INFERENCE_FAILURE_ON_UNTYPED_PARAMETER]);
}
test_functionTypeParameter_withType() async {
String code = r'''
void fn(String cb(int x)) => print(cb(7));
''';
await resolveTestUnit(code, noErrors: false);
await assertNoErrorsInCode(code);
''');
}
test_parameter_inTypedef_withType() async {
String code = r'''
await assertNoErrorsInCode(r'''
typedef cb = void Function(int a);
''';
await resolveTestUnit(code, noErrors: false);
await assertNoErrorsInCode(code);
''');
}
test_parameter_withType() async {
await assertNoErrorsInCode(r'''
void fn(int a) => print(a);
''');
}
test_parameter_withTypeAndDefault() async {
await assertNoErrorsInCode(r'''
void fn([int a = 7]) => print(a);
''');
}
test_parameter_withVar() async {
await assertErrorsInCode(r'''
void fn(var a) => print(a);
''', [
error(HintCode.INFERENCE_FAILURE_ON_UNTYPED_PARAMETER, 8, 5),
]);
}
test_parameter_withVarAndDefault() async {
await assertErrorsInCode(r'''
void fn([var a = 7]) => print(a);
''', [
error(HintCode.INFERENCE_FAILURE_ON_UNTYPED_PARAMETER, 9, 5),
]);
}
}