Test all methods in semantic_visitor_test.

BUG=
R=karlklose@google.com

Review URL: https://codereview.chromium.org//1152013006
This commit is contained in:
Johnni Winther 2015-06-07 10:30:55 +02:00
parent add5a6910d
commit deb1697fa9
4 changed files with 350 additions and 24 deletions

View file

@ -4078,6 +4078,7 @@ class TraversalSendMixin<R, A> implements SemanticSendVisitor<R, A> {
Selector selector,
Node rhs,
A arg) {
apply(receiver, arg);
apply(rhs, arg);
return null;
}

View file

@ -168,38 +168,52 @@ class Test {
}
const List<VisitKind> UNTESTABLE_KINDS = const <VisitKind>[
// A final field shadowing a non-final field is currently not supported in
// resolution.
VisitKind.VISIT_SUPER_FIELD_FIELD_COMPOUND,
VisitKind.VISIT_SUPER_FIELD_FIELD_PREFIX,
VisitKind.VISIT_SUPER_FIELD_FIELD_POSTFIX,
// Combination of method and setter with the same name is currently not
// supported by the element model.
VisitKind.VISIT_STATIC_METHOD_SETTER_COMPOUND,
VisitKind.VISIT_STATIC_METHOD_SETTER_PREFIX,
VisitKind.VISIT_STATIC_METHOD_SETTER_POSTFIX,
VisitKind.VISIT_TOP_LEVEL_METHOD_SETTER_COMPOUND,
VisitKind.VISIT_TOP_LEVEL_METHOD_SETTER_PREFIX,
VisitKind.VISIT_TOP_LEVEL_METHOD_SETTER_POSTFIX,
VisitKind.VISIT_SUPER_FIELD_FIELD_COMPOUND,
VisitKind.VISIT_SUPER_FIELD_FIELD_PREFIX,
VisitKind.VISIT_SUPER_FIELD_FIELD_POSTFIX,
VisitKind.VISIT_SUPER_METHOD_SETTER_COMPOUND,
VisitKind.VISIT_SUPER_METHOD_SETTER_PREFIX,
VisitKind.VISIT_SUPER_METHOD_SETTER_POSTFIX,
// Invalid use of setters is currently reported through an erroneous element.
VisitKind.VISIT_STATIC_SETTER_INVOKE,
VisitKind.VISIT_STATIC_SETTER_GET,
VisitKind.VISIT_TOP_LEVEL_SETTER_GET,
VisitKind.VISIT_TOP_LEVEL_SETTER_INVOKE,
// The constant expressions of assignment to constant type literals cannot be
// handled the compile constant evaluator.
VisitKind.VISIT_CLASS_TYPE_LITERAL_SET,
VisitKind.VISIT_TYPEDEF_TYPE_LITERAL_SET,
VisitKind.VISIT_TYPE_VARIABLE_TYPE_LITERAL_SET,
VisitKind.VISIT_DYNAMIC_TYPE_LITERAL_SET,
// Invalid assignments is currently report through an erroneous element.
VisitKind.VISIT_TYPE_VARIABLE_TYPE_LITERAL_SET,
VisitKind.VISIT_FINAL_PARAMETER_SET,
VisitKind.VISIT_FINAL_LOCAL_VARIABLE_SET,
VisitKind.VISIT_LOCAL_FUNCTION_SET,
VisitKind.VISIT_STATIC_GETTER_SET,
VisitKind.VISIT_STATIC_SETTER_GET,
VisitKind.VISIT_STATIC_SETTER_INVOKE,
VisitKind.VISIT_FINAL_STATIC_FIELD_SET,
VisitKind.VISIT_STATIC_FUNCTION_SET,
VisitKind.VISIT_FINAL_TOP_LEVEL_FIELD_SET,
VisitKind.VISIT_TOP_LEVEL_GETTER_SET,
VisitKind.VISIT_TOP_LEVEL_SETTER_GET,
VisitKind.VISIT_TOP_LEVEL_SETTER_INVOKE,
VisitKind.VISIT_TOP_LEVEL_FUNCTION_SET,
VisitKind.VISIT_FINAL_SUPER_FIELD_SET,
VisitKind.VISIT_SUPER_GETTER_SET,
VisitKind.VISIT_SUPER_METHOD_SET,
// The only undefined unary, `+`, is currently handled and skipped in the
// parser.
VisitKind.ERROR_UNDEFINED_UNARY_EXPRESSION,
// Constant expression are currently not computed during resolution.
VisitKind.VISIT_CONSTANT_GET,
VisitKind.VISIT_CONSTANT_INVOKE,
];
main(List<String> arguments) {
@ -245,7 +259,8 @@ main(List<String> arguments) {
m.simpleName != #apply)
.map((m) => m.simpleName).toSet();
symbols2.removeAll(symbols1);
print("Untested visit methods:\n ${symbols2.join(',\n ')},\n");
Expect.isTrue(symbols2.isEmpty,
"Untested visit methods:\n ${symbols2.join(',\n ')},\n");
}
], (f) => f()));
}
@ -281,15 +296,13 @@ Future test(Set<VisitKind> unvisitedKinds,
sourceFiles['main.dart'] = mainSource.toString();
Compiler compiler = compilerFor(sourceFiles,
options: ['--analyze-all', '--analyze-only']);
options: ['--analyze-all',
'--analyze-only',
'--enable-null-aware-operators']);
return compiler.run(Uri.parse('memory:main.dart')).then((_) {
testMap.forEach((String filename, Test test) {
LibraryElement library = compiler.libraryLoader.lookupLibrary(
Uri.parse('memory:$filename'));
var expectedVisits = test.expectedVisits;
if (expectedVisits is! List) {
expectedVisits = [expectedVisits];
}
Element element;
String cls = test.cls;
String method = test.method;
@ -302,6 +315,18 @@ Future test(Set<VisitKind> unvisitedKinds,
"${library.compilationUnit.script.text}");
element = classElement.localLookup(method);
}
var expectedVisits = test.expectedVisits;
if (expectedVisits == null) {
Expect.isTrue(element.isErroneous,
"Element '$method' expected to be have parse errors in:\n"
"${library.compilationUnit.script.text}");
return;
} else if (expectedVisits is! List) {
expectedVisits = [expectedVisits];
}
Expect.isFalse(element.isErroneous,
"Element '$method' is not expected to be have parse errors in:\n"
"${library.compilationUnit.script.text}");
void testAstElement(AstElement astElement) {
Expect.isNotNull(astElement, "Element '$method' not found in:\n"
@ -666,5 +691,18 @@ enum VisitKind {
VISIT_UNRESOLVED_PREFIX,
VISIT_UNRESOLVED_POSTFIX,
// TODO(johnniwinther): Add tests for more error cases.
VISIT_IF_NULL,
VISIT_IF_NOT_NULL_DYNAMIC_PROPERTY_GET,
VISIT_IF_NOT_NULL_DYNAMIC_PROPERTY_SET,
VISIT_IF_NOT_NULL_DYNAMIC_PROPERTY_INVOKE,
VISIT_IF_NOT_NULL_DYNAMIC_PROPERTY_COMPOUND,
VISIT_IF_NOT_NULL_DYNAMIC_PROPERTY_PREFIX,
VISIT_IF_NOT_NULL_DYNAMIC_PROPERTY_POSTFIX,
ERROR_INVALID_ASSERT,
ERROR_UNDEFINED_UNARY_EXPRESSION,
ERROR_UNDEFINED_BINARY_EXPRESSION,
VISIT_CONSTANT_GET,
VISIT_CONSTANT_INVOKE,
}

View file

@ -306,7 +306,7 @@ const Map<String, List<Test>> SEND_TESTS = const {
const Test.clazz(
'''
class C {
static static get o => 42;
static get o => 42;
m() { o = 42; }
}
''',
@ -316,7 +316,7 @@ const Map<String, List<Test>> SEND_TESTS = const {
const Test.clazz(
'''
class C {
static static get o => 42;
static get o => 42;
m() { C.o = 42; }
}
''',
@ -326,7 +326,7 @@ const Map<String, List<Test>> SEND_TESTS = const {
const Test.prefix(
'''
class C {
static static get o => 42;
static get o => 42;
}
''',
'm() { p.C.o = 42; }',
@ -1382,6 +1382,16 @@ const Map<String, List<Test>> SEND_TESTS = const {
m() { assert(false); }
''',
const Visit(VisitKind.VISIT_ASSERT, expression: 'false')),
const Test(
'''
m() { assert(); }
''',
const Visit(VisitKind.ERROR_INVALID_ASSERT, arguments: '()')),
const Test(
'''
m() { assert(42, true); }
''',
const Visit(VisitKind.ERROR_INVALID_ASSERT, arguments: '(42,true)')),
],
'Logical and': const [
// Logical and
@ -1541,6 +1551,18 @@ const Map<String, List<Test>> SEND_TESTS = const {
const Visit(VisitKind.VISIT_UNRESOLVED_SUPER_BINARY,
operator: '+',
right: '42')),
const Test(
'''
m() => 2 === 3;
''',
const Visit(VisitKind.ERROR_UNDEFINED_BINARY_EXPRESSION,
left: '2', operator: '===', right: '3')),
const Test(
'''
m() => 2 !== 3;
''',
const Visit(VisitKind.ERROR_UNDEFINED_BINARY_EXPRESSION,
left: '2', operator: '!==', right: '3')),
],
'Index': const [
// Index
@ -1782,6 +1804,14 @@ const Map<String, List<Test>> SEND_TESTS = const {
m() => !0;
''',
const Visit(VisitKind.VISIT_NOT, expression: '0')),
const Test(
'''
m() => +false;
''',
// TODO(johnniwinther): Should this be an
// ERROR_UNDEFINED_UNARY_EXPRESSION? Currently the parser just skips
// the `+`.
const []),
],
'Index set': const [
// Index set
@ -1851,7 +1881,7 @@ const Map<String, List<Test>> SEND_TESTS = const {
const Test(
'''
m() {
final a;
final a = 0;
a += 42;
}
''',
@ -2125,7 +2155,7 @@ const Map<String, List<Test>> SEND_TESTS = const {
var a;
}
class B extends A {
final a;
final a = 0;
}
class C extends B {
@ -3367,4 +3397,116 @@ const Map<String, List<Test>> SEND_TESTS = const {
type: 'Class',
selector: 'CallStructure(arity=2)')),
],
'If not null expressions': const [
const Test(
'''
m(a) => a?.b;
''',
const [
const Visit(
VisitKind.VISIT_IF_NOT_NULL_DYNAMIC_PROPERTY_GET,
receiver: 'a',
name: 'b'),
const Visit(
VisitKind.VISIT_PARAMETER_GET,
element: 'parameter(m#a)'),
]),
const Test(
'''
m(a) => a?.b = 42;
''',
const [
const Visit(
VisitKind.VISIT_IF_NOT_NULL_DYNAMIC_PROPERTY_SET,
receiver: 'a',
name: 'b',
rhs: '42'),
const Visit(
VisitKind.VISIT_PARAMETER_GET,
element: 'parameter(m#a)'),
]),
const Test(
'''
m(a) => a?.b(42, true);
''',
const [
const Visit(
VisitKind.VISIT_IF_NOT_NULL_DYNAMIC_PROPERTY_INVOKE,
receiver: 'a',
arguments: '(42,true)',
selector: 'Selector(call, b, arity=2)'),
const Visit(
VisitKind.VISIT_PARAMETER_GET,
element: 'parameter(m#a)'),
]),
const Test(
'''
m(a) => ++a?.b;
''',
const [
const Visit(
VisitKind.VISIT_IF_NOT_NULL_DYNAMIC_PROPERTY_PREFIX,
receiver: 'a',
getter: 'Selector(getter, b, arity=0)',
setter: 'Selector(setter, b, arity=1)',
operator: '++'),
const Visit(
VisitKind.VISIT_PARAMETER_GET,
element: 'parameter(m#a)'),
]),
const Test(
'''
m(a) => a?.b--;
''',
const [
const Visit(
VisitKind.VISIT_IF_NOT_NULL_DYNAMIC_PROPERTY_POSTFIX,
receiver: 'a',
getter: 'Selector(getter, b, arity=0)',
setter: 'Selector(setter, b, arity=1)',
operator: '--'),
const Visit(
VisitKind.VISIT_PARAMETER_GET,
element: 'parameter(m#a)'),
]),
const Test(
'''
m(a) => a?.b *= 42;
''',
const [
const Visit(
VisitKind.VISIT_IF_NOT_NULL_DYNAMIC_PROPERTY_COMPOUND,
receiver: 'a',
getter: 'Selector(getter, b, arity=0)',
setter: 'Selector(setter, b, arity=1)',
operator: '*=',
rhs: '42'),
const Visit(
VisitKind.VISIT_PARAMETER_GET,
element: 'parameter(m#a)'),
]),
const Test(
'''
m(a, b) => a ?? b;
''',
const [
const Visit(VisitKind.VISIT_IF_NULL,
left: 'a', right: 'b'),
const Visit(
VisitKind.VISIT_PARAMETER_GET,
element: 'parameter(m#a)'),
const Visit(
VisitKind.VISIT_PARAMETER_GET,
element: 'parameter(m#b)'),
]),
const Test(
'''
m(a) => a ??= 42;
''',
const Visit(
VisitKind.VISIT_PARAMETER_COMPOUND,
element: 'parameter(m#a)',
operator: '??=',
rhs: '42')),
],
};

View file

@ -25,7 +25,16 @@ class SemanticSendTestVisitor extends SemanticTestVisitor {
Node expression,
arg) {
visits.add(new Visit(VisitKind.VISIT_ASSERT, expression: expression));
apply(expression, arg);
super.visitAssert(node, expression, arg);
}
@override
errorInvalidAssert(
Send node,
NodeList arguments,
arg) {
visits.add(new Visit(VisitKind.ERROR_INVALID_ASSERT, arguments: arguments));
super.errorInvalidAssert(node, arguments, arg);
}
@override
@ -37,8 +46,19 @@ class SemanticSendTestVisitor extends SemanticTestVisitor {
arg) {
visits.add(new Visit(VisitKind.VISIT_BINARY,
left: left, operator: operator, right: right));
apply(left, arg);
apply(right, arg);
super.visitBinary(node, left, operator, right, arg);
}
@override
errorUndefinedBinaryExpression(
Send node,
Node left,
Operator operator,
Node right,
arg) {
visits.add(new Visit(VisitKind.ERROR_UNDEFINED_BINARY_EXPRESSION,
left: left, operator: operator, right: right));
super.errorUndefinedBinaryExpression(node, left, operator, right, arg);
}
@override
@ -848,7 +868,18 @@ class SemanticSendTestVisitor extends SemanticTestVisitor {
arg) {
visits.add(new Visit(VisitKind.VISIT_UNARY,
expression: expression, operator: operator));
apply(expression, arg);
super.visitUnary(node, operator, expression, arg);
}
@override
errorUndefinedUnaryExpression(
Send node,
Operator operator,
Node expression,
arg) {
visits.add(new Visit(VisitKind.ERROR_UNDEFINED_UNARY_EXPRESSION,
expression: expression, operator: operator));
super.errorUndefinedUnaryExpression(node, operator, expression, arg);
}
@override
@ -2781,4 +2812,118 @@ class SemanticSendTestVisitor extends SemanticTestVisitor {
visits.add(new Visit(VisitKind.VISIT_UNRESOLVED_SUPER_SETTER_PREFIX,
getter: getter, operator: operator));
}
@override
visitIfNotNullDynamicPropertyGet(
Send node,
Node receiver,
Selector selector,
arg) {
visits.add(new Visit(VisitKind.VISIT_IF_NOT_NULL_DYNAMIC_PROPERTY_GET,
receiver: receiver, name: selector.name));
super.visitIfNotNullDynamicPropertyGet(node, receiver, selector, arg);
}
@override
visitIfNotNullDynamicPropertySet(
Send node,
Node receiver,
Selector selector,
Node rhs,
arg) {
visits.add(new Visit(VisitKind.VISIT_IF_NOT_NULL_DYNAMIC_PROPERTY_SET,
receiver: receiver, name: selector.name, rhs: rhs));
super.visitIfNotNullDynamicPropertySet(node, receiver, selector, rhs, arg);
}
@override
visitIfNotNullDynamicPropertyInvoke(
Send node,
Node receiver,
NodeList arguments,
Selector selector,
arg) {
visits.add(new Visit(VisitKind.VISIT_IF_NOT_NULL_DYNAMIC_PROPERTY_INVOKE,
receiver: receiver, selector: selector, arguments: arguments));
super.visitIfNotNullDynamicPropertyInvoke(
node, receiver, arguments, selector, arg);
}
@override
visitIfNotNullDynamicPropertyPrefix(
Send node,
Node receiver,
IncDecOperator operator,
Selector getterSelector,
Selector setterSelector,
arg) {
visits.add(new Visit(VisitKind.VISIT_IF_NOT_NULL_DYNAMIC_PROPERTY_PREFIX,
receiver: receiver, operator: operator,
getter: getterSelector, setter: setterSelector));
super.visitIfNotNullDynamicPropertyPrefix(
node, receiver, operator, getterSelector, setterSelector, arg);
}
@override
visitIfNotNullDynamicPropertyPostfix(
Send node,
Node receiver,
IncDecOperator operator,
Selector getterSelector,
Selector setterSelector,
arg) {
visits.add(new Visit(VisitKind.VISIT_IF_NOT_NULL_DYNAMIC_PROPERTY_POSTFIX,
receiver: receiver, operator: operator,
getter: getterSelector, setter: setterSelector));
super.visitIfNotNullDynamicPropertyPostfix(
node, receiver, operator, getterSelector, setterSelector, arg);
}
@override
visitIfNotNullDynamicPropertyCompound(
Send node,
Node receiver,
AssignmentOperator operator,
Node rhs,
Selector getterSelector,
Selector setterSelector,
arg) {
visits.add(new Visit(VisitKind.VISIT_IF_NOT_NULL_DYNAMIC_PROPERTY_COMPOUND,
receiver: receiver, operator: operator, rhs: rhs,
getter: getterSelector, setter: setterSelector));
super.visitIfNotNullDynamicPropertyCompound(
node, receiver, operator, rhs, getterSelector, setterSelector, arg);
}
@override
visitIfNull(
Send node,
Node left,
Node right,
arg) {
visits.add(new Visit(VisitKind.VISIT_IF_NULL, left: left, right: right));
super.visitIfNull(node, left, right, arg);
}
@override
visitConstantGet(
Send node,
ConstantExpression constant,
arg) {
visits.add(new Visit(VisitKind.VISIT_CONSTANT_GET,
constant: constant.getText()));
super.visitConstantGet(node, constant, arg);
}
@override
visitConstantInvoke(
Send node,
ConstantExpression constant,
NodeList arguments,
CallStructure callStructure,
arg) {
visits.add(new Visit(VisitKind.VISIT_CONSTANT_INVOKE,
constant: constant.getText()));
super.visitConstantInvoke(node, constant, arguments, callStructure, arg);
}
}