Remove EngineTestCase.findSimpleIdentifier()

R=brianwilkerson@google.com

Change-Id: I96dd3d62f2df4e433c79afdc41156e522a779d8d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/118520
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:12:59 +00:00 committed by commit-bot@chromium.org
parent 11fa3e0e2b
commit 5a84f09a26
4 changed files with 139 additions and 210 deletions

View file

@ -151,6 +151,10 @@ class FindNode {
return _node(search, (n) => n is IntegerLiteral);
}
Label label(String search) {
return _node(search, (n) => n is Label);
}
LibraryDirective library(String search) {
return _node(search, (n) => n is LibraryDirective);
}

View file

@ -19,6 +19,7 @@ import 'package:analyzer/src/generated/testing/element_factory.dart';
import 'package:analyzer/src/generated/testing/element_search.dart';
import 'package:analyzer/src/generated/testing/node_search.dart';
import 'package:analyzer/src/generated/testing/token_factory.dart';
import 'package:analyzer/src/test_utilities/find_node.dart';
import 'package:test/test.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
@ -222,8 +223,8 @@ class C {
void test_metadata_localVariableDeclaration() {
var code = 'f() { @a int x, y; }';
buildElementsForText(code);
var x = findLocalVariable(code, 'x, ');
var y = findLocalVariable(code, 'x, ');
var x = findLocalVariable('x, ');
var y = findLocalVariable('x, ');
checkMetadata(x);
checkMetadata(y);
expect(x.metadata, same(y.metadata));
@ -232,15 +233,15 @@ class C {
void test_metadata_visitDeclaredIdentifier() {
var code = 'f() { for (@a var x in y) {} }';
buildElementsForText(code);
var x = findLocalVariable(code, 'x in');
var x = findLocalVariable('x in');
checkMetadata(x);
}
void test_visitCatchClause() {
var code = 'f() { try {} catch (e, s) {} }';
buildElementsForText(code);
var e = findLocalVariable(code, 'e, ');
var s = findLocalVariable(code, 's) {}');
var e = findLocalVariable('e, ');
var s = findLocalVariable('s) {}');
expect(e, isNotNull);
expect(e.name, 'e');
@ -263,7 +264,7 @@ class C {
void test_visitCatchClause_withType() {
var code = 'f() { try {} on E catch (e) {} }';
buildElementsForText(code);
var e = findLocalVariable(code, 'e) {}');
var e = findLocalVariable('e) {}');
expect(e, isNotNull);
expect(e.name, 'e');
expect(e.hasImplicitType, isFalse);
@ -292,7 +293,7 @@ class C {
void test_visitDeclaredIdentifier_noType() {
var code = 'f() { for (var i in []) {} }';
buildElementsForText(code);
var variable = findLocalVariable(code, 'i in');
var variable = findLocalVariable('i in');
assertHasCodeRange(variable, 11, 5);
expect(variable, isNotNull);
expect(variable.hasImplicitType, isTrue);
@ -309,7 +310,7 @@ class C {
void test_visitDeclaredIdentifier_type() {
var code = 'f() { for (int i in []) {} }';
buildElementsForText(code);
var variable = findLocalVariable(code, 'i in');
var variable = findLocalVariable('i in');
assertHasCodeRange(variable, 11, 5);
expect(variable.hasImplicitType, isFalse);
expect(variable.isConst, isFalse);
@ -522,7 +523,7 @@ class C {
void test_visitLabeledStatement() {
String code = 'f() { l: print(42); }';
buildElementsForText(code);
LabelElement label = findLabel(code, 'l:');
LabelElement label = findLabel('l:');
expect(label, isNotNull);
expect(label.name, 'l');
expect(label.isSynthetic, isFalse);
@ -548,13 +549,13 @@ class C {
expect(parameter, isNotNull);
expect(parameter.name, parameterName);
var v = findLocalVariable(code, 'v;');
var v = findLocalVariable('v;');
expect(v.name, 'v');
var e = findLocalVariable(code, 'e) {}');
var e = findLocalVariable('e) {}');
expect(e.name, 'e');
LabelElement label = findLabel(code, 'l:');
LabelElement label = findLabel('l:');
expect(label, isNotNull);
expect(label.name, labelName);
}
@ -750,7 +751,7 @@ class C {
void test_visitVariableDeclaration_inConstructor() {
var code = 'class C { C() { var v = 1; } }';
buildElementsForText(code);
var v = findLocalVariable(code, 'v =');
var v = findLocalVariable('v =');
assertHasCodeRange(v, 16, 9);
expect(v.hasImplicitType, isTrue);
expect(v.name, 'v');
@ -1063,11 +1064,12 @@ main() {
main.encloseElements(holder.functions);
main.encloseElements(holder.localVariables);
var f1 = findLocalFunction(code, 'f1() {');
var f2 = findLocalFunction(code, 'f2() {');
var v1 = findLocalVariable(code, 'v1;');
var v2 = findLocalVariable(code, 'v2;');
var v3 = findLocalVariable(code, 'v3;');
findNode = FindNode(code, _compilationUnit);
var f1 = findLocalFunction('f1() {');
var f2 = findLocalFunction('f2() {');
var v1 = findLocalVariable('v1;');
var v2 = findLocalVariable('v2;');
var v3 = findLocalVariable('v3;');
expect(v1.enclosingElement, main);
{
@ -1198,7 +1200,7 @@ main() {
void test_visitVariableDeclaration_local() {
var code = 'class C { m() { T v = null; } }';
buildElementsForText(code);
LocalVariableElement element = findIdentifier(code, 'v =').staticElement;
LocalVariableElement element = findNode.simple('v =').staticElement;
expect(element.hasImplicitType, isFalse);
expect(element.name, 'v');
expect(element.initializer, isNotNull);
@ -2607,6 +2609,7 @@ mixin M<T, U> on A, B implements C {
abstract class _BaseTest extends ParserTestCase {
CompilationUnitElement compilationUnitElement;
CompilationUnit _compilationUnit;
FindNode findNode;
CompilationUnit get compilationUnit => _compilationUnit;
@ -2667,20 +2670,16 @@ abstract class _BaseTest extends ParserTestCase {
AstVisitor createElementBuilder(ElementHolder holder);
SimpleIdentifier findIdentifier(String code, String prefix) {
return EngineTestCase.findSimpleIdentifier(compilationUnit, code, prefix);
LabelElement findLabel(String prefix) {
return findNode.simple(prefix).staticElement;
}
LabelElement findLabel(String code, String prefix) {
return findIdentifier(code, prefix).staticElement;
FunctionElement findLocalFunction(String search) {
return findNode.functionDeclaration(search).declaredElement;
}
FunctionElement findLocalFunction(String code, String prefix) {
return findIdentifier(code, prefix).staticElement;
}
LocalVariableElement findLocalVariable(String code, String prefix) {
return findIdentifier(code, prefix).staticElement;
LocalVariableElement findLocalVariable(String search) {
return findNode.simple(search).staticElement;
}
void setUp() {
@ -2702,6 +2701,7 @@ abstract class _BaseTest extends ParserTestCase {
AnalysisEngine.instance.logger = logger;
try {
_compilationUnit = parseCompilationUnit(code);
findNode = FindNode(code, _compilationUnit);
compilationUnit.accept(visitor);
} finally {
expect(logger.log, hasLength(0));

View file

@ -2,7 +2,7 @@
// 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/ast/ast.dart' show AstNode, SimpleIdentifier;
import 'package:analyzer/dart/ast/ast.dart' show AstNode;
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/diagnostic/diagnostic.dart';
@ -91,16 +91,6 @@ class EngineTestCase {
AstNode node = new NodeLocator(offset).searchWithin(root);
return node.thisOrAncestorMatching(predicate);
}
/// Find the [SimpleIdentifier] with at offset of the [prefix].
static SimpleIdentifier findSimpleIdentifier(
AstNode root, String code, String prefix) {
int offset = code.indexOf(prefix);
if (offset == -1) {
throw new ArgumentError("Not found '$prefix'.");
}
return new NodeLocator(offset).searchWithin(root);
}
}
/// A description of an error that is expected to be reported.

View file

@ -4,15 +4,13 @@
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/token.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/src/dart/element/type.dart';
import 'package:analyzer/src/generated/engine.dart';
import 'package:analyzer/src/test_utilities/function_ast_visitor.dart';
import 'package:test/test.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../../../generated/resolver_test_case.dart';
import '../../../generated/test_support.dart';
import '../../dart/resolution/driver_resolution.dart';
void main() {
defineReflectiveSuite(() {
@ -24,42 +22,37 @@ void main() {
///
/// https://github.com/dart-lang/sdk/issues/31638
@reflectiveTest
class Dart2InferenceTest extends ResolverTestCase {
@override
AnalysisOptions get defaultAnalysisOptions => new AnalysisOptionsImpl();
class Dart2InferenceTest extends DriverResolutionTest {
test_bool_assert() async {
var code = r'''
T f<T>() => null;
T f<T>(int _) => null;
main() {
assert(f()); // 1
assert(f(), f()); // 2
assert(f(1));
assert(f(2), f(3));
}
class C {
C() : assert(f()), // 3
assert(f(), f()); // 4
C() : assert(f(4)),
assert(f(5), f(6));
}
''';
var source = addSource(code);
var analysisResult = await computeAnalysisResult(source);
var unit = analysisResult.unit;
addTestFile(code);
await resolveTestFile();
String getType(String prefix) {
var invocation = _findMethodInvocation(unit, code, prefix);
return invocation.staticInvokeType.toString();
MethodInvocation invocation(String search) {
return findNode.methodInvocation(search);
}
expect(getType('f()); // 1'), 'bool Function()');
assertInvokeType(invocation('f(1));'), 'bool Function(int)');
expect(getType('f(), '), 'bool Function()');
expect(getType('f()); // 2'), 'dynamic Function()');
assertInvokeType(invocation('f(2)'), 'bool Function(int)');
assertInvokeType(invocation('f(3)'), 'dynamic Function(int)');
expect(getType('f()), // 3'), 'bool Function()');
assertInvokeType(invocation('f(4)'), 'bool Function(int)');
expect(getType('f(), '), 'bool Function()');
expect(getType('f()); // 4'), 'dynamic Function()');
assertInvokeType(invocation('f(5)'), 'bool Function(int)');
assertInvokeType(invocation('f(6)'), 'dynamic Function(int)');
}
test_bool_logical() async {
@ -74,12 +67,11 @@ main() {
var v2 = f() && f(); // 4
}
''';
var source = addSource(code);
var analysisResult = await computeAnalysisResult(source);
var unit = analysisResult.unit;
addTestFile(code);
await resolveTestFile();
void assertType(String prefix) {
var invocation = _findMethodInvocation(unit, code, prefix);
var invocation = findNode.methodInvocation(prefix);
expect(invocation.staticInvokeType.toString(), 'bool Function()');
}
@ -105,12 +97,11 @@ main() {
for (; f(); ) {} // 4
}
''';
var source = addSource(code);
var analysisResult = await computeAnalysisResult(source);
var unit = analysisResult.unit;
addTestFile(code);
await resolveTestFile();
void assertType(String prefix) {
var invocation = _findMethodInvocation(unit, code, prefix);
var invocation = findNode.methodInvocation(prefix);
expect(invocation.staticInvokeType.toString(), 'bool Function()');
}
@ -127,11 +118,10 @@ void main() {
g = () => 42;
}
''';
var source = addSource(code);
var analysisResult = await computeAnalysisResult(source);
var unit = analysisResult.unit;
addTestFile(code);
await resolveTestFile();
Expression closure = _findExpression(unit, code, '() => 42');
Expression closure = findNode.expression('() => 42');
expect(closure.staticType.toString(), 'List<int> Function()');
}
@ -144,16 +134,15 @@ void main() {
};
}
''';
var source = addSource(code);
var analysisResult = await computeAnalysisResult(source);
var unit = analysisResult.unit;
addTestFile(code);
await resolveTestFile();
Expression closure = _findExpression(unit, code, '() { // mark');
Expression closure = findNode.expression('() { // mark');
expect(closure.staticType.toString(), 'List<int> Function()');
}
test_compoundAssignment_index() async {
var code = r'''
addTestFile(r'''
int getInt() => 0;
num getNum() => 0;
double getDouble() => 0.0;
@ -273,15 +262,13 @@ void test9(Test<double, double> t) {
var /*@type=double*/ v10 = ++t['x'];
var /*@type=double*/ v11 = t['x']++;
}
''';
var source = addSource(code);
var analysisResult = await computeAnalysisResult(source);
var unit = analysisResult.unit;
_assertTypeAnnotations(code, unit);
''');
await resolveTestFile();
_assertTypeAnnotations();
}
test_compoundAssignment_prefixedIdentifier() async {
var code = r'''
await assertNoErrorsInCode(r'''
int getInt() => 0;
num getNum() => 0;
double getDouble() => 0.0;
@ -355,13 +342,8 @@ void test9(Test<double, double> t) {
var /*@type=double*/ v10 = ++t.x;
var /*@type=double*/ v11 = t.x++;
}
''';
var source = addSource(code);
var analysisResult = await computeAnalysisResult(source);
assertNoErrors(source);
var unit = analysisResult.unit;
_assertTypeAnnotations(code, unit);
''');
_assertTypeAnnotations();
}
test_compoundAssignment_propertyAccess() async {
@ -370,7 +352,7 @@ void test9(Test<double, double> t) {
var t5 = 'new Test<num, num>()';
var t8 = 'new Test<double, num>()';
var t9 = 'new Test<double, double>()';
var code = '''
await assertNoErrorsInCode('''
int getInt() => 0;
num getNum() => 0;
double getDouble() => 0.0;
@ -444,17 +426,12 @@ void test9() {
var /*@type=double*/ v10 = ++$t9.x;
var /*@type=double*/ v11 = $t9.x++;
}
''';
var source = addSource(code);
var analysisResult = await computeAnalysisResult(source);
assertNoErrors(source);
var unit = analysisResult.unit;
_assertTypeAnnotations(code, unit);
''');
_assertTypeAnnotations();
}
test_compoundAssignment_simpleIdentifier() async {
var code = r'''
await assertNoErrorsInCode(r'''
int getInt() => 0;
num getNum() => 0;
double getDouble() => 0.0;
@ -538,17 +515,12 @@ class Test9 extends Test<double, double> {
var /*@type=double*/ v11 = x++;
}
}
''';
var source = addSource(code);
var analysisResult = await computeAnalysisResult(source);
assertNoErrors(source);
var unit = analysisResult.unit;
_assertTypeAnnotations(code, unit);
''');
_assertTypeAnnotations();
}
test_compoundAssignment_simpleIdentifier_topLevel() async {
var code = r'''
await assertNoErrorsInCode(r'''
class A {}
class B extends A {
@ -562,13 +534,8 @@ void set topLevel(A value) {}
main() {
var /*@type=B*/ v = topLevel += 1;
}
''';
var source = addSource(code);
var analysisResult = await computeAnalysisResult(source);
assertNoErrors(source);
var unit = analysisResult.unit;
_assertTypeAnnotations(code, unit);
''');
_assertTypeAnnotations();
}
test_forIn_identifier() async {
@ -592,12 +559,11 @@ class C {
for (aTopLevelSetter in f()) {} // top setter
}
}''';
var source = addSource(code);
var analysisResult = await computeAnalysisResult(source);
var unit = analysisResult.unit;
addTestFile(code);
await resolveTestFile();
void assertType(String prefix) {
var invocation = _findMethodInvocation(unit, code, prefix);
var invocation = findNode.methodInvocation(prefix);
expect(invocation.staticType.toString(), 'Iterable<A>');
}
@ -618,35 +584,34 @@ void test(Iterable<num> iter) {
for (num y in f()) {} // 3
}
''';
var source = addSource(code);
var analysisResult = await computeAnalysisResult(source);
var unit = analysisResult.unit;
addTestFile(code);
await resolveTestFile();
{
var node = EngineTestCase.findSimpleIdentifier(unit, code, 'w in');
var node = findNode.simple('w in');
VariableElement element = node.staticElement;
expect(node.staticType, typeProvider.dynamicType);
expect(element.type, typeProvider.dynamicType);
var invocation = _findMethodInvocation(unit, code, 'f()) {} // 1');
var invocation = findNode.methodInvocation('f()) {} // 1');
expect(invocation.staticType.toString(), 'Iterable<dynamic>');
}
{
var node = EngineTestCase.findSimpleIdentifier(unit, code, 'x in');
var node = findNode.simple('x in');
VariableElement element = node.staticElement;
expect(node.staticType, typeProvider.numType);
expect(element.type, typeProvider.numType);
}
{
var node = EngineTestCase.findSimpleIdentifier(unit, code, 'y in');
var node = findNode.simple('y in');
VariableElement element = node.staticElement;
expect(node.staticType, typeProvider.numType);
expect(element.type, typeProvider.numType);
var invocation = _findMethodInvocation(unit, code, 'f()) {} // 3');
var invocation = findNode.methodInvocation('f()) {} // 3');
expect(invocation.staticType.toString(), 'Iterable<num>');
}
}
@ -666,16 +631,15 @@ void test(List<A> listA, List<B> listB) {
for (B b3 in f(listB)) {} // 5
}
''';
var source = addSource(code);
var analysisResult = await computeAnalysisResult(source);
var unit = analysisResult.unit;
addTestFile(code);
await resolveTestFile();
void assertTypes(
String vSearch, String vType, String fSearch, String fType) {
var node = EngineTestCase.findSimpleIdentifier(unit, code, vSearch);
var node = findNode.simple(vSearch);
expect(node.staticType.toString(), vType);
var invocation = _findMethodInvocation(unit, code, fSearch);
var invocation = findNode.methodInvocation(fSearch);
expect(invocation.staticType.toString(), fType);
}
@ -693,11 +657,10 @@ class C {
operator []=(int index, double value) => null;
}
''';
var source = addSource(code);
var analysisResult = await computeAnalysisResult(source);
var unit = analysisResult.unit;
addTestFile(code);
await resolveTestFile();
ClassElement c = unit.declaredElement.getType('C');
ClassElement c = findElement.class_('C');
PropertyAccessorElement x = c.accessors[0];
expect(x.returnType, VoidTypeImpl.instance);
@ -717,11 +680,10 @@ class Derived extends Base {
set x(_) {}
operator[]=(int x, int y) {}
}''';
var source = addSource(code);
var analysisResult = await computeAnalysisResult(source);
var unit = analysisResult.unit;
addTestFile(code);
await resolveTestFile();
ClassElement c = unit.declaredElement.getType('Derived');
ClassElement c = findElement.class_('Derived');
PropertyAccessorElement x = c.accessors[0];
expect(x.returnType, VoidTypeImpl.instance);
@ -738,10 +700,10 @@ void main() {
f((x) {});
}
''';
var source = addSource(code);
var analysisResult = await computeAnalysisResult(source);
var unit = analysisResult.unit;
var xNode = EngineTestCase.findSimpleIdentifier(unit, code, 'x) {}');
addTestFile(code);
await resolveTestFile();
var xNode = findNode.simple('x) {}');
VariableElement xElement = xNode.staticElement;
expect(xNode.staticType, typeProvider.objectType);
expect(xElement.type, typeProvider.objectType);
@ -752,14 +714,13 @@ void main() {
var x = [];
var y = {};
''';
var source = addSource(code);
var analysisResult = await computeAnalysisResult(source);
var unit = analysisResult.unit;
addTestFile(code);
await resolveTestFile();
SimpleIdentifier x = _findExpression(unit, code, 'x = ');
SimpleIdentifier x = findNode.expression('x = ');
expect(x.staticType.toString(), 'List<dynamic>');
SimpleIdentifier y = _findExpression(unit, code, 'y = ');
SimpleIdentifier y = findNode.expression('y = ');
expect(y.staticType.toString(), 'Map<dynamic, dynamic>');
}
@ -768,14 +729,13 @@ var y = {};
var x = [null];
var y = {null: null};
''';
var source = addSource(code);
var analysisResult = await computeAnalysisResult(source);
var unit = analysisResult.unit;
addTestFile(code);
await resolveTestFile();
SimpleIdentifier x = _findExpression(unit, code, 'x = ');
SimpleIdentifier x = findNode.expression('x = ');
expect(x.staticType.toString(), 'List<Null>');
SimpleIdentifier y = _findExpression(unit, code, 'y = ');
SimpleIdentifier y = findNode.expression('y = ');
expect(y.staticType.toString(), 'Map<Null, Null>');
}
@ -793,11 +753,10 @@ void test(C<int> x) {
break;
}
}''';
var source = addSource(code);
var analysisResult = await computeAnalysisResult(source);
var unit = analysisResult.unit;
addTestFile(code);
await resolveTestFile();
var node = _findInstanceCreation(unit, code, 'const C():');
var node = findNode.instanceCreation('const C():');
expect(node.staticType.toString(), 'C<int>');
}
@ -811,14 +770,13 @@ main() {
var y = new C().m();
}
''';
var source = addSource(code);
var analysisResult = await computeAnalysisResult(source);
var unit = analysisResult.unit;
addTestFile(code);
await resolveTestFile();
SimpleIdentifier x = _findExpression(unit, code, 'x = ');
SimpleIdentifier x = findNode.expression('x = ');
expect(x.staticType, VoidTypeImpl.instance);
SimpleIdentifier y = _findExpression(unit, code, 'y = ');
SimpleIdentifier y = findNode.expression('y = ');
expect(y.staticType, VoidTypeImpl.instance);
}
@ -830,18 +788,20 @@ main() {
var y = f();
}
''';
var source = addSource(code);
var analysisResult = await computeAnalysisResult(source);
var unit = analysisResult.unit;
addTestFile(code);
await resolveTestFile();
SimpleIdentifier x = _findExpression(unit, code, 'x = ');
SimpleIdentifier x = findNode.expression('x = ');
expect(x.staticType, VoidTypeImpl.instance);
SimpleIdentifier y = _findExpression(unit, code, 'y = ');
SimpleIdentifier y = findNode.expression('y = ');
expect(y.staticType, VoidTypeImpl.instance);
}
void _assertTypeAnnotations(String code, CompilationUnit unit) {
void _assertTypeAnnotations() {
var code = result.content;
var unit = result.unit;
var types = <int, String>{};
{
int lastIndex = 0;
@ -858,43 +818,18 @@ main() {
lastIndex = closeIndex;
}
}
unit.accept(new _TypeAnnotationsValidator(types));
}
Expression _findExpression(AstNode root, String code, String prefix) {
return EngineTestCase.findNode(root, code, prefix, (n) {
return n is Expression;
});
}
InstanceCreationExpression _findInstanceCreation(
AstNode root, String code, String prefix) {
return EngineTestCase.findNode(root, code, prefix, (n) {
return n is InstanceCreationExpression;
});
}
MethodInvocation _findMethodInvocation(
AstNode root, String code, String prefix) {
return EngineTestCase.findNode(root, code, prefix, (n) {
return n is MethodInvocation;
});
}
}
class _TypeAnnotationsValidator extends RecursiveAstVisitor {
final Map<int, String> types;
_TypeAnnotationsValidator(this.types);
void visitSimpleIdentifier(SimpleIdentifier node) {
Token comment = node.token.precedingComments;
if (comment != null) {
String expectedType = types[comment.offset];
if (expectedType != null) {
String actualType = node.staticType.toString();
expect(actualType, expectedType, reason: '@${comment.offset}');
}
}
unit.accept(FunctionAstVisitor(
simpleIdentifier: (node) {
Token comment = node.token.precedingComments;
if (comment != null) {
String expectedType = types[comment.offset];
if (expectedType != null) {
String actualType = node.staticType.toString();
expect(actualType, expectedType, reason: '@${comment.offset}');
}
}
},
));
}
}