Move literal inference tests from StaticTypeAnalyzer2Test.

R=brianwilkerson@google.com

Change-Id: Ie08a60dcd1a0942e5e72ae3a1ed4bb76238786db
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/174460
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
This commit is contained in:
Konstantin Shcheglov 2020-11-30 20:30:03 +00:00 committed by commit-bot@chromium.org
parent 295a9a856e
commit 49ca82c4e8
5 changed files with 40 additions and 89 deletions

View file

@ -284,17 +284,6 @@ class ResolutionVerifier extends RecursiveAstVisitor<void> {
/// Shared infrastructure for [StaticTypeAnalyzer2Test].
class StaticTypeAnalyzer2TestShared extends PubPackageResolutionTest {
/// Find the expression that starts at the offset of [search] and validate its
/// that its static type matches the given [type].
///
/// If [type] is a string, validates that the expression's static type
/// stringifies to that text. Otherwise, [type] is used directly a [Matcher]
/// to match the type.
void expectExpressionType(String search, type) {
Expression expression = findNode.expression(search);
_expectType(expression.staticType, type);
}
/// Looks up the identifier with [name] and validates that its type type
/// stringifies to [type] and that its generics match the given stringified
/// output.

View file

@ -29,7 +29,6 @@ import 'test_support.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(SetLiteralsTest);
defineReflectiveTests(StaticTypeAnalyzerTest);
defineReflectiveTests(StaticTypeAnalyzer2Test);
});
@ -44,83 +43,9 @@ void _fail(String message) {
fail(message);
}
@reflectiveTest
class SetLiteralsTest extends StaticTypeAnalyzer2TestShared {
test_emptySetLiteral_parameter_typed() async {
await assertNoErrorsInCode(r'''
main() {
useSet({});
}
void useSet(Set<int> s) {
}
''');
expectExpressionType('{}', 'Set<int>');
}
}
/// Like [StaticTypeAnalyzerTest], but as end-to-end tests.
@reflectiveTest
class StaticTypeAnalyzer2Test extends StaticTypeAnalyzer2TestShared {
test_emptyListLiteral_inferredFromLinkedList() async {
await assertErrorsInCode(r'''
abstract class ListImpl<T> implements List<T> {}
ListImpl<int> f() => [];
''', [
error(CompileTimeErrorCode.INVALID_CAST_LITERAL_LIST, 70, 2),
]);
expectExpressionType('[]', 'List<dynamic>');
}
test_emptyMapLiteral_inferredFromLinkedHashMap() async {
await assertErrorsInCode(r'''
import 'dart:collection';
LinkedHashMap<int, int> f() => {};
''', [
error(CompileTimeErrorCode.INVALID_CAST_LITERAL_MAP, 57, 2),
]);
expectExpressionType('{}', 'Map<dynamic, dynamic>');
}
test_emptyMapLiteral_initializer_var() async {
await assertErrorsInCode(r'''
main() {
var v = {};
}
''', [
error(HintCode.UNUSED_LOCAL_VARIABLE, 15, 1),
]);
expectExpressionType('{}', 'Map<dynamic, dynamic>');
}
test_emptyMapLiteral_parameter_typed() async {
await assertNoErrorsInCode(r'''
main() {
useMap({});
}
void useMap(Map<int, int> m) {
}
''');
expectExpressionType('{}', 'Map<int, int>');
}
test_emptySetLiteral_inferredFromLinkedHashSet() async {
await assertErrorsInCode(r'''
import 'dart:collection';
LinkedHashSet<int> f() => {};
''', [
error(CompileTimeErrorCode.INVALID_CAST_LITERAL_SET, 52, 2),
]);
expectExpressionType('{}', 'Set<dynamic>');
}
test_emptySetLiteral_initializer_typed_nested() async {
await assertNoErrorsInCode(r'''
Set<Set<int>> ints = {{}};
''');
expectExpressionType('{}', 'Set<int>');
expectExpressionType('{{}}', 'Set<Set<int>>');
}
test_FunctionExpressionInvocation_block() async {
await assertErrorsInCode(r'''
main() {

View file

@ -32,7 +32,16 @@ List<int> a = [1];
assertType(findNode.listLiteral('['), 'List<int>');
}
test_context_noTypeArgs_noElements() async {
test_context_noTypeArgs_noElements_fromReturnType() async {
await assertNoErrorsInCode('''
List<int> f() {
return [];
}
''');
assertType(findNode.listLiteral('[]'), 'List<int>');
}
test_context_noTypeArgs_noElements_fromVariableType() async {
await assertNoErrorsInCode('''
List<String> a = [];
''');

View file

@ -55,7 +55,17 @@ FutureOr<Map<int, String>> f() {
assertType(setOrMapLiteral('{};'), 'Map<int, String>');
}
test_context_noTypeArgs_noEntries() async {
test_context_noTypeArgs_noEntries_fromParameterType() async {
await assertNoErrorsInCode('''
void f() {
useMap({});
}
void useMap(Map<int, String> _) {}
''');
assertType(setOrMapLiteral('{})'), 'Map<int, String>');
}
test_context_noTypeArgs_noEntries_fromVariableType() async {
await assertNoErrorsInCode('''
Map<String, String> a = {};
''');

View file

@ -35,13 +35,31 @@ Set<int> a = {1};
assertType(setLiteral('{'), 'Set<int>');
}
test_context_noTypeArgs_noElements() async {
test_context_noTypeArgs_noElements_fromParameterType() async {
await assertNoErrorsInCode('''
void f() {
useSet({});
}
void useSet(Set<int> _) {}
''');
assertType(setLiteral('{});'), 'Set<int>');
}
test_context_noTypeArgs_noElements_fromVariableType() async {
await assertNoErrorsInCode('''
Set<String> a = {};
''');
assertType(setLiteral('{'), 'Set<String>');
}
test_context_noTypeArgs_noElements_fromVariableType_nested() async {
await assertNoErrorsInCode('''
Set<Set<String>> a = {{}};
''');
assertType(setLiteral('{}'), 'Set<String>');
assertType(setLiteral('{{}}'), 'Set<Set<String>>');
}
test_context_noTypeArgs_noElements_futureOr() async {
await resolveTestCode('''
import 'dart:async';