blob: b3fba288dacea1f32e587d5b65bfaf6fc5a63ca1 [file] [log] [blame]
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
// 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/src/error/codes.dart';
import 'package:analyzer/src/generated/engine.dart';
import 'package:analyzer/src/test_utilities/package_mixin.dart';
import 'package:test/test.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/driver_resolution.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(InferenceFailureOnInstanceCreationTest);
});
}
@reflectiveTest
class InferenceFailureOnInstanceCreationTest extends DriverResolutionTest
with PackageMixin {
@override
AnalysisOptionsImpl get analysisOptions =>
AnalysisOptionsImpl()..strictInference = true;
test_constructorNames_named() async {
await assertErrorsInCode('''
import 'dart:collection';
void f() {
HashMap.from({1: 1, 2: 2, 3: 3});
}
''', [
error(HintCode.INFERENCE_FAILURE_ON_INSTANCE_CREATION, 39, 12),
]);
expect(result.errors[0].message, contains("'HashMap.from'"));
}
test_constructorNames_unnamed() async {
await assertErrorsInCode('''
import 'dart:collection';
void f() {
HashMap();
}
''', [
error(HintCode.INFERENCE_FAILURE_ON_INSTANCE_CREATION, 39, 7),
]);
expect(result.errors[0].message, contains("'HashMap'"));
}
test_explicitTypeArgument() async {
await assertNoErrorsInCode(r'''
import 'dart:collection';
void f() {
HashMap<int, int>();
}
''');
}
test_missingTypeArgument_downwardInference() async {
await assertNoErrorsInCode(r'''
import 'dart:collection';
HashMap<int, int> f() {
return HashMap();
}
''');
}
test_missingTypeArgument_noInference() async {
await assertErrorsInCode(r'''
import 'dart:collection';
void f() {
HashMap();
}
''', [
error(HintCode.INFERENCE_FAILURE_ON_INSTANCE_CREATION, 39, 7),
]);
}
test_missingTypeArgument_noInference_optionalTypeArgs() async {
addMetaPackage();
await assertNoErrorsInCode(r'''
import 'package:meta/meta.dart';
@optionalTypeArgs
class C<T> {}
void f() {
C();
}
''');
}
test_missingTypeArgument_upwardInference() async {
await assertNoErrorsInCode(r'''
import 'dart:collection';
void f() {
HashMap.of({1: 1, 2: 2, 3: 3});
}
''');
}
}