From 8a7276fb4de06c397fa4e7279e38cb8afb9899d4 Mon Sep 17 00:00:00 2001 From: Konstantin Shcheglov Date: Mon, 18 May 2026 08:27:45 -0700 Subject: [PATCH] CQ. Migrate resolution tests to resolveTestCodeWithDiagnostics. CD. Change-Id: I621af89bafde3fc8b3e492482cfe33ece11ad844 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/503702 Reviewed-by: Johnni Winther Commit-Queue: Konstantin Shcheglov --- .../cascade_expression_resolution_test.dart | 16 +- .../dart/resolution/cast_pattern_test.dart | 23 +- .../src/dart/resolution/class_alias_test.dart | 53 +- .../test/src/dart/resolution/class_test.dart | 827 ++++++++---------- .../src/dart/resolution/comment_test.dart | 183 ++-- .../conditional_expression_test.dart | 55 +- .../resolution/constant_pattern_test.dart | 52 +- .../src/dart/resolution/constant_test.dart | 69 +- .../constructor_field_initializer_test.dart | 102 +-- .../constructor_reference_test.dart | 227 ++--- .../src/dart/resolution/constructor_test.dart | 73 +- .../declared_variable_pattern_test.dart | 96 +- ...shorthand_constructor_invocation_test.dart | 347 +++----- .../dot_shorthand_invocation_test.dart | 280 +++--- .../dot_shorthand_property_access_test.dart | 224 ++--- 15 files changed, 1168 insertions(+), 1459 deletions(-) diff --git a/pkg/analyzer/test/src/dart/resolution/cascade_expression_resolution_test.dart b/pkg/analyzer/test/src/dart/resolution/cascade_expression_resolution_test.dart index 690388532e8..cb687dcbcab 100644 --- a/pkg/analyzer/test/src/dart/resolution/cascade_expression_resolution_test.dart +++ b/pkg/analyzer/test/src/dart/resolution/cascade_expression_resolution_test.dart @@ -17,7 +17,7 @@ main() { @reflectiveTest class CascadeExpressionResolutionTest extends PubPackageResolutionTest { test_nullAware_indexGet_promotableField() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class C { final D? _d; C(this._d); @@ -46,7 +46,7 @@ SimpleIdentifier } test_nullAware_indexGet_promotableLocal() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' abstract class C { D? get d; } @@ -75,7 +75,7 @@ SimpleIdentifier } test_nullAware_indexSet_promotableLocal() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' abstract class C { D get d; void f(int i); @@ -103,7 +103,7 @@ SimpleIdentifier } test_nullAware_methodInvocation_promotableField() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class C { final D? _d; C(this._d); @@ -131,7 +131,7 @@ SimpleIdentifier } test_nullAware_methodInvocation_promotableLocal() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' abstract class C { D? get d; } @@ -158,7 +158,7 @@ SimpleIdentifier } test_nullAware_propertyGet_promotableField() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class C { final D? _d; C(this._d); @@ -187,7 +187,7 @@ SimpleIdentifier } test_nullAware_propertyGet_promotableLocal() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' abstract class C { D? get d; } @@ -216,7 +216,7 @@ SimpleIdentifier } test_nullAware_propertySet_promotableLocal() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' abstract class C { int? x; void f(int i); diff --git a/pkg/analyzer/test/src/dart/resolution/cast_pattern_test.dart b/pkg/analyzer/test/src/dart/resolution/cast_pattern_test.dart index 90be9d7916b..ddc74fd85ea 100644 --- a/pkg/analyzer/test/src/dart/resolution/cast_pattern_test.dart +++ b/pkg/analyzer/test/src/dart/resolution/cast_pattern_test.dart @@ -2,28 +2,28 @@ // 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/diagnostic/diagnostic.dart' as diag; import 'package:test_reflective_loader/test_reflective_loader.dart'; import 'context_collection_resolution.dart'; +import 'node_text_expectations.dart'; main() { defineReflectiveSuite(() { defineReflectiveTests(CastPatternResolutionTest); + defineReflectiveTests(UpdateNodeTextExpectations); }); } @reflectiveTest class CastPatternResolutionTest extends PubPackageResolutionTest { test_ifCase() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f(x) { if (x case var y as int) {} +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. } -''', - [error(diag.unusedLocalVariable, 29, 1)], - ); +'''); var node = findNode.singleGuardedPattern.pattern; assertResolvedNodeText(node, r''' CastPattern @@ -44,7 +44,7 @@ CastPattern } test_switchCase() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' void f(x) { const a = 0; switch (x) { @@ -72,14 +72,13 @@ CastPattern } test_variableDeclaration() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f(x) { var (a as int) = x; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'a' isn't used. } -''', - [error(diag.unusedLocalVariable, 19, 1)], - ); +'''); var node = findNode.singlePatternVariableDeclaration; assertResolvedNodeText(node, r''' PatternVariableDeclaration diff --git a/pkg/analyzer/test/src/dart/resolution/class_alias_test.dart b/pkg/analyzer/test/src/dart/resolution/class_alias_test.dart index e7fe5fb88bb..185824fead4 100644 --- a/pkg/analyzer/test/src/dart/resolution/class_alias_test.dart +++ b/pkg/analyzer/test/src/dart/resolution/class_alias_test.dart @@ -2,21 +2,22 @@ // 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/diagnostic/diagnostic.dart' as diag; import 'package:test_reflective_loader/test_reflective_loader.dart'; import 'context_collection_resolution.dart'; +import 'node_text_expectations.dart'; main() { defineReflectiveSuite(() { defineReflectiveTests(ClassTypeAliasResolutionTest); + defineReflectiveTests(UpdateNodeTextExpectations); }); } @reflectiveTest class ClassTypeAliasResolutionTest extends PubPackageResolutionTest { // solo_test_X() async { - // await assertNoErrorsInCode(r''' + // await resolveTestCodeWithDiagnostics(r''' // '''); // // final node = findNode.singleListLiteral; @@ -25,7 +26,7 @@ class ClassTypeAliasResolutionTest extends PubPackageResolutionTest { // } test_element() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A {} mixin class B {} class C {} @@ -63,45 +64,42 @@ ClassTypeAlias } test_element_typeFunction_extends() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' mixin class A {} class X = Function with A; -''', - [error(diag.finalClassExtendedOutsideOfLibrary, 27, 8)], - ); +// ^^^^^^^^ +// [diag.finalClassExtendedOutsideOfLibrary] The class 'Function' can't be extended outside of its library because it's a final class. +'''); var x = findElement2.class_('X'); assertType(x.supertype, 'Object'); } test_element_typeFunction_implements() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' mixin class A {} class B {} class X = Object with A implements A, Function, B; -''', - [error(diag.finalClassImplementedOutsideOfLibrary, 66, 8)], - ); +// ^^^^^^^^ +// [diag.finalClassImplementedOutsideOfLibrary] The class 'Function' can't be implemented outside of its library because it's a final class. +'''); var x = findElement2.class_('X'); assertElementTypes(x.interfaces, ['A', 'B']); } test_element_typeFunction_with() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' mixin class A {} mixin class B {} class X = Object with A, Function, B; -''', - [error(diag.classUsedAsMixin, 59, 8)], - ); +// ^^^^^^^^ +// [diag.classUsedAsMixin] The class 'Function' can't be used as a mixin because it's neither a mixin class nor a mixin. +'''); var x = findElement2.class_('X'); assertElementTypes(x.mixins, ['A', 'B']); } test_implicitConstructors_const() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { const A(); } @@ -115,8 +113,7 @@ const x = const C(); } test_implicitConstructors_const_field() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { const A(); } @@ -128,16 +125,14 @@ mixin M { class C = A with M; const x = const C(); -''', - [ - error(diag.constWithNonConst, 83, 5), - error(diag.constInitializedWithNonConstantValue, 83, 5), - ], - ); +// ^^^^^ +// [diag.constWithNonConst] The constructor being called isn't a const constructor. +// [diag.constInitializedWithNonConstantValue] Const variables must be initialized with a constant value. +'''); } test_implicitConstructors_const_getter() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { const A(); } @@ -153,7 +148,7 @@ const x = const C(); } test_implicitConstructors_const_setter() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { const A(); } diff --git a/pkg/analyzer/test/src/dart/resolution/class_test.dart b/pkg/analyzer/test/src/dart/resolution/class_test.dart index 5178f9f48d0..f71cb983a99 100644 --- a/pkg/analyzer/test/src/dart/resolution/class_test.dart +++ b/pkg/analyzer/test/src/dart/resolution/class_test.dart @@ -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 'package:analyzer/src/diagnostic/diagnostic.dart' as diag; import 'package:test_reflective_loader/test_reflective_loader.dart'; import 'context_collection_resolution.dart'; @@ -22,7 +21,7 @@ main() { @reflectiveTest class ClassDeclarationResolutionTest extends PubPackageResolutionTest { test_element_allSupertypes() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A {} mixin B {} mixin C {} @@ -66,7 +65,7 @@ class X5 extends A with B, C implements D, E {} } test_element_allSupertypes_generic() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A {} class B {} class C extends B {} @@ -92,83 +91,77 @@ class X3 extends C {} } test_element_allSupertypes_recursive() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A extends B {} +// ^ +// [diag.recursiveInterfaceInheritance] 'A' can't be a superinterface of itself: C, B, A. class B extends C {} +// ^ +// [diag.recursiveInterfaceInheritance] 'B' can't be a superinterface of itself: C, B, A. class C extends A {} +// ^ +// [diag.recursiveInterfaceInheritance] 'C' can't be a superinterface of itself: C, B, A. class X extends A {} -''', - [ - error(diag.recursiveInterfaceInheritance, 6, 1), - error(diag.recursiveInterfaceInheritance, 27, 1), - error(diag.recursiveInterfaceInheritance, 48, 1), - ], - ); +'''); assertElementTypes(findElement2.class_('X').allSupertypes, ['A', 'Object']); } test_element_typeFunction_extends() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A extends Function {} -''', - [error(diag.finalClassExtendedOutsideOfLibrary, 16, 8)], - ); +// ^^^^^^^^ +// [diag.finalClassExtendedOutsideOfLibrary] The class 'Function' can't be extended outside of its library because it's a final class. +'''); var a = findElement2.class_('A'); assertType(a.supertype, 'Object'); } test_element_typeFunction_extends_language219() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' // @dart = 2.19 class A extends Function {} -''', - [error(diag.deprecatedExtendsFunction, 32, 8)], - ); +// ^^^^^^^^ +// [diag.deprecatedExtendsFunction] Extending 'Function' is deprecated. +'''); var a = findElement2.class_('A'); assertType(a.supertype, 'Object'); } test_element_typeFunction_with() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' mixin A {} mixin B {} class C extends Object with A, Function, B {} -''', - [error(diag.classUsedAsMixin, 53, 8)], - ); +// ^^^^^^^^ +// [diag.classUsedAsMixin] The class 'Function' can't be used as a mixin because it's neither a mixin class nor a mixin. +'''); assertElementTypes(findElement2.class_('C').mixins, ['A', 'B']); } test_element_typeFunction_with_language219() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' // @dart = 2.19 mixin A {} mixin B {} class C extends Object with A, Function, B {} -''', - [error(diag.deprecatedMixinFunction, 69, 8)], - ); +// ^^^^^^^^ +// [diag.deprecatedMixinFunction] Mixing in 'Function' is deprecated. +'''); assertElementTypes(findElement2.class_('C').mixins, ['A', 'B']); } test_field_static_typeParameter() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { static T? foo; +// ^ +// [diag.typeParameterReferencedByStatic] Static members can't reference type parameters of the class. } -''', - [error(diag.typeParameterReferencedByStatic, 22, 1)], - ); +'''); var node = findNode.singleFieldDeclaration; assertResolvedNodeText(node, r''' @@ -190,35 +183,33 @@ FieldDeclaration } test_issue32815() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A extends B {} +// ^ +// [diag.recursiveInterfaceInheritance] 'A' can't be a superinterface of itself: B, A. class B extends A {} +// ^ +// [diag.recursiveInterfaceInheritance] 'B' can't be a superinterface of itself: B, A. class C extends B implements I {} abstract class I {} main() { Iterable> x = [new C()]; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'x' isn't used. } -''', - [ - error(diag.recursiveInterfaceInheritance, 6, 1), - error(diag.recursiveInterfaceInheritance, 33, 1), - error(diag.unusedLocalVariable, 150, 1), - ], - ); +'''); } test_method_static_typeParameter() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { static T? foo() {} +// ^ +// [diag.typeParameterReferencedByStatic] Static members can't reference type parameters of the class. } -''', - [error(diag.typeParameterReferencedByStatic, 22, 1)], - ); +'''); var node = findNode.singleMethodDeclaration; assertResolvedNodeText(node, r''' @@ -244,11 +235,9 @@ MethodDeclaration } test_nameWithTypeParameters_hasTypeParameters() async { - var code = r''' + await resolveTestCodeWithDiagnostics(r''' class A {} -'''; - - await assertNoErrorsInCode(code); +'''); var node = findNode.singleClassDeclaration; assertResolvedNodeText(node, r''' @@ -277,11 +266,9 @@ ClassDeclaration } test_nameWithTypeParameters_noTypeParameters() async { - var code = r''' + await resolveTestCodeWithDiagnostics(r''' class A {} -'''; - - await assertNoErrorsInCode(code); +'''); var node = findNode.singleClassDeclaration; assertResolvedNodeText(node, r''' @@ -297,7 +284,7 @@ ClassDeclaration } test_primaryConstructor_declaringFormalParameter_default_namedOptional_final() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A({final int a = 0}); '''); @@ -338,7 +325,7 @@ ClassDeclaration } test_primaryConstructor_declaringFormalParameter_default_namedRequired_final() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A({required final int a}); '''); @@ -375,7 +362,7 @@ ClassDeclaration } test_primaryConstructor_declaringFormalParameter_functionTyped_final() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A(final int a(String x)); '''); @@ -422,7 +409,7 @@ ClassDeclaration } test_primaryConstructor_declaringFormalParameter_simple_final() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A(final int a) {} '''); @@ -457,7 +444,7 @@ ClassDeclaration } test_primaryConstructor_declaringFormalParameter_simple_var() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A(var int a) {} '''); @@ -492,7 +479,7 @@ ClassDeclaration } test_primaryConstructor_field_staticConst() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A(final String a, final bool b) { static const int foo = 0; static const int bar = 1; @@ -578,7 +565,7 @@ ClassDeclaration } test_primaryConstructor_fieldFormalParameter() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A(int this.a) { final int a; } @@ -630,7 +617,7 @@ ClassDeclaration } test_primaryConstructor_formalParameters_bodyScope_metadata() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' const foo = 0; class A(@foo int x) { static const foo = 1; @@ -668,14 +655,13 @@ PrimaryConstructorDeclaration } test_primaryConstructor_formalParameters_bodyScope_type() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A(int x) { +// ^^^ +// [diag.notAType] int isn't a type. static const String int = ''; } -''', - [error(diag.notAType, 8, 3)], - ); +'''); var node = findNode.singlePrimaryConstructorDeclaration; assertResolvedNodeText(node, r''' @@ -700,7 +686,7 @@ PrimaryConstructorDeclaration } test_primaryConstructor_hasTypeParameters_named() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A.named(T t) {} '''); @@ -744,7 +730,7 @@ ClassDeclaration } test_primaryConstructor_hasTypeParameters_unnamed() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A(T t) {} '''); @@ -785,7 +771,7 @@ ClassDeclaration } test_primaryConstructor_noTypeParameters_named() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A.named(int a) {} '''); @@ -821,7 +807,7 @@ ClassDeclaration } test_primaryConstructor_noTypeParameters_unnamed() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A(int a) {} '''); @@ -854,7 +840,7 @@ ClassDeclaration } test_primaryConstructor_scopes() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' const foo = 0; class A<@foo T>([@foo int x = foo]) { static const foo = 1; @@ -916,7 +902,7 @@ PrimaryConstructorDeclaration } test_primaryConstructor_superFormalParameter() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A(final int a); class B(super.a) extends A; '''); @@ -953,7 +939,7 @@ ClassDeclaration } test_primaryConstructor_typeParameters() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class D(T t, U u); '''); @@ -1016,19 +1002,18 @@ ClassDeclaration } test_primaryConstructorBody_duplicate() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A(bool x, bool y) { this : assert(x) { y; } this : assert(!x) { +//^^^^ +// [diag.multiplePrimaryConstructorBodyDeclarations] Only one primary constructor body declaration is allowed. !y; } } -''', - [error(diag.multiplePrimaryConstructorBodyDeclarations, 60, 4)], - ); +'''); var node = findNode.singleClassDeclaration; assertResolvedNodeText(node, r''' @@ -1123,7 +1108,7 @@ ClassDeclaration } test_primaryConstructorBody_metadata() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A() { @deprecated this; @@ -1148,15 +1133,14 @@ PrimaryConstructorBody } test_primaryConstructorBody_metadata_noDeclaration() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { @deprecated this; +//^^^^ +// [diag.primaryConstructorBodyWithoutDeclaration] A primary constructor body requires a primary constructor declaration. } -''', - [error(diag.primaryConstructorBodyWithoutDeclaration, 26, 4)], - ); +'''); var node = findNode.singlePrimaryConstructorBody; assertResolvedNodeText(node, r''' @@ -1176,20 +1160,19 @@ PrimaryConstructorBody } test_primaryConstructorBody_noDeclaration() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { this : assert(x) { +//^^^^ +// [diag.primaryConstructorBodyWithoutDeclaration] A primary constructor body requires a primary constructor declaration. +// ^ +// [diag.undefinedIdentifier] Undefined name 'x'. y; +// ^ +// [diag.undefinedIdentifier] Undefined name 'y'. } } -''', - [ - error(diag.primaryConstructorBodyWithoutDeclaration, 12, 4), - error(diag.undefinedIdentifier, 26, 1), - error(diag.undefinedIdentifier, 35, 1), - ], - ); +'''); var node = findNode.singlePrimaryConstructorBody; assertResolvedNodeText(node, r''' @@ -1220,7 +1203,7 @@ PrimaryConstructorBody } test_primaryConstructorBody_primaryInitializerScope_declaringFormalParameter() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A(final bool a) { this : assert(a); } @@ -1246,7 +1229,7 @@ PrimaryConstructorBody } test_primaryConstructorBody_primaryInitializerScope_declaringFormalParameter_shadowedClassName() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A(final int A()) { this : assert(A() > 0); } @@ -1269,7 +1252,7 @@ FunctionExpressionInvocation } test_primaryConstructorBody_primaryInitializerScope_fieldFormalParameter() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A(this.a) { final bool a; this : assert(a); @@ -1296,7 +1279,7 @@ PrimaryConstructorBody } test_primaryConstructorBody_primaryInitializerScope_fieldFormalParameter_shadowedClassName() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int _); } @@ -1324,7 +1307,7 @@ FunctionExpressionInvocation } test_primaryConstructorBody_primaryInitializerScope_simpleFormalParameter() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A(bool a) { this : assert(a); } @@ -1350,7 +1333,7 @@ PrimaryConstructorBody } test_primaryConstructorBody_primaryInitializerScope_superFormalParameter() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A(final bool a); class B(super.a) extends A { this : assert(a); @@ -1377,7 +1360,7 @@ PrimaryConstructorBody } test_primaryConstructorBody_primaryInitializerScope_superFormalParameter_shadowedClassName() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A(int Function() A); class B(super.A) extends A { this : assert(A() > 0); @@ -1401,7 +1384,7 @@ FunctionExpressionInvocation } test_primaryConstructorBody_primaryParameterScope_declaringFormalParameter() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A(final int a) { this { a; @@ -1436,7 +1419,7 @@ PrimaryConstructorBody } test_primaryConstructorBody_primaryParameterScope_fieldFormalParameter() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A(this.a) { final int a; this { @@ -1472,7 +1455,7 @@ PrimaryConstructorBody } test_primaryConstructorBody_primaryParameterScope_simpleFormalParameter() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A(int a) { this { a; @@ -1533,7 +1516,7 @@ PrimaryConstructorBody } test_primaryConstructorBody_primaryParameterScope_superFormalParameter() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A(final int a); class B(super.a) extends A { this { @@ -1569,7 +1552,7 @@ PrimaryConstructorBody } test_primaryInitializerScope_fieldInitializer_instance() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A(int foo) { var bar = foo; } @@ -1595,7 +1578,7 @@ FieldDeclaration } test_primaryInitializerScope_fieldInitializer_instance_declaringFormal() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A(final int foo) { var bar = foo; } @@ -1621,14 +1604,13 @@ FieldDeclaration } test_primaryInitializerScope_fieldInitializer_instance_late() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A(int foo) { late var bar = foo; +// ^^^ +// [diag.undefinedIdentifier] Undefined name 'foo'. } -''', - [error(diag.undefinedIdentifier, 36, 3)], - ); +'''); var node = findNode.singleFieldDeclaration; assertResolvedNodeText(node, r''' @@ -1651,14 +1633,13 @@ FieldDeclaration } test_primaryInitializerScope_fieldInitializer_static() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A(int foo) { static var bar = foo; +// ^^^ +// [diag.undefinedIdentifier] Undefined name 'foo'. } -''', - [error(diag.undefinedIdentifier, 38, 3)], - ); +'''); var node = findNode.singleFieldDeclaration; assertResolvedNodeText(node, r''' @@ -1681,7 +1662,7 @@ FieldDeclaration } test_primaryInitializerScope_fieldTypeAnnotation_shadowedClassName() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A(int A) { A? field; } @@ -1707,35 +1688,33 @@ NamedType class ClassDeclarationResolutionTest_constructor_super extends PubPackageResolutionTest { test_named_N1_superFormals_N1_hasSuper_N1() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A.named({required int n1}); } class B extends A { B.named({required super.n1}) : super.named(n1: 0); +// ^^ +// [diag.duplicateNamedArgument] The argument for the named parameter 'n1' was already specified. } -''', - [error(diag.duplicateNamedArgument, 107, 2)], - ); +'''); } test_named_P_N1_n2_superFormals_P_hasSuper_n2() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A.named(int p1, {required int n1, int? n2}); } class B extends A { B.named(super.p1) : super.named(n2: 1); +// ^^^^^^^^^^^^^^^^^^ +// [diag.missingRequiredArgument] The named parameter 'n1' is required, but there's no corresponding argument. } -''', - [error(diag.missingRequiredArgument, 101, 18)], - ); +'''); } test_named_P_n1_superFormals_P_hasSuper_n1() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A.named(int p1, {int? n1}); } @@ -1746,7 +1725,7 @@ class B extends A { } test_named_P_N1_superFormals_P_N1_hasSuper_none() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A.named(int p1, {required int n1}); } @@ -1757,21 +1736,20 @@ class B extends A { } test_named_PP_superFormals_P_hasSuper_P() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A.named(int p1, int p2); } class B extends A { B.named(super.p1) : super.named(0); +// ^^ +// [diag.positionalSuperFormalParameterWithPositionalArgument] Positional super parameters can't be used when the super constructor invocation has a positional argument. } -''', - [error(diag.positionalSuperFormalParameterWithPositionalArgument, 75, 2)], - ); +'''); } test_unnamed_n1_n2_n3_superFormals_n1_n2_hasSuper_n3() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A({int? n1, int? n2, int? n3}); } @@ -1782,21 +1760,20 @@ class B extends A { } test_unnamed_N1_N2_superFormals_N1_hasConstructor_noSuper() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A({required int n1, required int n2}); } class B extends A { B({required super.n1}); +//^ +// [diag.implicitSuperInitializerMissingArguments] The implicitly invoked unnamed constructor from 'A' has required parameters. } -''', - [error(diag.implicitSuperInitializerMissingArguments, 75, 1)], - ); +'''); } test_unnamed_N1_N2_superFormals_N1_hasSuper_N2() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A({required int n1, required int n2}); } @@ -1807,7 +1784,7 @@ class B extends A { } test_unnamed_n1_n2_superFormals_n1_hasSuper_n2() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A({int? n1, int? n2}); } @@ -1818,21 +1795,20 @@ class B extends A { } test_unnamed_N1_N2_superFormals_N1_hasSuper_none() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A({required int n1, required int n2}); } class B extends A { B({required super.n1}) : super(); +// ^^^^^^^ +// [diag.missingRequiredArgument] The named parameter 'n2' is required, but there's no corresponding argument. } -''', - [error(diag.missingRequiredArgument, 100, 7)], - ); +'''); } test_unnamed_n1_N2_superFormals_n1_N2_hasConstructor_noSuper() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A({int? n1, required int n2}); } @@ -1843,7 +1819,7 @@ class B extends A { } test_unnamed_N1_superFormals_n1_default_hasConstructor_noSuper() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A({required int n1}); } @@ -1854,24 +1830,22 @@ class B extends A { } test_unnamed_N1_superFormals_N1_hasConstructor_N2_noSuper() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A({required int n2}); } class B extends A { B({required super.n1}); +//^ +// [diag.implicitSuperInitializerMissingArguments] The implicitly invoked unnamed constructor from 'A' has required parameters. +// ^^ +// [diag.superFormalParameterWithoutAssociatedNamed] No associated named super constructor parameter. } -''', - [ - error(diag.implicitSuperInitializerMissingArguments, 58, 1), - error(diag.superFormalParameterWithoutAssociatedNamed, 76, 2), - ], - ); +'''); } test_unnamed_N1_superFormals_N1_hasConstructor_noSuper() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A({required int n1}); } @@ -1882,7 +1856,7 @@ class B extends A { } test_unnamed_n1_superFormals_n1_hasConstructor_noSuper() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A({int? n1}); } @@ -1893,35 +1867,33 @@ class B extends A { } test_unnamed_N1_superFormals_N1_hasSuper_N1() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A({required int n1}); } class B extends A { B({required super.n1}) : super(n1: 0); +// ^^ +// [diag.duplicateNamedArgument] The argument for the named parameter 'n1' was already specified. } -''', - [error(diag.duplicateNamedArgument, 89, 2)], - ); +'''); } test_unnamed_n1_superFormals_n1_hasSuper_n1() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A({int? n1}); } class B extends A { B({super.n1}) : super(n1: 1); +// ^^ +// [diag.duplicateNamedArgument] The argument for the named parameter 'n1' was already specified. } -''', - [error(diag.duplicateNamedArgument, 72, 2)], - ); +'''); } test_unnamed_N1_superFormals_none_hasSuper_N1() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A({required int n1}); } @@ -1932,7 +1904,7 @@ class B extends A { } test_unnamed_n1_superFormals_none_hasSuper_n1() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A({int? n1}); } @@ -1943,38 +1915,35 @@ class B extends A { } test_unnamed_N1_superFormals_P_hasConstructor_noSuper() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A({required int n1}); } class B extends A { B(super.n1); +//^ +// [diag.implicitSuperInitializerMissingArguments] The implicitly invoked unnamed constructor from 'A' has required parameters. +// ^^ +// [diag.superFormalParameterWithoutAssociatedPositional] No associated positional super constructor parameter. } -''', - [ - error(diag.implicitSuperInitializerMissingArguments, 58, 1), - error(diag.superFormalParameterWithoutAssociatedPositional, 66, 2), - ], - ); +'''); } test_unnamed_n1_superFormals_P_hasConstructor_noSuper() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A({int? n1}); } class B extends A { B(super.p1); +// ^^ +// [diag.superFormalParameterWithoutAssociatedPositional] No associated positional super constructor parameter. } -''', - [error(diag.superFormalParameterWithoutAssociatedPositional, 58, 2)], - ); +'''); } test_unnamed_P_n1_n2_superFormals_P_hasSuper_n1_n2() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int p1, {int? n1, int? n2}); } @@ -1985,21 +1954,20 @@ class B extends A { } test_unnamed_P_N1_n2_superFormals_P_hasSuper_n2() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int p1, {required int n1, int? n2}); } class B extends A { B(super.p1) : super(n2: 1); +// ^^^^^^^^^^^^ +// [diag.missingRequiredArgument] The named parameter 'n1' is required, but there's no corresponding argument. } -''', - [error(diag.missingRequiredArgument, 89, 12)], - ); +'''); } test_unnamed_P_n1_n2_superFormals_P_n1_hasSuper_n2() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int p1, {int? n1, int? n2}); } @@ -2010,7 +1978,7 @@ class B extends A { } test_unnamed_P_n1_N2_superFormals_P_n1_N2_hasConstructor_noSuper() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int p1, {int? n1, required int n2}); } @@ -2021,7 +1989,7 @@ class B extends A { } test_unnamed_P_N1_n2_superFormals_P_n2_hasSuper_N1() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int p1, {required int n1, int? n2}); } @@ -2032,7 +2000,7 @@ class B extends A { } test_unnamed_P_N1_superFormals_N1_hasSuper_P() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int p1, {required int n1}); } @@ -2043,7 +2011,7 @@ class B extends A { } test_unnamed_P_n1_superFormals_n1_hasSuper_P() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int p1, {int? n1}); } @@ -2054,21 +2022,20 @@ class B extends A { } test_unnamed_P_N1_superFormals_P_hasConstructor_noSuper() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int p1, {required int n1}); } class B extends A { B(super.p1); +//^ +// [diag.implicitSuperInitializerMissingArguments] The implicitly invoked unnamed constructor from 'A' has required parameters. } -''', - [error(diag.implicitSuperInitializerMissingArguments, 66, 1)], - ); +'''); } test_unnamed_P_n1_superFormals_P_hasSuper_n1() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int p1, {int? n1}); } @@ -2079,7 +2046,7 @@ class B extends A { } test_unnamed_P_N1_superFormals_P_N1_hasConstructor_noSuper() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int p1, {required int n1}); } @@ -2090,7 +2057,7 @@ class B extends A { } test_unnamed_P_n1_superFormals_P_n1_hasConstructor_noSuper() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int p1, {int? n1}); } @@ -2101,66 +2068,61 @@ class B extends A { } test_unnamed_P_superFormals_N1_hasConstructor_noSuper() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int p1); } class B extends A { B({required super.p1}); +//^ +// [diag.implicitSuperInitializerMissingArguments] The implicitly invoked unnamed constructor from 'A' has required parameters. +// ^^ +// [diag.superFormalParameterWithoutAssociatedNamed] No associated named super constructor parameter. } -''', - [ - error(diag.implicitSuperInitializerMissingArguments, 47, 1), - error(diag.superFormalParameterWithoutAssociatedNamed, 65, 2), - ], - ); +'''); } test_unnamed_p_superFormals_n1_hasConstructor_noSuper() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A([int? p1]); } class B extends A { B({super.n1}); +// ^^ +// [diag.superFormalParameterWithoutAssociatedNamed] No associated named super constructor parameter. } -''', - [error(diag.superFormalParameterWithoutAssociatedNamed, 59, 2)], - ); +'''); } test_unnamed_P_superFormals_none_hasConstructor_noSuper() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int p1); } class B extends A { B(); +//^ +// [diag.implicitSuperInitializerMissingArguments] The implicitly invoked unnamed constructor from 'A' has required parameters. } -''', - [error(diag.implicitSuperInitializerMissingArguments, 47, 1)], - ); +'''); } test_unnamed_P_superFormals_none_hasSuper_none() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int p1); } class B extends A { B() : super(); +// ^ +// [diag.notEnoughPositionalArgumentsNameSingular] 1 positional argument expected by 'A.new', but 0 found. } -''', - [error(diag.notEnoughPositionalArgumentsNameSingular, 59, 1)], - ); +'''); } test_unnamed_P_superFormals_none_hasSuper_p() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int p1); } @@ -2171,7 +2133,7 @@ class B extends A { } test_unnamed_p_superFormals_none_hasSuper_p() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A([int? p1]); } @@ -2182,19 +2144,18 @@ class B extends A { } test_unnamed_P_superFormals_none_noConstructor() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int p1); } class B extends A {} -''', - [error(diag.noDefaultSuperConstructorImplicit, 31, 1)], - ); +// ^ +// [diag.noDefaultSuperConstructorImplicit] The superclass 'A' doesn't have a zero argument constructor. +'''); } test_unnamed_P_superFormals_p_default_hasConstructor_noSuper() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int p1); } @@ -2205,7 +2166,7 @@ class B extends A { } test_unnamed_p_superFormals_p_hasConstructor_noSuper() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A([int? p1]); } @@ -2216,21 +2177,20 @@ class B extends A { } test_unnamed_P_superFormals_P_hasConstructor_noSuper_wrongType() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int p1); } class B extends A { B(String super.p1); +// ^^ +// [diag.superFormalParameterTypeIsNotSubtypeOfAssociated] The type 'String' of this parameter isn't a subtype of the type 'int' of the associated super constructor parameter. } -''', - [error(diag.superFormalParameterTypeIsNotSubtypeOfAssociated, 62, 2)], - ); +'''); } test_unnamed_P_superFormals_P_hasSuper_none() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int p1); } @@ -2241,91 +2201,85 @@ class B extends A { } test_unnamed_P_superFormals_p_hasSuper_none() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int p1); } class B extends A { B([super.p1]) : super(); +// ^^ +// [diag.missingDefaultValueForParameterPositional] The parameter 'p1' can't have a value of 'null' because of its type, but the implicit default value is 'null'. } -''', - [error(diag.missingDefaultValueForParameterPositional, 56, 2)], - ); +'''); } test_unnamed_p_superFormals_p_hasSuper_p() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A([int? p1]); } class B extends A { B(super.p1) : super(1); +// ^^ +// [diag.positionalSuperFormalParameterWithPositionalArgument] Positional super parameters can't be used when the super constructor invocation has a positional argument. } -''', - [error(diag.positionalSuperFormalParameterWithPositionalArgument, 58, 2)], - ); +'''); } test_unnamed_P_superFormals_PP_hasConstructor_noSuper() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int p1); } class B extends A { B(super.p1, super.p2); +// ^^ +// [diag.superFormalParameterWithoutAssociatedPositional] No associated positional super constructor parameter. } -''', - [error(diag.superFormalParameterWithoutAssociatedPositional, 65, 2)], - ); +'''); } test_unnamed_PP_superFormals_P_hasConstructor_noSuper() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int p1, int p2); } class B extends A { B(super.p1); +//^ +// [diag.implicitSuperInitializerMissingArguments] The implicitly invoked unnamed constructor from 'A' has required parameters. } -''', - [error(diag.implicitSuperInitializerMissingArguments, 55, 1)], - ); +'''); } test_unnamed_PP_superFormals_P_hasSuper_none() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int p1, int p2); } class B extends A { B(super.p1) : super(); +// ^ +// [diag.notEnoughPositionalArgumentsNamePlural] 2 positional arguments expected by 'A.new', but 1 found. } -''', - [error(diag.notEnoughPositionalArgumentsNamePlural, 75, 1)], - ); +'''); } test_unnamed_PP_superFormals_P_hasSuper_P() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int p1, int p2); } class B extends A { B(super.p1) : super(0); +// ^^ +// [diag.positionalSuperFormalParameterWithPositionalArgument] Positional super parameters can't be used when the super constructor invocation has a positional argument. } -''', - [error(diag.positionalSuperFormalParameterWithPositionalArgument, 63, 2)], - ); +'''); } test_unnamed_Pp_superFormals_Pp_hasConstructor_noSuper() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int p1, [int? p2]); } @@ -2345,35 +2299,33 @@ class B extends A { class ClassDeclarationResolutionTest_primaryConstructor_super extends PubPackageResolutionTest { test_named_N1_superFormals_N1_hasSuper_N1() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A.named({required int n1}); } class B.named({required super.n1}) extends A { this : super.named(n1: 0); +// ^^ +// [diag.duplicateNamedArgument] The argument for the named parameter 'n1' was already specified. } -''', - [error(diag.duplicateNamedArgument, 110, 2)], - ); +'''); } test_named_P_N1_n2_superFormals_P_hasSuper_n2() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A.named(int p1, {required int n1, int? n2}); } class B.named(super.p1) extends A { this : super.named(n2: 1); +// ^^^^^^^^^^^^^^^^^^ +// [diag.missingRequiredArgument] The named parameter 'n1' is required, but there's no corresponding argument. } -''', - [error(diag.missingRequiredArgument, 104, 18)], - ); +'''); } test_named_P_n1_superFormals_P_hasSuper_n1() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A.named(int p1, {int? n1}); } @@ -2384,7 +2336,7 @@ class B.named(super.p1) extends A { } test_named_P_N1_superFormals_P_N1_hasSuper_none() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A.named(int p1, {required int n1}); } @@ -2395,21 +2347,20 @@ class B.named(super.p1, {required super.n1}) extends A { } test_named_PP_superFormals_P_hasSuper_P() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A.named(int p1, int p2); } class B.named(super.p1) extends A { +// ^^ +// [diag.positionalSuperFormalParameterWithPositionalArgument] Positional super parameters can't be used when the super constructor invocation has a positional argument. this : super.named(0); } -''', - [error(diag.positionalSuperFormalParameterWithPositionalArgument, 59, 2)], - ); +'''); } test_unnamed_n1_n2_n3_superFormals_n1_n2_hasSuper_n3() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A({int? n1, int? n2, int? n3}); } @@ -2420,21 +2371,20 @@ class B({super.n1, super.n2}) extends A { } test_unnamed_N1_N2_superFormals_N1_hasBody_noSuper() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A({required int n1, required int n2}); } class B({required super.n1}) extends A { this; +//^^^^ +// [diag.implicitSuperInitializerMissingArguments] The implicitly invoked unnamed constructor from 'A' has required parameters. } -''', - [error(diag.implicitSuperInitializerMissingArguments, 96, 4)], - ); +'''); } test_unnamed_N1_N2_superFormals_N1_hasSuper_N2() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A({required int n1, required int n2}); } @@ -2445,7 +2395,7 @@ class B({required super.n1}) extends A { } test_unnamed_n1_n2_superFormals_n1_hasSuper_n2() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A({int? n1, int? n2}); } @@ -2456,21 +2406,20 @@ class B({super.n1}) extends A { } test_unnamed_N1_N2_superFormals_N1_hasSuper_none() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A({required int n1, required int n2}); } class B({required super.n1}) extends A { this : super(); +// ^^^^^^^ +// [diag.missingRequiredArgument] The named parameter 'n2' is required, but there's no corresponding argument. } -''', - [error(diag.missingRequiredArgument, 103, 7)], - ); +'''); } test_unnamed_n1_N2_superFormals_n1_N2_noBody() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A({int? n1, required int n2}); } @@ -2479,19 +2428,18 @@ class B({super.n1, required super.n2}) extends A; } test_unnamed_N1_N2_superFormals_N1_noBody() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A({required int n1, required int n2}); } class B({required super.n1}) extends A; -''', - [error(diag.implicitSuperInitializerMissingArguments, 59, 1)], - ); +// ^ +// [diag.implicitSuperInitializerMissingArguments] The implicitly invoked unnamed constructor from 'A' has required parameters. +'''); } test_unnamed_N1_superFormals_n1_default_noBody() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A({required int n1}); } @@ -2500,7 +2448,7 @@ class B({super.n1 = 1}) extends A; } test_unnamed_N1_superFormals_N1_hasBody_noSuper() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A({required int n1}); } @@ -2511,35 +2459,33 @@ class B({required super.n1}) extends A { } test_unnamed_N1_superFormals_N1_hasSuper_N1() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A({required int n1}); } class B({required super.n1}) extends A { this : super(n1: 0); +// ^^ +// [diag.duplicateNamedArgument] The argument for the named parameter 'n1' was already specified. } -''', - [error(diag.duplicateNamedArgument, 92, 2)], - ); +'''); } test_unnamed_n1_superFormals_n1_hasSuper_n1() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A({int? n1}); } class B({super.n1}) extends A { this : super(n1: 1); +// ^^ +// [diag.duplicateNamedArgument] The argument for the named parameter 'n1' was already specified. } -''', - [error(diag.duplicateNamedArgument, 75, 2)], - ); +'''); } test_unnamed_N1_superFormals_N1_noBody() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A({required int n1}); } @@ -2548,7 +2494,7 @@ class B({required super.n1}) extends A; } test_unnamed_n1_superFormals_n1_noBody() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A({int? n1}); } @@ -2557,22 +2503,20 @@ class B({super.n1}) extends A; } test_unnamed_N1_superFormals_N1_noBody_N2_noSuper() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A({required int n2}); } class B({required super.n1}) extends A; -''', - [ - error(diag.implicitSuperInitializerMissingArguments, 42, 1), - error(diag.superFormalParameterWithoutAssociatedNamed, 60, 2), - ], - ); +// ^ +// [diag.implicitSuperInitializerMissingArguments] The implicitly invoked unnamed constructor from 'A' has required parameters. +// ^^ +// [diag.superFormalParameterWithoutAssociatedNamed] No associated named super constructor parameter. +'''); } test_unnamed_N1_superFormals_none_hasSuper_N1() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A({required int n1}); } @@ -2583,7 +2527,7 @@ class B() extends A { } test_unnamed_n1_superFormals_none_hasSuper_n1() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A({int? n1}); } @@ -2594,51 +2538,46 @@ class B() extends A { } test_unnamed_N1_superFormals_P_hasBody_noSuper() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A({required int n1}); } class B(super.n1) extends A { +// ^^ +// [diag.superFormalParameterWithoutAssociatedPositional] No associated positional super constructor parameter. this; +//^^^^ +// [diag.implicitSuperInitializerMissingArguments] The implicitly invoked unnamed constructor from 'A' has required parameters. } -''', - [ - error(diag.superFormalParameterWithoutAssociatedPositional, 50, 2), - error(diag.implicitSuperInitializerMissingArguments, 68, 4), - ], - ); +'''); } test_unnamed_N1_superFormals_P_noBody() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A({required int n1}); } class B(super.n1) extends A; -''', - [ - error(diag.implicitSuperInitializerMissingArguments, 42, 1), - error(diag.superFormalParameterWithoutAssociatedPositional, 50, 2), - ], - ); +// ^ +// [diag.implicitSuperInitializerMissingArguments] The implicitly invoked unnamed constructor from 'A' has required parameters. +// ^^ +// [diag.superFormalParameterWithoutAssociatedPositional] No associated positional super constructor parameter. +'''); } test_unnamed_n1_superFormals_P_noBody() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A({int? n1}); } class B(super.p1) extends A; -''', - [error(diag.superFormalParameterWithoutAssociatedPositional, 42, 2)], - ); +// ^^ +// [diag.superFormalParameterWithoutAssociatedPositional] No associated positional super constructor parameter. +'''); } test_unnamed_P_n1_n2_superFormals_P_hasSuper_n1_n2() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int p1, {int? n1, int? n2}); } @@ -2649,21 +2588,20 @@ class B(super.p1) extends A { } test_unnamed_P_N1_n2_superFormals_P_hasSuper_n2() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int p1, {required int n1, int? n2}); } class B(super.p1) extends A { this : super(n2: 1); +// ^^^^^^^^^^^^ +// [diag.missingRequiredArgument] The named parameter 'n1' is required, but there's no corresponding argument. } -''', - [error(diag.missingRequiredArgument, 92, 12)], - ); +'''); } test_unnamed_P_n1_n2_superFormals_P_n1_hasSuper_n2() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int p1, {int? n1, int? n2}); } @@ -2674,7 +2612,7 @@ class B(super.p1, {super.n1}) extends A { } test_unnamed_P_n1_N2_superFormals_P_n1_N2_noBody() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int p1, {int? n1, required int n2}); } @@ -2683,7 +2621,7 @@ class B(super.p1, {super.n1, required super.n2}) extends A; } test_unnamed_P_N1_n2_superFormals_P_n2_hasSuper_N1() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int p1, {required int n1, int? n2}); } @@ -2694,7 +2632,7 @@ class B(super.p1, {super.n2}) extends A { } test_unnamed_P_N1_superFormals_N1_hasSuper_P() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int p1, {required int n1}); } @@ -2705,7 +2643,7 @@ class B({required super.n1}) extends A { } test_unnamed_P_n1_superFormals_n1_hasSuper_P() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int p1, {int? n1}); } @@ -2716,21 +2654,20 @@ class B({super.n1}) extends A { } test_unnamed_P_N1_superFormals_P_hasBody_noSuper() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int p1, {required int n1}); } class B(super.p1) extends A { this; +//^^^^ +// [diag.implicitSuperInitializerMissingArguments] The implicitly invoked unnamed constructor from 'A' has required parameters. } -''', - [error(diag.implicitSuperInitializerMissingArguments, 76, 4)], - ); +'''); } test_unnamed_P_n1_superFormals_P_hasSuper_n1() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int p1, {int? n1}); } @@ -2741,7 +2678,7 @@ class B(super.p1) extends A { } test_unnamed_P_N1_superFormals_P_N1_noBody() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int p1, {required int n1}); } @@ -2750,7 +2687,7 @@ class B(super.p1, {required super.n1}) extends A; } test_unnamed_P_n1_superFormals_P_n1_noBody() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int p1, {int? n1}); } @@ -2759,60 +2696,55 @@ class B(super.p1, {super.n1}) extends A; } test_unnamed_P_N1_superFormals_P_noBody() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int p1, {required int n1}); } class B(super.p1) extends A; -''', - [error(diag.implicitSuperInitializerMissingArguments, 50, 1)], - ); +// ^ +// [diag.implicitSuperInitializerMissingArguments] The implicitly invoked unnamed constructor from 'A' has required parameters. +'''); } test_unnamed_P_superFormals_N1_noBody() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int p1); } class B({required super.p1}) extends A; -''', - [ - error(diag.implicitSuperInitializerMissingArguments, 31, 1), - error(diag.superFormalParameterWithoutAssociatedNamed, 49, 2), - ], - ); +// ^ +// [diag.implicitSuperInitializerMissingArguments] The implicitly invoked unnamed constructor from 'A' has required parameters. +// ^^ +// [diag.superFormalParameterWithoutAssociatedNamed] No associated named super constructor parameter. +'''); } test_unnamed_p_superFormals_n1_noBody() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A([int? p1]); } class B({super.n1}) extends A; -''', - [error(diag.superFormalParameterWithoutAssociatedNamed, 43, 2)], - ); +// ^^ +// [diag.superFormalParameterWithoutAssociatedNamed] No associated named super constructor parameter. +'''); } test_unnamed_P_superFormals_none_hasSuper_none() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int p1); } class B() extends A { this : super(); +// ^ +// [diag.notEnoughPositionalArgumentsNameSingular] 1 positional argument expected by 'A.new', but 0 found. } -''', - [error(diag.notEnoughPositionalArgumentsNameSingular, 62, 1)], - ); +'''); } test_unnamed_P_superFormals_none_hasSuper_p() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int p1); } @@ -2823,7 +2755,7 @@ class B() extends A { } test_unnamed_p_superFormals_none_hasSuper_p() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A([int? p1]); } @@ -2834,31 +2766,29 @@ class B() extends A { } test_unnamed_P_superFormals_none_noBody() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int p1); } class B() extends A; -''', - [error(diag.implicitSuperInitializerMissingArguments, 31, 1)], - ); +// ^ +// [diag.implicitSuperInitializerMissingArguments] The implicitly invoked unnamed constructor from 'A' has required parameters. +'''); } test_unnamed_P_superFormals_none_noConstructor() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int p1); } class B() extends A; -''', - [error(diag.implicitSuperInitializerMissingArguments, 31, 1)], - ); +// ^ +// [diag.implicitSuperInitializerMissingArguments] The implicitly invoked unnamed constructor from 'A' has required parameters. +'''); } test_unnamed_P_superFormals_p_default_noBody() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int p1); } @@ -2867,7 +2797,7 @@ class B([super.p1 = 1]) extends A; } test_unnamed_P_superFormals_P_hasSuper_none() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int p1); } @@ -2878,35 +2808,33 @@ class B(super.p1) extends A { } test_unnamed_P_superFormals_p_hasSuper_none() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int p1); } class B([super.p1]) extends A { +// ^^ +// [diag.missingDefaultValueForParameterPositional] The parameter 'p1' can't have a value of 'null' because of its type, but the implicit default value is 'null'. this : super(); } -''', - [error(diag.missingDefaultValueForParameterPositional, 40, 2)], - ); +'''); } test_unnamed_p_superFormals_p_hasSuper_p() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A([int? p1]); } class B(super.p1) extends A { +// ^^ +// [diag.positionalSuperFormalParameterWithPositionalArgument] Positional super parameters can't be used when the super constructor invocation has a positional argument. this : super(1); } -''', - [error(diag.positionalSuperFormalParameterWithPositionalArgument, 42, 2)], - ); +'''); } test_unnamed_p_superFormals_p_noBody() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A([int? p1]); } @@ -2915,71 +2843,66 @@ class B([super.p1]) extends A; } test_unnamed_P_superFormals_P_noBody_wrongType() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int p1); } class B(String super.p1) extends A; -''', - [error(diag.superFormalParameterTypeIsNotSubtypeOfAssociated, 46, 2)], - ); +// ^^ +// [diag.superFormalParameterTypeIsNotSubtypeOfAssociated] The type 'String' of this parameter isn't a subtype of the type 'int' of the associated super constructor parameter. +'''); } test_unnamed_P_superFormals_PP_noBody() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int p1); } class B(super.p1, super.p2) extends A; -''', - [error(diag.superFormalParameterWithoutAssociatedPositional, 49, 2)], - ); +// ^^ +// [diag.superFormalParameterWithoutAssociatedPositional] No associated positional super constructor parameter. +'''); } test_unnamed_PP_superFormals_P_hasSuper_none() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int p1, int p2); } class B(super.p1) extends A { this : super(); +// ^ +// [diag.notEnoughPositionalArgumentsNamePlural] 2 positional arguments expected by 'A.new', but 1 found. } -''', - [error(diag.notEnoughPositionalArgumentsNamePlural, 78, 1)], - ); +'''); } test_unnamed_PP_superFormals_P_hasSuper_P() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int p1, int p2); } class B(super.p1) extends A { +// ^^ +// [diag.positionalSuperFormalParameterWithPositionalArgument] Positional super parameters can't be used when the super constructor invocation has a positional argument. this : super(0); } -''', - [error(diag.positionalSuperFormalParameterWithPositionalArgument, 47, 2)], - ); +'''); } test_unnamed_PP_superFormals_P_noBody() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int p1, int p2); } class B(super.p1) extends A; -''', - [error(diag.implicitSuperInitializerMissingArguments, 39, 1)], - ); +// ^ +// [diag.implicitSuperInitializerMissingArguments] The implicitly invoked unnamed constructor from 'A' has required parameters. +'''); } test_unnamed_Pp_superFormals_Pp_noBody() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int p1, [int? p2]); } diff --git a/pkg/analyzer/test/src/dart/resolution/comment_test.dart b/pkg/analyzer/test/src/dart/resolution/comment_test.dart index 86f7888142c..61f10de03c4 100644 --- a/pkg/analyzer/test/src/dart/resolution/comment_test.dart +++ b/pkg/analyzer/test/src/dart/resolution/comment_test.dart @@ -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 'package:analyzer/src/diagnostic/diagnostic.dart' as diag; import 'package:test_reflective_loader/test_reflective_loader.dart'; import 'context_collection_resolution.dart'; @@ -24,7 +23,7 @@ class CommentResolutionTest_PrefixedIdentifier // TODO(srawlins): improve coverage regarding constructors, operators, the // 'new' keyword, and members on an extension on a type variable // (`extension on T`). - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' class A { A.named(); } @@ -51,7 +50,7 @@ CommentReference } test_class_constructor_unnamedViaNew() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' class A { A(); } @@ -78,7 +77,7 @@ CommentReference } test_class_instanceGetter() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' class A { int get foo => 0; } @@ -105,7 +104,7 @@ CommentReference } test_class_instanceGetter_onTypedef() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' class A { int get foo => 0; } @@ -133,7 +132,7 @@ CommentReference } test_class_instanceMethod() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' class A { void foo() {} } @@ -160,7 +159,7 @@ CommentReference } test_class_instanceSetter() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' class A { set foo(int _) {} } @@ -187,7 +186,7 @@ CommentReference } test_class_invalid_ambiguousExtension() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' /// [foo] class A {} @@ -210,7 +209,7 @@ CommentReference } test_class_invalid_unresolved() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' /// [foo] class A {} '''); @@ -225,7 +224,7 @@ CommentReference } test_class_staticGetter() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' class A { static int get foo => 0; } @@ -252,7 +251,7 @@ CommentReference } test_class_staticGetter_onTypedef() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' class A { static int get foo => 0; } @@ -281,7 +280,7 @@ CommentReference } test_class_staticMethod() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' class A { static void foo() {} } @@ -308,7 +307,7 @@ CommentReference } test_class_staticSetter() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' class A { static set foo(int _) {} } @@ -340,7 +339,7 @@ class A { A.named(); } '''); - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' /// @docImport 'foo.dart'; library; @@ -371,7 +370,7 @@ class A { A(); } '''); - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' /// @docImport 'foo.dart'; library; @@ -402,7 +401,7 @@ class A { int get foo => 0; } '''); - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' /// @docImport 'foo.dart'; library; @@ -433,7 +432,7 @@ class A { void foo() {} } '''); - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' /// @docImport 'foo.dart'; library; @@ -464,7 +463,7 @@ class A { static int get foo => 0; } '''); - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' /// @docImport 'foo.dart'; library; @@ -495,7 +494,7 @@ class A { static void foo() {} } '''); - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' /// @docImport 'foo.dart'; library; @@ -526,7 +525,7 @@ class A { static set foo(int _) {} } '''); - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' /// @docImport 'foo.dart'; library; @@ -557,7 +556,7 @@ extension E on int { int get foo => 0; } '''); - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' /// @docImport 'foo.dart'; library; @@ -588,7 +587,7 @@ extension E on int { void foo() {} } '''); - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' /// @docImport 'foo.dart'; library; @@ -619,7 +618,7 @@ extension E on int { set foo(int _) {} } '''); - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' /// @docImport 'foo.dart'; library; @@ -650,7 +649,7 @@ extension E on int { static int get foo => 0; } '''); - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' /// @docImport 'foo.dart'; library; @@ -681,7 +680,7 @@ extension E on int { static void foo() {} } '''); - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' /// @docImport 'foo.dart'; library; @@ -712,7 +711,7 @@ extension E on int { static set foo(int _) {} } '''); - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' /// @docImport 'foo.dart'; library; @@ -738,7 +737,7 @@ CommentReference } test_extension_instanceGetter() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' extension E on int { int get foo => 0; } @@ -765,7 +764,7 @@ CommentReference } test_extension_instanceMethod() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' extension E on int { void foo() {} } @@ -792,7 +791,7 @@ CommentReference } test_extension_instanceSetter() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' extension E on int { set foo(int _) {} } @@ -819,7 +818,7 @@ CommentReference } test_extension_staticGetter() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' extension E on int { static int get foo => 0; } @@ -846,7 +845,7 @@ CommentReference } test_extension_staticMethod() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' extension E on int { static void foo() {} } @@ -873,7 +872,7 @@ CommentReference } test_extension_staticSetter() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' extension E on int { static set foo(int _) {} } @@ -903,7 +902,7 @@ CommentReference @reflectiveTest class CommentResolutionTest_PropertyAccess extends PubPackageResolutionTest { test_class_constructor_named() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' import '' as self; class A { A.named(); @@ -939,7 +938,7 @@ CommentReference } test_class_constructor_unnamedViaNew() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' import '' as self; class A { A(); @@ -974,7 +973,7 @@ CommentReference } test_class_instanceGetter() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' import '' as self; class A { int get foo => 0; @@ -1009,7 +1008,7 @@ CommentReference } test_class_instanceGetter_onTypedef() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' import '' as self; class A { int get foo => 0; @@ -1045,7 +1044,7 @@ CommentReference } test_class_instanceMethod() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' import '' as self; class A { void foo() {} @@ -1080,7 +1079,7 @@ CommentReference } test_class_instanceSetter() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' import '' as self; class A { set foo(int value) {} @@ -1115,7 +1114,7 @@ CommentReference } test_class_staticGetter() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' import '' as self; class A { static int get foo => 0; @@ -1150,7 +1149,7 @@ CommentReference } test_class_staticGetter_onTypedef() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' import '' as self; class A { static int get foo => 0; @@ -1186,7 +1185,7 @@ CommentReference } test_class_staticMethod() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' import '' as self; class A { static void foo() {} @@ -1221,7 +1220,7 @@ CommentReference } test_class_staticSetter() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' import '' as self; class A { static set foo(int value) {} @@ -1256,7 +1255,7 @@ CommentReference } test_extension_instanceGetter() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' import '' as self; extension E on int { int get foo => 0; @@ -1291,7 +1290,7 @@ CommentReference } test_extension_instanceMethod() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' import '' as self; extension E on int { void foo() {} @@ -1326,7 +1325,7 @@ CommentReference } test_extension_instanceSetter() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' import '' as self; extension E on int { set foo(int value) {} @@ -1361,7 +1360,7 @@ CommentReference } test_extension_staticGetter() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' import '' as self; extension E on int { static int get foo => 0; @@ -1396,7 +1395,7 @@ CommentReference } test_extension_staticMethod() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' import '' as self; extension E on int { static void foo() {} @@ -1431,7 +1430,7 @@ CommentReference } test_extension_staticSetter() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' import '' as self; extension E on int { static set foo(int value) {} @@ -1469,7 +1468,7 @@ CommentReference @reflectiveTest class CommentResolutionTest_SimpleIdentifier extends PubPackageResolutionTest { test_associatedSetterAndGetter() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' int get foo => 0; set foo(int value) {} @@ -1488,7 +1487,7 @@ CommentReference } test_associatedSetterAndGetter_setterInScope() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' extension E1 on int { int get foo => 0; } @@ -1509,7 +1508,7 @@ CommentReference } test_beforeClass() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' /// [foo] class A { foo() {} @@ -1526,7 +1525,7 @@ CommentReference } test_beforeConstructor_fieldParameter() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { final int p; @@ -1545,7 +1544,7 @@ CommentReference } test_beforeConstructor_normalParameter() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { /// [p] A(int p); @@ -1561,7 +1560,7 @@ CommentReference } test_beforeConstructor_superParameter() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int p); } @@ -1582,7 +1581,7 @@ CommentReference } test_beforeEnum() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' /// This is the [Samurai] kind. enum Samurai { /// Use [int]. @@ -1617,7 +1616,7 @@ CommentReference } test_beforeFunction_blockBody() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' /// [p] foo(int p) {} '''); @@ -1632,7 +1631,7 @@ SimpleIdentifier } test_beforeFunction_expressionBody() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' /// [p] foo(int p) => null; '''); @@ -1647,7 +1646,7 @@ CommentReference } test_beforeFunctionTypeAlias() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' /// [p] typedef Foo(int p); '''); @@ -1662,7 +1661,7 @@ CommentReference } test_beforeGenericTypeAlias() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' /// Can resolve [T], [S], and [p]. typedef Foo = Function(int p); '''); @@ -1693,7 +1692,7 @@ CommentReference } test_beforeGetter() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' /// [int] get g => null; '''); @@ -1708,7 +1707,7 @@ SimpleIdentifier } test_beforeMethod() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' abstract class A { /// [p1] ma(int p1); @@ -1779,7 +1778,7 @@ int get foo => 0; set foo(int value) {} '''); - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' /// @docImport 'foo.dart'; library; @@ -1802,7 +1801,7 @@ extension E1 on int { int get foo => 0; } '''); - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' /// @docImport 'foo.dart'; library; @@ -1828,7 +1827,7 @@ class C {} newFile('$testPackageLibPath/two.dart', r''' export 'one.dart'; '''); - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' /// @docImport 'two.dart'; library; @@ -1852,19 +1851,17 @@ class A { A.named(); } '''); - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' /// @docImport 'foo.dart'; library; /// [new A] or [new A.named] +// ^^^ +// [diag.deprecatedNewInCommentReference] Using the 'new' keyword in a comment reference is deprecated. +// ^^^ +// [diag.deprecatedNewInCommentReference] Using the 'new' keyword in a comment reference is deprecated. main() {} -''', - [ - error(diag.deprecatedNewInCommentReference, 42, 3), - error(diag.deprecatedNewInCommentReference, 53, 3), - ], - ); +'''); assertResolvedNodeText(findNode.commentReference('A]'), r''' CommentReference @@ -1897,7 +1894,7 @@ CommentReference newFile('$testPackageLibPath/foo.dart', r''' void foo() {} '''); - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' /// @docImport 'foo.dart'; library; @@ -1921,7 +1918,7 @@ CommentReference newFile('$testPackageLibPath/foo.dart', r''' void foo() {} '''); - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' /// @docImport 'foo.dart'; library; @@ -1942,7 +1939,7 @@ CommentReference newFile('$testPackageLibPath/foo.dart', r''' class A {} '''); - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' /// @docImport 'foo.dart'; library; class C { @@ -1964,7 +1961,7 @@ CommentReference newFile('$testPackageLibPath/foo.dart', r''' class A {} '''); - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' /// @docImport 'foo.dart'; /// /// Text [A]. @@ -1984,7 +1981,7 @@ CommentReference newFile('$testPackageLibPath/foo.dart', r''' void foo() {} '''); - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' /// @docImport 'foo.dart'; library; @@ -2005,7 +2002,7 @@ CommentReference newFile('$testPackageLibPath/foo.dart', r''' class A {} '''); - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' /// @docImport 'foo.dart'; library; /// Text [A]. @@ -2025,7 +2022,7 @@ CommentReference newFile('$testPackageLibPath/foo.dart', r''' void foo() {} '''); - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' /// @docImport 'foo.dart'; library; @@ -2043,21 +2040,19 @@ CommentReference } test_newKeyword() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A { A(); A.named(); } /// [new A] or [new A.named] +// ^^^ +// [diag.deprecatedNewInCommentReference] Using the 'new' keyword in a comment reference is deprecated. +// ^^^ +// [diag.deprecatedNewInCommentReference] Using the 'new' keyword in a comment reference is deprecated. main() {} -''', - [ - error(diag.deprecatedNewInCommentReference, 38, 3), - error(diag.deprecatedNewInCommentReference, 49, 3), - ], - ); +'''); assertResolvedNodeText(findNode.commentReference('A]'), r''' CommentReference @@ -2088,7 +2083,7 @@ CommentReference test_onFieldFormalParameter() async { // TODO(scheglov): add tests for references to nested formal parameters - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { final int f; A({ @@ -2109,7 +2104,7 @@ CommentReference } test_onFunctionTypedFormalParameter() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' void f( /// [int] void g(int a), @@ -2128,7 +2123,7 @@ CommentReference test_onFunctionTypedFormalParameter_self() async { // TODO(scheglov): add tests for references to nested formal parameters - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' /// [bar] void f(int bar()) {} '''); @@ -2143,7 +2138,7 @@ CommentReference } test_onSimpleFormalParameter() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' void f( /// [int] int x, @@ -2162,7 +2157,7 @@ CommentReference test_onSuperFormalParameter() async { // TODO(scheglov): add tests for references to nested formal parameters - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { A({required int f}); } @@ -2186,7 +2181,7 @@ CommentReference } test_setter() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { /// [x] in A mA() {} @@ -2217,7 +2212,7 @@ CommentReference } test_unqualifiedReferenceToNonLocalStaticMember() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' class A { static void foo() {} } diff --git a/pkg/analyzer/test/src/dart/resolution/conditional_expression_test.dart b/pkg/analyzer/test/src/dart/resolution/conditional_expression_test.dart index e3d43a89383..a6590ccd575 100644 --- a/pkg/analyzer/test/src/dart/resolution/conditional_expression_test.dart +++ b/pkg/analyzer/test/src/dart/resolution/conditional_expression_test.dart @@ -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 'package:analyzer/src/diagnostic/diagnostic.dart' as diag; import 'package:test_reflective_loader/test_reflective_loader.dart'; import 'context_collection_resolution.dart'; @@ -19,16 +18,15 @@ main() { @reflectiveTest class ConditionalExpressionResolutionTest extends PubPackageResolutionTest { test_condition_super() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A { void f() { super ? 0 : 1; +// ^^^^^ +// [diag.nonBoolCondition] Conditions must have a static type of 'bool'. } } -''', - [error(diag.nonBoolCondition, 27, 5)], - ); +'''); var node = findNode.singleConditionalExpression; assertResolvedNodeText(node, r''' @@ -75,16 +73,15 @@ MethodInvocation } test_else_super() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A { void f(bool c) { c ? 0 : super; +// ^^^^^ +// [diag.missingAssignableSelector] Missing selector such as '.identifier' or '[0]'. } } -''', - [error(diag.missingAssignableSelector, 41, 5)], - ); +'''); var node = findNode.singleConditionalExpression; assertResolvedNodeText(node, r''' @@ -106,7 +103,7 @@ ConditionalExpression } test_ifNull_lubUsedEvenIfItDoesNotSatisfyContext() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' // @dart=3.3 class A {} class B1 extends A {} @@ -142,19 +139,18 @@ ConditionalExpression } test_issue49692() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' T f(T t, bool b) { if (t is int) { final u = b ? t : null; return u; +// ^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'int?' can't be returned from the function 'f' because it has a return type of 'T'. } else { return t; } } -''', - [error(diag.returnOfInvalidTypeFromFunction, 79, 1)], - ); +'''); var node = findNode.conditionalExpression('b ?'); assertResolvedNodeText(node, r''' @@ -177,7 +173,7 @@ ConditionalExpression } test_recordType_differentShape() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' void f(bool b, (int, String) r1, ({int a}) r2) { b ? r1 : r2; } @@ -205,7 +201,7 @@ ConditionalExpression } test_recordType_sameShape_named() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' void f(bool b, ({int a}) r1, ({double a}) r2) { b ? r1 : r2; } @@ -233,16 +229,15 @@ ConditionalExpression } test_then_super() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A { void f(bool c) { c ? super : 0; +// ^^^^^ +// [diag.missingAssignableSelector] Missing selector such as '.identifier' or '[0]'. } } -''', - [error(diag.missingAssignableSelector, 37, 5)], - ); +'''); var node = findNode.singleConditionalExpression; assertResolvedNodeText(node, r''' @@ -264,7 +259,7 @@ ConditionalExpression } test_type_int_double() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' void f(bool b) { b ? 0 : 1.2; } @@ -290,7 +285,7 @@ ConditionalExpression } test_type_int_null() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' void f(bool b) { b ? 42 : null; } @@ -329,7 +324,7 @@ void f(bool a, int b, int c) { @reflectiveTest class InferenceUpdate3Test extends PubPackageResolutionTest { test_contextIsConvertedToATypeUsingGreatestClosure() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' class A {} class B1 extends A {} class B2 extends A {} @@ -367,7 +362,7 @@ f(bool b, C1 c1, C2 c2) { } test_contextNotUsedIfLhsDoesNotSatisfyContext() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' class A {} class B1 extends A {} class B2 extends A {} @@ -404,7 +399,7 @@ f(bool b, B2 b2, C1 c1, Object? o) { } test_contextNotUsedIfRhsDoesNotSatisfyContext() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' class A {} class B1 extends A {} class B2 extends A {} @@ -441,7 +436,7 @@ f(bool b, C1 c1, B2 b2, Object? o) { } test_contextUsedInsteadOfLubIfLubDoesNotSatisfyContext() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' class A {} class B1 extends A {} class B2 extends A {} diff --git a/pkg/analyzer/test/src/dart/resolution/constant_pattern_test.dart b/pkg/analyzer/test/src/dart/resolution/constant_pattern_test.dart index 3cdcc502eb0..f6a441425fa 100644 --- a/pkg/analyzer/test/src/dart/resolution/constant_pattern_test.dart +++ b/pkg/analyzer/test/src/dart/resolution/constant_pattern_test.dart @@ -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 'package:analyzer/src/diagnostic/diagnostic.dart' as diag; import 'package:test_reflective_loader/test_reflective_loader.dart'; import 'context_collection_resolution.dart'; @@ -18,7 +17,7 @@ main() { @reflectiveTest class ConstantPatternResolutionTest extends PubPackageResolutionTest { test_expression_class_field() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { static const foo = 0; } @@ -48,7 +47,7 @@ ConstantPattern } test_expression_instanceCreation() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { const A(); } @@ -78,7 +77,7 @@ ConstantPattern } test_expression_integerLiteral() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' void f(x) { if (x case 0) {} } @@ -94,7 +93,7 @@ ConstantPattern } test_expression_integerLiteral_contextType_double() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' void f(double x) { switch (x) { case 0: @@ -113,7 +112,7 @@ ConstantPattern } test_expression_listLiteral() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' void f(x) { if (x case const [0]) {} } @@ -135,7 +134,7 @@ ConstantPattern } test_expression_mapLiteral() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' void f(x) { if (x case const {0: 1}) {} } @@ -169,7 +168,7 @@ class A { } '''); - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' import 'a.dart' as prefix; void f(x) { @@ -208,7 +207,7 @@ ConstantPattern const foo = 0; '''); - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' import 'a.dart' as prefix; void f(x) { @@ -236,7 +235,7 @@ ConstantPattern } test_expression_setLiteral() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' void f(x) { if (x case const {0, 1}) {} } @@ -262,7 +261,7 @@ ConstantPattern } test_expression_topLevelVariable() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' const foo = 0; void f(x) { @@ -281,7 +280,7 @@ ConstantPattern } test_expression_typeLiteral_notPrefixed_dynamicElement() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' void f(Object? x) { if (x case dynamic) {} } @@ -300,7 +299,7 @@ ConstantPattern } test_expression_typeLiteral_notPrefixed_interfaceElement() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' void f(Object? x) { if (x case int) {} } @@ -319,7 +318,7 @@ ConstantPattern } test_expression_typeLiteral_notPrefixed_nested() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' void f(Object? x) { if (x case [0, int]) {} } @@ -349,7 +348,7 @@ ListPattern } test_expression_typeLiteral_notPrefixed_neverElement() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' void f(Object? x) { if (x case Never) {} } @@ -368,7 +367,7 @@ ConstantPattern } test_expression_typeLiteral_notPrefixed_typeAliasElement() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' typedef A = int; void f(Object? x) { @@ -390,14 +389,13 @@ ConstantPattern } test_expression_typeLiteral_notPrefixed_typeParameterElement() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f(Object? x) { if (x case T) {} +// ^ +// [diag.constTypeParameter] Type parameters can't be used in a constant expression. } -''', - [error(diag.constTypeParameter, 36, 1)], - ); +'''); var node = findNode.singleGuardedPattern.pattern; assertResolvedNodeText(node, r''' ConstantPattern @@ -412,7 +410,7 @@ ConstantPattern } test_expression_typeLiteral_prefixed_dynamicElement() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:core' as core; void f(core.Object? x) { @@ -437,7 +435,7 @@ ConstantPattern } test_expression_typeLiteral_prefixed_interfaceElement() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:core' as core; void f(core.Object? x) { @@ -462,7 +460,7 @@ ConstantPattern } test_expression_typeLiteral_prefixed_neverElement() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:core' as core; void f(core.Object? x) { @@ -491,7 +489,7 @@ ConstantPattern typedef A = int; '''); - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' import 'a.dart' as prefix; void f(Object? x) { @@ -518,7 +516,7 @@ ConstantPattern } test_location_ifCase() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' void f(x) { if (x case 0) {} } @@ -534,7 +532,7 @@ ConstantPattern } test_location_switchCase() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' void f(x) { switch (x) { case 0: diff --git a/pkg/analyzer/test/src/dart/resolution/constant_test.dart b/pkg/analyzer/test/src/dart/resolution/constant_test.dart index 9001c28290a..0551dd2ed47 100644 --- a/pkg/analyzer/test/src/dart/resolution/constant_test.dart +++ b/pkg/analyzer/test/src/dart/resolution/constant_test.dart @@ -4,15 +4,16 @@ import 'package:analyzer/src/dart/constant/value.dart'; import 'package:analyzer/src/dart/element/element.dart'; -import 'package:analyzer/src/diagnostic/diagnostic.dart' as diag; import 'package:test/test.dart'; import 'package:test_reflective_loader/test_reflective_loader.dart'; import 'context_collection_resolution.dart'; +import 'node_text_expectations.dart'; main() { defineReflectiveSuite(() { defineReflectiveTests(ConstantResolutionTest); + defineReflectiveTests(UpdateNodeTextExpectations); }); } @@ -24,13 +25,12 @@ class A { const A({int p}); } '''); - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'a.dart'; const a = const A(); -''', - [error(diag.constConstructorParamTypeMismatch, 27, 9)], - ); +// ^^^^^^^^^ +// [diag.constConstructorParamTypeMismatch] A value of type 'Null' can't be assigned to a parameter of type 'int' in a const constructor. +'''); var aLib = findElement2.import('package:test/a.dart').importedLibrary!; var aConstructor = aLib.getClass('A')!.constructors.single; @@ -43,7 +43,7 @@ const a = const A(); } test_constFactoryRedirection_super() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class I { const factory I(int f) = B; } @@ -68,83 +68,78 @@ main() {} } test_constList_withNullAwareElement() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { const A(); foo() { return const [?A()]; +// ^ +// [diag.invalidNullAwareElement] The element can't be null, so the null-aware operator '?' is unnecessary. } } -''', - [error(diag.invalidNullAwareElement, 51, 1)], - ); +'''); assertType(findNode.listLiteral('const ['), 'List'); } test_constMap_withNullAwareKey() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { const A(); foo() { return const {?A(): 0}; +// ^ +// [diag.invalidNullAwareMapEntryKey] The map entry key can't be null, so the null-aware operator '?' is unnecessary. } } -''', - [error(diag.invalidNullAwareMapEntryKey, 51, 1)], - ); +'''); assertType(findNode.setOrMapLiteral('const {'), 'Map'); } test_constMap_withNullAwareValue() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { const A(); foo() { return const {0: ?A()}; +// ^ +// [diag.invalidNullAwareMapEntryValue] The map entry value can't be null, so the null-aware operator '?' is unnecessary. } } -''', - [error(diag.invalidNullAwareMapEntryValue, 54, 1)], - ); +'''); assertType(findNode.setOrMapLiteral('const {'), 'Map'); } test_constNotInitialized() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class B { const B(_); } class C extends B { static const a; +// ^ +// [diag.constNotInitialized] The constant 'a' must be initialized. const C() : super(a); } -''', - [error(diag.constNotInitialized, 62, 1)], - ); +'''); } test_constSet_withNullAwareElement() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { const A(); foo() { return const {?A()}; +// ^ +// [diag.invalidNullAwareElement] The element can't be null, so the null-aware operator '?' is unnecessary. } } -''', - [error(diag.invalidNullAwareElement, 51, 1)], - ); +'''); assertType(findNode.setOrMapLiteral('const {'), 'Set'); } test_context_eliminateTypeVariables() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { const A({List a = const []}); } @@ -153,7 +148,7 @@ class A { } test_context_eliminateTypeVariables_functionType() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { const A({List a = const []}); } @@ -173,7 +168,7 @@ class C { const C(); } '''); - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' import 'a.dart'; const v = a; @@ -266,7 +261,7 @@ class A { } '''); - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' import 'a.dart'; class B extends A { @@ -284,7 +279,7 @@ class B extends A { } test_local_prefixedIdentifier_staticField_extension() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' const a = E.f; extension E on int { diff --git a/pkg/analyzer/test/src/dart/resolution/constructor_field_initializer_test.dart b/pkg/analyzer/test/src/dart/resolution/constructor_field_initializer_test.dart index c9315486a10..355b96c74bf 100644 --- a/pkg/analyzer/test/src/dart/resolution/constructor_field_initializer_test.dart +++ b/pkg/analyzer/test/src/dart/resolution/constructor_field_initializer_test.dart @@ -2,14 +2,15 @@ // 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/diagnostic/diagnostic.dart' as diag; import 'package:test_reflective_loader/test_reflective_loader.dart'; import 'context_collection_resolution.dart'; +import 'node_text_expectations.dart'; main() { defineReflectiveSuite(() { defineReflectiveTests(ConstructorFieldInitializerResolutionTest); + defineReflectiveTests(UpdateNodeTextExpectations); }); } @@ -17,7 +18,7 @@ main() { class ConstructorFieldInitializerResolutionTest extends PubPackageResolutionTest { test_fieldOfAugmentation() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { int get foo; } @@ -46,7 +47,7 @@ ConstructorFieldInitializer } test_formalParameter() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' class A { final int f; A(int a) : f = a; @@ -181,16 +182,15 @@ ConstructorFieldInitializer } test_invalid_declarationAndInitializer() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A { final x = 0; const A() : x = a; +// ^ +// [diag.fieldInitializedInInitializerAndDeclaration] Fields can't be initialized in the constructor if they are final and were already initialized at their declaration. } const a = 0; -''', - [error(diag.fieldInitializedInInitializerAndDeclaration, 39, 1)], - ); +'''); var node = findNode.singleConstructorFieldInitializer; assertResolvedNodeText(node, r''' @@ -208,16 +208,15 @@ ConstructorFieldInitializer } test_invalid_notField_class() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A { const A() : X = a; +// ^^^^^ +// [diag.initializerForNonExistentField] 'X' isn't a field in the enclosing class. } const a = 0; class X {} -''', - [error(diag.initializerForNonExistentField, 24, 5)], - ); +'''); var node = findNode.singleConstructorFieldInitializer; assertResolvedNodeText(node, r''' @@ -235,16 +234,15 @@ ConstructorFieldInitializer } test_invalid_notField_getter() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A { A() : x = a; +// ^^^^^ +// [diag.initializerForNonExistentField] 'x' isn't a field in the enclosing class. int get x => 0; } const a = 0; -''', - [error(diag.initializerForNonExistentField, 18, 5)], - ); +'''); var node = findNode.singleConstructorFieldInitializer; assertResolvedNodeText(node, r''' @@ -262,19 +260,17 @@ ConstructorFieldInitializer } test_invalid_notField_importPrefix() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' import 'dart:async' as x; +// ^^^^^^^^^^^^ +// [diag.unusedImport] Unused import: 'dart:async'. class A { A() : x = a; +// ^^^^^ +// [diag.initializerForNonExistentField] 'x' isn't a field in the enclosing class. } const a = 0; -''', - [ - error(diag.unusedImport, 7, 12), - error(diag.initializerForNonExistentField, 44, 5), - ], - ); +'''); var node = findNode.singleConstructorFieldInitializer; assertResolvedNodeText(node, r''' @@ -292,16 +288,15 @@ ConstructorFieldInitializer } test_invalid_notField_method() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A { A() : x = a; +// ^^^^^ +// [diag.initializerForNonExistentField] 'x' isn't a field in the enclosing class. void x() {} } const a = 0; -''', - [error(diag.initializerForNonExistentField, 18, 5)], - ); +'''); var node = findNode.singleConstructorFieldInitializer; assertResolvedNodeText(node, r''' @@ -319,16 +314,15 @@ ConstructorFieldInitializer } test_invalid_notField_setter() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A { A() : x = a; +// ^^^^^ +// [diag.initializerForNonExistentField] 'x' isn't a field in the enclosing class. set x(int _) {} } const a = 0; -''', - [error(diag.initializerForNonExistentField, 18, 5)], - ); +'''); var node = findNode.singleConstructorFieldInitializer; assertResolvedNodeText(node, r''' @@ -346,16 +340,15 @@ ConstructorFieldInitializer } test_invalid_notField_topLevelFunction() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A { A() : x = a; +// ^^^^^ +// [diag.initializerForNonExistentField] 'x' isn't a field in the enclosing class. } const a = 0; void x() {} -''', - [error(diag.initializerForNonExistentField, 18, 5)], - ); +'''); var node = findNode.singleConstructorFieldInitializer; assertResolvedNodeText(node, r''' @@ -373,16 +366,15 @@ ConstructorFieldInitializer } test_invalid_notField_topLevelVariable() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A { A() : x = a; +// ^^^^^ +// [diag.initializerForNonExistentField] 'x' isn't a field in the enclosing class. } const a = 0; var x = 0; -''', - [error(diag.initializerForNonExistentField, 18, 5)], - ); +'''); var node = findNode.singleConstructorFieldInitializer; assertResolvedNodeText(node, r''' @@ -400,15 +392,14 @@ ConstructorFieldInitializer } test_invalid_notField_typeParameter() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A { A() : T = a; +// ^^^^^ +// [diag.initializerForNonExistentField] 'T' isn't a field in the enclosing class. } const a = 0; -''', - [error(diag.initializerForNonExistentField, 21, 5)], - ); +'''); var node = findNode.singleConstructorFieldInitializer; assertResolvedNodeText(node, r''' @@ -426,15 +417,14 @@ ConstructorFieldInitializer } test_invalid_notField_unresolved() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A { A() : x = a; +// ^^^^^ +// [diag.initializerForNonExistentField] 'x' isn't a field in the enclosing class. } const a = 0; -''', - [error(diag.initializerForNonExistentField, 18, 5)], - ); +'''); var node = findNode.singleConstructorFieldInitializer; assertResolvedNodeText(node, r''' diff --git a/pkg/analyzer/test/src/dart/resolution/constructor_reference_test.dart b/pkg/analyzer/test/src/dart/resolution/constructor_reference_test.dart index a1437a2d086..5601783f05b 100644 --- a/pkg/analyzer/test/src/dart/resolution/constructor_reference_test.dart +++ b/pkg/analyzer/test/src/dart/resolution/constructor_reference_test.dart @@ -2,10 +2,10 @@ // 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/diagnostic/diagnostic.dart' as diag; import 'package:test_reflective_loader/test_reflective_loader.dart'; import 'context_collection_resolution.dart'; +import 'node_text_expectations.dart'; main() { defineReflectiveSuite(() { @@ -14,13 +14,14 @@ main() { defineReflectiveTests( ConstructorReferenceResolutionTest_WithoutConstructorTearoffs, ); + defineReflectiveTests(UpdateNodeTextExpectations); }); } @reflectiveTest class ConstructorReferenceResolutionTest extends PubPackageResolutionTest { test_abstractClass_factory() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' abstract class A { factory A() => A2(); } @@ -51,18 +52,17 @@ ConstructorReference } test_abstractClass_generative() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' abstract class A { A(); } foo() { A.new; +//^^^^^ +// [diag.tearoffOfGenerativeConstructorOfAbstractClass] A generative constructor of an abstract class can't be torn off. } -''', - [error(diag.tearoffOfGenerativeConstructorOfAbstractClass, 39, 5)], - ); +'''); var node = findNode.constructorReference('A.new;'); assertResolvedNodeText(node, r''' @@ -83,8 +83,7 @@ ConstructorReference } test_abstractClass_redirecting() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' abstract class A { A(): this.two(); @@ -93,10 +92,10 @@ abstract class A { foo() { A.new; +//^^^^^ +// [diag.tearoffOfGenerativeConstructorOfAbstractClass] A generative constructor of an abstract class can't be torn off. } -''', - [error(diag.tearoffOfGenerativeConstructorOfAbstractClass, 63, 5)], - ); +'''); var node = findNode.constructorReference('A.new;'); assertResolvedNodeText(node, r''' @@ -117,25 +116,18 @@ ConstructorReference } test_class_generic_inferFromContext_badTypeArgument() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A { A.foo(); } A Function() bar() { +// [context 1][column 1][length 9] The inverted type 'A' is also not regular-bounded, so the type is not well-bounded. +//^^^^^^ +// [diag.typeArgumentNotMatchingBounds][context 1] 'String' doesn't conform to the bound 'num' of the type parameter 'T'. return A.foo; } -''', - [ - error( - diag.typeArgumentNotMatchingBounds, - 41, - 6, - contextMessages: [message(testFile, 39, 9)], - ), - ], - ); +'''); var node = findNode.constructorReference('A.foo;'); assertResolvedNodeText(node, r''' @@ -158,7 +150,7 @@ ConstructorReference } test_class_generic_named_inferTypeFromContext() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' class A { A.foo(); } @@ -189,7 +181,7 @@ ConstructorReference } test_class_generic_named_uninstantiated() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' class A { A.foo(); } @@ -218,7 +210,7 @@ ConstructorReference } test_class_generic_named_uninstantiated_bound() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' class A { A.foo(); } @@ -247,7 +239,7 @@ ConstructorReference } test_class_nonGeneric_const() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' class A { const A(); } @@ -274,7 +266,7 @@ ConstructorReference } test_class_nonGeneric_named() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' class A { A.foo(); } @@ -303,7 +295,7 @@ ConstructorReference } test_class_nonGeneric_unnamed() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' class A { A(); } @@ -338,7 +330,7 @@ class A { } typedef TA = A; '''); - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' import 'a.dart' as a; bar() { a.TA.foo; @@ -374,7 +366,7 @@ class A { } typedef TA = A; '''); - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' import 'a.dart' as a; bar() { a.TA.new; @@ -409,7 +401,7 @@ class A { A.foo(); } '''); - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' import 'a.dart' as a; bar() { a.A.foo; @@ -444,7 +436,7 @@ class A { A(); } '''); - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' import 'a.dart' as a; bar() { a.A.new; @@ -474,7 +466,7 @@ ConstructorReference } test_typeAlias_generic_const() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' class A { const A(); } @@ -502,7 +494,7 @@ ConstructorReference } test_typeAlias_generic_named_uninstantiated() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' class A { A.foo(); } @@ -532,7 +524,7 @@ ConstructorReference } test_typeAlias_instantiated_const() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' class A { const A(); } @@ -564,7 +556,7 @@ ConstructorReference } test_typeAlias_instantiated_named() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' class A { A.foo(); } @@ -602,7 +594,7 @@ ConstructorReference class ConstructorReferenceResolutionTest_TypeArgs extends PubPackageResolutionTest { test_alias_generic_const() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' class A { const A.foo(); } @@ -646,7 +638,7 @@ ConstructorReference } test_alias_generic_const_differingNumberOfTypeParameters() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' class A { A.foo() {} } @@ -686,7 +678,7 @@ ConstructorReference } test_alias_generic_named() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' class A { A.foo(); } @@ -732,7 +724,7 @@ ConstructorReference } test_alias_generic_uninstantiated_const() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' class A { const A.foo(); } @@ -760,7 +752,7 @@ ConstructorReference } test_alias_generic_unnamed() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' class A { A(); } @@ -802,8 +794,7 @@ ConstructorReference } test_alias_generic_with_inferred_type_parameter() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class C { final T x; C(this.x); @@ -811,14 +802,14 @@ class C { typedef Direct = C; void main() { var x = const Function(int)>[Direct.new]; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'x' isn't used. } -''', - [error(diag.unusedLocalVariable, 87, 1)], - ); +'''); } test_alias_genericWithBound_unnamed() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' class A { A(); } @@ -860,8 +851,7 @@ ConstructorReference } test_alias_genericWithBound_unnamed_badBound() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A { A(); } @@ -869,10 +859,10 @@ typedef TA = A; void bar() { TA.new; +// ^^^^^^ +// [diag.typeArgumentNotMatchingBounds] 'String' doesn't conform to the bound 'num' of the type parameter 'T'. } -''', - [error(diag.typeArgumentNotMatchingBounds, 75, 6)], - ); +'''); var node = findNode.constructorReference('TA.new;'); assertResolvedNodeText(node, r''' @@ -905,7 +895,7 @@ ConstructorReference } test_class_generic_const() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' class A { const A(); } @@ -944,7 +934,7 @@ ConstructorReference } test_class_generic_named() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' class A { A.foo(); } @@ -985,69 +975,59 @@ ConstructorReference } test_class_generic_named_cascade() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A { A.foo(); } void bar() { A..foo; +// ^ +// [diag.undefinedOperator] The operator '<' isn't defined for the type 'Type'. +// ^ +// [diag.equalityCannotBeEqualityOperand] A comparison expression can't be an operand of another comparison expression. +// ^^ +// [diag.missingIdentifier] Expected an identifier. } -''', - [ - error(diag.undefinedOperator, 43, 1), - error(diag.equalityCannotBeEqualityOperand, 47, 1), - error(diag.missingIdentifier, 48, 2), - ], - ); +'''); // The parser produces nonsense here because the `<` disambiguates as a // relational operator, so no need to assert anything about analysis // results. } test_class_generic_named_nullAware() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A { A.foo(); } void bar() { A?.foo; +// ^ +// [diag.undefinedOperator] The operator '<' isn't defined for the type 'Type'. +// ^ +// [diag.equalityCannotBeEqualityOperand] A comparison expression can't be an operand of another comparison expression. +// ^^ +// [diag.missingIdentifier] Expected an identifier. } -''', - [ - error(diag.undefinedOperator, 43, 1), - error(diag.equalityCannotBeEqualityOperand, 47, 1), - error(diag.missingIdentifier, 48, 2), - ], - ); +'''); // The parser produces nonsense here because the `<` disambiguates as a // relational operator, so no need to assert anything about analysis // results. } test_class_generic_named_typeArgs() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A { A.foo(); } void bar() { A.foo; +// ^^^^^ +// [diag.wrongNumberOfTypeArgumentsConstructor] The constructor 'A.foo' doesn't have type parameters. } -''', - [ - error( - diag.wrongNumberOfTypeArgumentsConstructor, - 52, - 5, - messageContains: ["The constructor 'A.foo'"], - ), - ], - ); +'''); var node = findNode.constructorReference('A.foo;'); assertResolvedNodeText(node, r''' @@ -1080,25 +1060,17 @@ ConstructorReference } test_class_generic_new_typeArgs() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A { A.new(); } void bar() { A.new; +// ^^^^^ +// [diag.wrongNumberOfTypeArgumentsConstructor] The constructor 'A.new' doesn't have type parameters. } -''', - [ - error( - diag.wrongNumberOfTypeArgumentsConstructor, - 52, - 5, - messageContains: ["The constructor 'A.new'"], - ), - ], - ); +'''); var node = findNode.constructorReference('A.new;'); assertResolvedNodeText(node, r''' @@ -1131,18 +1103,17 @@ ConstructorReference } test_class_generic_nonConstructor() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A { static int i = 1; } void bar() { A.i; +//^^^^^^^^ +// [diag.classInstantiationAccessToStaticMember] The static member 'i' can't be accessed on a class instantiation. } -''', - [error(diag.classInstantiationAccessToStaticMember, 51, 8)], - ); +'''); var node = findNode.constructorReference('A.i;'); assertResolvedNodeText(node, r''' @@ -1171,18 +1142,17 @@ ConstructorReference } test_class_generic_nothing_hasNamedConstructor() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A { A.foo(); } void bar() { A.; +// ^ +// [diag.missingIdentifier] Expected an identifier. } -''', - [error(diag.missingIdentifier, 49, 1)], - ); +'''); var node = findNode.constructorReference('A.;'); assertResolvedNodeText(node, r''' @@ -1211,7 +1181,7 @@ ConstructorReference } test_class_generic_unnamed() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' class A { A(); } @@ -1252,7 +1222,7 @@ ConstructorReference } test_class_generic_unnamed_partOfPropertyAccess() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' class A { A(); } @@ -1293,7 +1263,7 @@ ConstructorReference } test_class_genericWithBound_unnamed() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' class A { A(); } @@ -1334,18 +1304,17 @@ ConstructorReference } test_class_genericWithBound_unnamed_badBound() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A { A(); } void bar() { A.new; +// ^^^^^^ +// [diag.typeArgumentNotMatchingBounds] 'String' doesn't conform to the bound 'num' of the type parameter 'T'. } -''', - [error(diag.typeArgumentNotMatchingBounds, 52, 6)], - ); +'''); var node = findNode.constructorReference('A.new;'); assertResolvedNodeText(node, r''' @@ -1384,7 +1353,7 @@ class A { } typedef TA = A; '''); - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' import 'a.dart' as a; void bar() { a.TA.new; @@ -1431,7 +1400,7 @@ class A { A.foo(); } '''); - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' import 'a.dart' as a; void bar() { a.A.foo; @@ -1478,7 +1447,7 @@ class A { A(); } '''); - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' import 'a.dart' as a; extension on Function { void m() {} @@ -1528,7 +1497,7 @@ class A { A(); } '''); - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' import 'a.dart' as a; void bar() { a.A.new; @@ -1575,18 +1544,17 @@ class ConstructorReferenceResolutionTest_WithoutConstructorTearoffs extends PubPackageResolutionTest with WithoutConstructorTearoffsMixin { test_class_generic_nonConstructor() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A { static int i = 1; } void bar() { A.i; +// ^^^^^ +// [diag.experimentNotEnabled] This requires the 'constructor-tearoffs' language feature to be enabled. } -''', - [error(diag.experimentNotEnabled, 52, 5)], - ); +'''); var node = findNode.constructorReference('A.i;'); assertResolvedNodeText(node, r''' @@ -1615,18 +1583,17 @@ ConstructorReference } test_constructorTearoff() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A { A.foo(); } void bar() { A.foo; +//^^^^^ +// [diag.sdkVersionConstructorTearoffs] Tearing off a constructor requires the 'constructor-tearoffs' language feature. } -''', - [error(diag.sdkVersionConstructorTearoffs, 39, 5)], - ); +'''); var node = findNode.constructorReference('A.foo;'); assertResolvedNodeText(node, r''' diff --git a/pkg/analyzer/test/src/dart/resolution/constructor_test.dart b/pkg/analyzer/test/src/dart/resolution/constructor_test.dart index 69d8b596609..45bc99dd316 100644 --- a/pkg/analyzer/test/src/dart/resolution/constructor_test.dart +++ b/pkg/analyzer/test/src/dart/resolution/constructor_test.dart @@ -2,21 +2,22 @@ // 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/diagnostic/diagnostic.dart' as diag; import 'package:test_reflective_loader/test_reflective_loader.dart'; import 'context_collection_resolution.dart'; +import 'node_text_expectations.dart'; main() { defineReflectiveSuite(() { defineReflectiveTests(ConstructorDeclarationResolutionTest); + defineReflectiveTests(UpdateNodeTextExpectations); }); } @reflectiveTest class ConstructorDeclarationResolutionTest extends PubPackageResolutionTest { test_factory_redirect_generic_instantiated() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A implements B { A(T a); } @@ -47,16 +48,15 @@ ConstructorName } test_fieldShadowingWildcardParameter() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { var v; var _; A(_) : v = _; +// ^ +// [diag.implicitThisReferenceInInitializer] The instance member '_' can't be accessed in an initializer. } -''', - [error(diag.implicitThisReferenceInInitializer, 41, 1)], - ); +'''); var node = findNode.constructorFieldInitializer('v = _'); assertResolvedNodeText(node, r''' @@ -74,7 +74,7 @@ ConstructorFieldInitializer } test_formalParameterScope() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' class a {} class B { @@ -121,16 +121,17 @@ ConstructorDeclaration } test_privateNamedParameter_accessInInitializer() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C { int? _x; +// ^^ +// [diag.unusedField] The value of the field '_x' isn't used. int? _y; +// ^^ +// [diag.unusedField] The value of the field '_y' isn't used. C({this._x}) : _y = _x; } -''', - [error(diag.unusedField, 17, 2), error(diag.unusedField, 28, 2)], - ); +'''); var node = findNode.singleConstructorFieldInitializer; assertResolvedNodeText(node, r''' @@ -148,15 +149,14 @@ ConstructorFieldInitializer } test_privateNamedParameter_fieldFormal() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C { int? _x; +// ^^ +// [diag.unusedField] The value of the field '_x' isn't used. C({this._x}); } -''', - [error(diag.unusedField, 17, 2)], - ); +'''); var node = findNode.singleConstructorDeclaration; assertResolvedNodeText(node, r''' @@ -172,7 +172,7 @@ ConstructorDeclaration thisKeyword: this period: . name: _x - declaredFragment: x@31 + declaredFragment: x@103 element: hasImplicitType isFinal isPublic type: int? field: ::@class::C::@field::_x @@ -189,14 +189,13 @@ ConstructorDeclaration test_privateNamedParameter_nonFieldFormal() async { // The user is incorrectly using a private named parameter for a non-field // parameter. This is erroneous, but resolve using the private name. - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C { C({int? _x}); +// ^^ +// [diag.privateNamedNonFieldParameter] Named parameters that don't refer to instance variables can't start with underscore. } -''', - [error(diag.privateNamedNonFieldParameter, 20, 2)], - ); +'''); var node = findNode.singleConstructorDeclaration; assertResolvedNodeText(node, r''' @@ -229,7 +228,7 @@ ConstructorDeclaration } test_redirectedConstructor_named() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A implements B { A.named(); } @@ -271,7 +270,7 @@ ConstructorDeclaration } test_redirectedConstructor_named_generic() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A implements B { A.named(); } @@ -325,18 +324,17 @@ ConstructorDeclaration } test_redirectedConstructor_named_unresolved() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A implements B { A(); } class B { factory B() = A.named; +// ^^^^^^^ +// [diag.redirectToMissingConstructor] The constructor 'A.named' couldn't be found in 'A'. } -''', - [error(diag.redirectToMissingConstructor, 59, 7)], - ); +'''); var node = findNode.constructorDeclaration('factory B'); assertResolvedNodeText(node, r''' @@ -370,7 +368,7 @@ ConstructorDeclaration } test_redirectedConstructor_unnamed() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A implements B { A(); } @@ -409,7 +407,7 @@ ConstructorDeclaration } test_redirectedConstructor_unnamed_generic() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A implements B { A(); } @@ -458,18 +456,17 @@ ConstructorDeclaration } test_redirectedConstructor_unnamed_unresolved() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A implements B { A.named(); } class B { factory B.named() = A; +// ^ +// [diag.redirectToMissingConstructor] The constructor 'A' couldn't be found in 'A'. } -''', - [error(diag.redirectToMissingConstructor, 71, 1)], - ); +'''); var node = findNode.constructorDeclaration('factory B'); assertResolvedNodeText(node, r''' diff --git a/pkg/analyzer/test/src/dart/resolution/declared_variable_pattern_test.dart b/pkg/analyzer/test/src/dart/resolution/declared_variable_pattern_test.dart index 0a0270efde9..61924066590 100644 --- a/pkg/analyzer/test/src/dart/resolution/declared_variable_pattern_test.dart +++ b/pkg/analyzer/test/src/dart/resolution/declared_variable_pattern_test.dart @@ -2,31 +2,31 @@ // 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/diagnostic/diagnostic.dart' as diag; import 'package:test_reflective_loader/test_reflective_loader.dart'; import 'context_collection_resolution.dart'; +import 'node_text_expectations.dart'; main() { defineReflectiveSuite(() { defineReflectiveTests(DeclaredVariablePatternResolutionTest); + defineReflectiveTests(UpdateNodeTextExpectations); }); } @reflectiveTest class DeclaredVariablePatternResolutionTest extends PubPackageResolutionTest { test_final_switchCase() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f(int x) { switch (x) { case final y: +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. break; } } -''', - [error(diag.unusedLocalVariable, 46, 1)], - ); +'''); var node = findNode.singleGuardedPattern.pattern; assertResolvedNodeText(node, r''' DeclaredVariablePattern @@ -40,17 +40,16 @@ DeclaredVariablePattern } test_final_typed_switchCase() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f(x) { switch (x) { case final int y: +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. break; } } -''', - [error(diag.unusedLocalVariable, 46, 1)], - ); +'''); var node = findNode.singleGuardedPattern.pattern; assertResolvedNodeText(node, r''' DeclaredVariablePattern @@ -68,7 +67,7 @@ DeclaredVariablePattern } test_patternVariableDeclaration_final_recordPattern_listPattern() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' void f() { // ignore:unused_local_variable final [a] = [0]; @@ -92,7 +91,7 @@ ListPattern } test_patternVariableDeclaration_final_recordPattern_listPattern_restPattern() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' void f() { // ignore:unused_local_variable final [...a] = [0, 1, 2]; @@ -118,7 +117,7 @@ ListPattern } test_patternVariableDeclaration_final_recordPattern_mapPattern_entry() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' void f() { // ignore:unused_local_variable final {0: a} = {0: 1}; @@ -147,7 +146,7 @@ MapPattern } test_patternVariableDeclaration_final_recordPattern_objectPattern() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' void f() { // ignore:unused_local_variable final int(sign: a) = 0; @@ -179,7 +178,7 @@ ObjectPattern } test_patternVariableDeclaration_final_recordPattern_parenthesizedPattern() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' void f() { // ignore:unused_local_variable final (a) = 0; @@ -201,7 +200,7 @@ ParenthesizedPattern } test_patternVariableDeclaration_final_recordPattern_recordPattern() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' void f() { // ignore:unused_local_variable final (a,) = (0,); @@ -226,17 +225,16 @@ RecordPattern } test_typed_switchCase() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f(x) { switch (x) { case int y: +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. break; } } -''', - [error(diag.unusedLocalVariable, 40, 1)], - ); +'''); var node = findNode.singleGuardedPattern.pattern; assertResolvedNodeText(node, r''' DeclaredVariablePattern @@ -253,16 +251,15 @@ DeclaredVariablePattern } test_var_demoteType() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f(T x) { if (x is int) { if (x case var y) {} +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. } } -''', - [error(diag.unusedLocalVariable, 54, 1)], - ); +'''); var node = findNode.singleGuardedPattern.pattern; assertResolvedNodeText(node, r''' @@ -277,14 +274,13 @@ DeclaredVariablePattern } test_var_ifCase() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f(int x) { if (x case var y) {} +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. } -''', - [error(diag.unusedLocalVariable, 33, 1)], - ); +'''); var node = findNode.singleGuardedPattern.pattern; assertResolvedNodeText(node, r''' DeclaredVariablePattern @@ -298,14 +294,13 @@ DeclaredVariablePattern } test_var_nullOrEquivalent_neverQuestion() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f(Never? x) { if (x case var y) {} +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. } -''', - [error(diag.unusedLocalVariable, 36, 1)], - ); +'''); var node = findNode.singleGuardedPattern.pattern; assertResolvedNodeText(node, r''' DeclaredVariablePattern @@ -319,14 +314,13 @@ DeclaredVariablePattern } test_var_nullOrEquivalent_nullNone() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f(Null x) { if (x case var y) {} +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. } -''', - [error(diag.unusedLocalVariable, 34, 1)], - ); +'''); var node = findNode.singleGuardedPattern.pattern; assertResolvedNodeText(node, r''' DeclaredVariablePattern @@ -340,17 +334,16 @@ DeclaredVariablePattern } test_var_switchCase() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f(int x) { switch (x) { case var y: +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. break; } } -''', - [error(diag.unusedLocalVariable, 44, 1)], - ); +'''); var node = findNode.singleGuardedPattern.pattern; assertResolvedNodeText(node, r''' DeclaredVariablePattern @@ -364,17 +357,16 @@ DeclaredVariablePattern } test_var_switchCase_cast() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f(num x) { switch (x) { case var y as int: +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. break; } } -''', - [error(diag.unusedLocalVariable, 44, 1)], - ); +'''); var node = findNode.singleGuardedPattern.pattern; assertResolvedNodeText(node, r''' CastPattern diff --git a/pkg/analyzer/test/src/dart/resolution/dot_shorthand_constructor_invocation_test.dart b/pkg/analyzer/test/src/dart/resolution/dot_shorthand_constructor_invocation_test.dart index 5f488197ecc..be07e268d64 100644 --- a/pkg/analyzer/test/src/dart/resolution/dot_shorthand_constructor_invocation_test.dart +++ b/pkg/analyzer/test/src/dart/resolution/dot_shorthand_constructor_invocation_test.dart @@ -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 'package:analyzer/src/diagnostic/diagnostic.dart' as diag; import 'package:test_reflective_loader/test_reflective_loader.dart'; import 'context_collection_resolution.dart'; @@ -19,23 +18,21 @@ main() { class DotShorthandConstructorInvocationResolutionTest extends PubPackageResolutionTest { test_abstractClass() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' abstract class Foo { Foo(); } void main() { Foo _ = .new(); +// ^^^^^^ +// [diag.instantiateAbstractClass] Abstract classes can't be instantiated. } -''', - [error(diag.instantiateAbstractClass, 60, 6)], - ); +'''); } test_abstractClass_const() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' abstract class C { static C fn() => CB.named(1); } @@ -47,16 +44,15 @@ class CB implements C { void main() { C c = const .fn(1); +// ^^ +// [diag.constWithUndefinedConstructor] The class 'C' doesn't have a constant constructor 'fn'. print(c); } -''', - [error(diag.constWithUndefinedConstructor, 145, 2)], - ); +'''); } test_abstractClass_const_typeArguments() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' abstract class C { static C fn() => CB.named(1); } @@ -68,18 +64,17 @@ class CB implements C { void main() { C c = const .fn(1); +// ^^ +// [diag.constWithUndefinedConstructor] The class 'C' doesn't have a constant constructor 'fn'. +// ^^^^^ +// [diag.wrongNumberOfTypeArgumentsDotShorthandConstructor] The dot shorthand resolves to the constructor 'C.fn', and type parameters can't be applied to dot shorthand constructor invocations. print(c); } -''', - [ - error(diag.constWithUndefinedConstructor, 145, 2), - error(diag.wrongNumberOfTypeArgumentsDotShorthandConstructor, 147, 5), - ], - ); +'''); } test_abstractClass_factory() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' void main() async { var iter = [1, 2]; await for (var x in .fromIterable(iter)) { @@ -115,7 +110,7 @@ DotShorthandConstructorInvocation } test_abstractClass_factory_const() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' abstract class Foo { const factory Foo.a() = _Foo; @@ -149,8 +144,7 @@ DotShorthandConstructorInvocation } test_abstractClass_factory_const_typeArguments() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' abstract class Foo { const factory Foo.a() = _Foo; @@ -162,14 +156,13 @@ class _Foo extends Foo { } Foo bar() => const .a(); -''', - [error(diag.wrongNumberOfTypeArgumentsDotShorthandConstructor, 154, 5)], - ); +// ^^^^^ +// [diag.wrongNumberOfTypeArgumentsDotShorthandConstructor] The dot shorthand resolves to the constructor 'Foo.a', and type parameters can't be applied to dot shorthand constructor invocations. +'''); } test_abstractClass_factory_typeArguments() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' abstract class Foo { factory Foo.a() = _Foo; @@ -181,39 +174,37 @@ class _Foo extends Foo { } Foo bar() => .a(); -''', - [error(diag.wrongNumberOfTypeArgumentsDotShorthandConstructor, 128, 3)], - ); +// ^^^ +// [diag.wrongNumberOfTypeArgumentsDotShorthandConstructor] The dot shorthand resolves to the constructor 'Foo.a', and type parameters can't be applied to dot shorthand constructor invocations. +'''); } test_abstractClass_function() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' Function getFunction() { return .new(); +// ^^^^^^ +// [diag.instantiateAbstractClass] Abstract classes can't be instantiated. } -''', - [error(diag.instantiateAbstractClass, 34, 6)], - ); +'''); } test_abstractClass_typeArguments() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' abstract class Foo { Foo(); } void main() { Foo _ = .new(); +// ^^^^^^^^^^^ +// [diag.instantiateAbstractClass] Abstract classes can't be instantiated. } -''', - [error(diag.instantiateAbstractClass, 60, 11)], - ); +'''); } test_chain_method() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class C { int x; C(this.x); @@ -259,7 +250,7 @@ MethodInvocation } test_chain_method_const() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class C { final int x; const C(this.x); @@ -306,7 +297,7 @@ MethodInvocation } test_chain_property() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class C { int x; C(this.x); @@ -348,7 +339,7 @@ PropertyAccess } test_chain_property_const() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class C { final int x; const C(this.x); @@ -391,7 +382,7 @@ PropertyAccess } test_conflict_instance_getter() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { final int value; // Same name as constructor A.value(this.value); @@ -426,7 +417,7 @@ DotShorthandConstructorInvocation } test_conflict_instance_method() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { final int val; A.value(this.val); @@ -462,7 +453,7 @@ DotShorthandConstructorInvocation } test_conflict_instance_method_factory() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { final int val; A._(this.val); @@ -494,7 +485,7 @@ DotShorthandConstructorInvocation } test_conflict_instance_setter() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class A { int? val; A.value(this.val); @@ -534,7 +525,7 @@ DotShorthandConstructorInvocation } test_const_assert() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class C { final int x; const C.named(this.x); @@ -570,7 +561,7 @@ DotShorthandConstructorInvocation } test_const_inConstantContext() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class C { final int x; const C.named(this.x); @@ -604,7 +595,7 @@ DotShorthandConstructorInvocation } test_const_keyword() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class C { final int x; const C.named(this.x); @@ -639,8 +630,7 @@ DotShorthandConstructorInvocation } test_const_nonConst_constructor() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C { final int x; C.named(this.x); @@ -648,16 +638,15 @@ class C { void main() { C c = const .named(1); +// ^^^^^ +// [diag.constWithNonConst] The constructor being called isn't a const constructor. print(c); } -''', - [error(diag.constWithNonConst, 69, 5)], - ); +'''); } test_const_nonConst_method() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C { static C fn() => C.named(1); final int x; @@ -666,15 +655,15 @@ class C { void main() { C c = const .fn(1); +// ^^ +// [diag.constWithUndefinedConstructor] The class 'C' doesn't have a constant constructor 'fn'. print(c); } -''', - [error(diag.constWithUndefinedConstructor, 107, 2)], - ); +'''); } test_constructor_named() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class C { int x; C.named(this.x); @@ -708,7 +697,7 @@ DotShorthandConstructorInvocation } test_constructor_named_futureOr() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:async'; class C { @@ -748,8 +737,7 @@ DotShorthandConstructorInvocation } test_enum_constructor() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' enum E { v.named(); @@ -758,15 +746,15 @@ enum E { void f() { E e = .named(); +// ^^^^^ +// [diag.invalidReferenceToGenerativeEnumConstructor] Generative enum constructors can only be used to create an enum constant. print(e); } -''', - [error(diag.invalidReferenceToGenerativeEnumConstructor, 65, 5)], - ); +'''); } test_equality() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class C { int x; C.named(this.x); @@ -802,7 +790,7 @@ DotShorthandConstructorInvocation } test_equality_inferTypeParameters() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' void main() { bool x = [] == .filled(2, '2'); print(x); @@ -838,7 +826,7 @@ DotShorthandConstructorInvocation } test_equality_pattern() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class C { final int x; const C.named(this.x); @@ -873,7 +861,7 @@ DotShorthandConstructorInvocation } test_factory() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class Foo { factory Foo.a() = _Foo; @@ -906,7 +894,7 @@ DotShorthandConstructorInvocation } test_factory_const() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class Foo { const factory Foo.a() = _Foo; @@ -940,8 +928,7 @@ DotShorthandConstructorInvocation } test_factory_const_typeArguments() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class Foo { const factory Foo.a() = _Foo; @@ -953,14 +940,13 @@ class _Foo extends Foo { } Foo bar() => const .a(); -''', - [error(diag.wrongNumberOfTypeArgumentsDotShorthandConstructor, 145, 5)], - ); +// ^^^^^ +// [diag.wrongNumberOfTypeArgumentsDotShorthandConstructor] The dot shorthand resolves to the constructor 'Foo.a', and type parameters can't be applied to dot shorthand constructor invocations. +'''); } test_factory_typeArguments() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class Foo { factory Foo.a() = _Foo; @@ -972,26 +958,25 @@ class _Foo extends Foo { } Foo bar() => .a(); -''', - [error(diag.wrongNumberOfTypeArgumentsDotShorthandConstructor, 119, 3)], - ); +// ^^^ +// [diag.wrongNumberOfTypeArgumentsDotShorthandConstructor] The dot shorthand resolves to the constructor 'Foo.a', and type parameters can't be applied to dot shorthand constructor invocations. +'''); } test_functionExpression() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C {} void main() { final C _ = .new()(); +// ^^^^^^ +// [diag.invocationOfNonFunctionExpression] The expression doesn't evaluate to a function, so it can't be invoked. } -''', - [error(diag.invocationOfNonFunctionExpression, 40, 6)], - ); +'''); } test_functionExpression_call() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class C { C call() => this; } @@ -1025,7 +1010,7 @@ FunctionExpressionInvocation } test_functionExpression_call_argument() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class C { C call(int x) => this; } @@ -1064,7 +1049,7 @@ FunctionExpressionInvocation } test_functionExpression_call_extension() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class C {} extension CallC on C { @@ -1100,7 +1085,7 @@ FunctionExpressionInvocation } test_functionExpression_call_generic() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class C { C call(T t) => this; } @@ -1151,7 +1136,7 @@ FunctionExpressionInvocation } test_functionExpression_call_namedConstructor() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class C { C.named(); C call() => this; @@ -1186,7 +1171,7 @@ FunctionExpressionInvocation } test_functionExpression_call_nested() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class C { C(C c); C.a(); @@ -1235,8 +1220,7 @@ FunctionExpressionInvocation } test_functionExpression_nested() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C { C(C c); C.a(); @@ -1244,14 +1228,14 @@ class C { void main() { C _ = .new(.a())(); +// ^^^^^^^^^^ +// [diag.invocationOfNonFunctionExpression] The expression doesn't evaluate to a function, so it can't be invoked. } -''', - [error(diag.invocationOfNonFunctionExpression, 54, 10)], - ); +'''); } test_nested_invocation() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class C { static C member() => C(1); T x; @@ -1299,7 +1283,7 @@ DotShorthandConstructorInvocation } test_nested_property() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class C { static C get member => C(1); T x; @@ -1343,7 +1327,7 @@ DotShorthandConstructorInvocation } test_new() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class C { int x; C(this.x); @@ -1377,37 +1361,33 @@ DotShorthandConstructorInvocation } test_postfixOperator() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C {} void main() { C c = .new()++; +// ^^^ +// [diag.dotShorthandUndefinedInvocation] The static method or constructor 'new' isn't defined for the context type '_'. +// ^^ +// [diag.illegalAssignmentToNonAssignable] Illegal assignment to non-assignable expression. print(c); } -''', - [ - error(diag.dotShorthandUndefinedInvocation, 35, 3), - error(diag.illegalAssignmentToNonAssignable, 40, 2), - ], - ); +'''); } test_prefixOperator() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C {} void main() { C c = ++.new(); +// ^^^ +// [diag.dotShorthandUndefinedInvocation] The static method or constructor 'new' isn't defined for the context type '_'. +// ^ +// [diag.missingAssignableSelector] Missing selector such as '.identifier' or '[0]'. print(c); } -''', - [ - error(diag.dotShorthandUndefinedInvocation, 37, 3), - error(diag.missingAssignableSelector, 41, 1), - ], - ); +'''); } test_privateClass_otherLibrary_constConstructor() async { @@ -1420,17 +1400,16 @@ typedef Public = _Private; const Public p = _Private.named(); '''); - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'a.dart'; void main() { var x = p; x = const .named(); +// ^^^^^^^^^^^^^^ +// [diag.dotShorthandMissingContext] A dot shorthand can't be used where there is no context type. print(x); } -''', - [error(diag.dotShorthandMissingContext, 50, 14)], - ); +'''); } test_privateClass_otherLibrary_constConstructor_withUnresolvedArg() async { @@ -1443,24 +1422,22 @@ typedef Public = _Private; const Public p = _Private.named(0); '''); - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'a.dart'; void main() { var x = p; x = const .named(unknown); +// ^^^^^^^^^^^^^^^^^^^^^ +// [diag.dotShorthandMissingContext] A dot shorthand can't be used where there is no context type. +// ^^^^^^^ +// [diag.undefinedIdentifier] Undefined name 'unknown'. print(x); } -''', - [ - error(diag.dotShorthandMissingContext, 50, 21), - error(diag.undefinedIdentifier, 63, 7), - ], - ); +'''); } test_privateClass_sameLibrary_constConstructor() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class _Private { const _Private.named(); } @@ -1496,8 +1473,7 @@ DotShorthandConstructorInvocation } test_requiredParameters_missing() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C { int x; C({required this.x}); @@ -1505,124 +1481,89 @@ class C { void main() { C c = .new(); +// ^^^ +// [diag.missingRequiredArgument] The named parameter 'x' is required, but there's no corresponding argument. print(c); } -''', - [error(diag.missingRequiredArgument, 69, 3)], - ); +'''); } test_typeParameters() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C { C(); } void main() { C c = .new(); +// ^^^^^ +// [diag.wrongNumberOfTypeArgumentsDotShorthandConstructor] The dot shorthand resolves to the constructor 'C.new', and type parameters can't be applied to dot shorthand constructor invocations. print(c); } -''', - [error(diag.wrongNumberOfTypeArgumentsDotShorthandConstructor, 46, 5)], - ); +'''); } test_typeParameters_const() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C { const C(); } void main() { C c = const .new(); +// ^^^^^ +// [diag.wrongNumberOfTypeArgumentsDotShorthandConstructor] The dot shorthand resolves to the constructor 'C.new', and type parameters can't be applied to dot shorthand constructor invocations. print(c); } -''', - [error(diag.wrongNumberOfTypeArgumentsDotShorthandConstructor, 58, 5)], - ); +'''); } test_typeParameters_missingContext() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void main() { var c = const .new(); +// ^^^^^^^^^^^^^^^^^ +// [diag.dotShorthandMissingContext] A dot shorthand can't be used where there is no context type. print(c); } -''', - [error(diag.dotShorthandMissingContext, 24, 17)], - ); +'''); } test_undefinedConstructor_message() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' int f() => const .foo(); -''', - [ - error( - diag.constWithUndefinedConstructor, - 18, - 3, - messageContains: ["class 'int'", "constructor 'foo'"], - ), - ], - ); +// ^^^ +// [diag.constWithUndefinedConstructor] The class 'int' doesn't have a constant constructor 'foo'. +'''); } test_undefinedConstructor_message_equalityRhs() async { - await assertErrorsInCode( - r''' + // Make sure the error message properly refers to the `int` class. See + // https://github.com/dart-lang/sdk/issues/62352. + await resolveTestCodeWithDiagnostics(r''' bool f(int x) => x == const .foo(); -''', - [ - // Make sure the error message properly refers to the `int` class. See - // https://github.com/dart-lang/sdk/issues/62352. - error( - diag.constWithUndefinedConstructor, - 29, - 3, - messageContains: ["class 'int'", "constructor 'foo'"], - ), - ], - ); +// ^^^ +// [diag.constWithUndefinedConstructor] The class 'int' doesn't have a constant constructor 'foo'. +'''); } test_wrongNumberOfTypeArguments_message() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C {} C f() => .new(); -''', - [ - error( - diag.wrongNumberOfTypeArgumentsDotShorthandConstructor, - 25, - 5, - messageContains: ["constructor 'C.new'"], - ), - ], - ); +// ^^^^^ +// [diag.wrongNumberOfTypeArgumentsDotShorthandConstructor] The dot shorthand resolves to the constructor 'C.new', and type parameters can't be applied to dot shorthand constructor invocations. +'''); } test_wrongNumberOfTypeArguments_message_equalityRhs() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C {} bool f(C c) => c == .new(); -''', - [ - error( - diag.wrongNumberOfTypeArgumentsDotShorthandConstructor, - 36, - 5, - messageContains: ["constructor 'C.new'"], - ), - ], - ); +// ^^^^^ +// [diag.wrongNumberOfTypeArgumentsDotShorthandConstructor] The dot shorthand resolves to the constructor 'C.new', and type parameters can't be applied to dot shorthand constructor invocations. +'''); } } diff --git a/pkg/analyzer/test/src/dart/resolution/dot_shorthand_invocation_test.dart b/pkg/analyzer/test/src/dart/resolution/dot_shorthand_invocation_test.dart index 4c7fb72b628..001865b021b 100644 --- a/pkg/analyzer/test/src/dart/resolution/dot_shorthand_invocation_test.dart +++ b/pkg/analyzer/test/src/dart/resolution/dot_shorthand_invocation_test.dart @@ -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 'package:analyzer/src/diagnostic/diagnostic.dart' as diag; import 'package:test_reflective_loader/test_reflective_loader.dart'; import 'context_collection_resolution.dart'; @@ -18,8 +17,7 @@ main() { @reflectiveTest class DotShorthandInvocationResolutionTest extends PubPackageResolutionTest { test_assert_lhs() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C { final int x; const C.named(this.x); @@ -28,14 +26,14 @@ class C { class CAssert { const CAssert.regular(C ctor) : assert(const .named(1) == ctor); +// ^^^^^^^^^^^^^^^ +// [diag.dotShorthandMissingContext] A dot shorthand can't be used where there is no context type. } -''', - [error(diag.dotShorthandMissingContext, 114, 15)], - ); +'''); } test_basic() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class C { static C member() => C(1); int x; @@ -66,7 +64,7 @@ DotShorthandInvocation } test_basic_generic() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class C { static C member(U x) => C(x); T x; @@ -114,7 +112,7 @@ DotShorthandInvocation } test_basic_parameter() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class C { static C member(int x) => C(x); int x; @@ -150,7 +148,7 @@ DotShorthandInvocation } test_chain_method() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class C { static C member() => C(1); int x; @@ -182,7 +180,7 @@ DotShorthandInvocation } test_chain_property() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class C { static C member() => C(1); int x; @@ -214,7 +212,7 @@ DotShorthandInvocation } test_equality() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' class C { static C member(int x) => C(x); int x; @@ -252,7 +250,7 @@ DotShorthandInvocation } test_equality_indexExpression() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class C { int x; C(this.x); @@ -282,79 +280,74 @@ DotShorthandInvocation } test_error_context_invalid() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C { } void main() { C Function() c = .member(); +// ^^^^^^ +// [diag.dotShorthandUndefinedInvocation] The static method or constructor 'member' isn't defined for the context type 'C Function()'. print(c); } -''', - [error(diag.dotShorthandUndefinedInvocation, 47, 6)], - ); +'''); } test_error_context_none() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void main() { var c = .member(); +// ^^^^^^ +// [diag.dotShorthandUndefinedInvocation] The static method or constructor 'member' isn't defined for the context type '_'. print(c); } -''', - [error(diag.dotShorthandUndefinedInvocation, 25, 6)], - ); +'''); } test_error_notStatic() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C { C foo() => C(); } void main() { final C c = .foo(); +// ^^^ +// [diag.dotShorthandUndefinedInvocation] The static method or constructor 'foo' isn't defined for the context type 'C'. print(c); } -''', - [error(diag.dotShorthandUndefinedInvocation, 60, 3)], - ); +'''); } test_error_unresolved() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C { } void main() { C c = .member(); +// ^^^^^^ +// [diag.dotShorthandUndefinedInvocation] The static method or constructor 'member' isn't defined for the context type 'C'. print(c); } -''', - [error(diag.dotShorthandUndefinedInvocation, 36, 6)], - ); +'''); } test_error_unresolved_new() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C { C.named(); } void main() { C c = .new(); +// ^^^ +// [diag.dotShorthandUndefinedInvocation] The static method or constructor 'new' isn't defined for the context type 'C'. print(c); } -''', - [error(diag.dotShorthandUndefinedInvocation, 49, 3)], - ); +'''); } test_extensionType() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' extension type C(int integer) { static C one() => C(1); } @@ -383,22 +376,21 @@ DotShorthandInvocation } test_functionExpression() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C { static C member() => C(); } void main() { final C _ = .member()(); +// ^^^^^^^^^ +// [diag.invocationOfNonFunctionExpression] The expression doesn't evaluate to a function, so it can't be invoked. } -''', - [error(diag.invocationOfNonFunctionExpression, 69, 9)], - ); +'''); } test_functionExpression_call() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class C { static C member() => C(); C call() => this; @@ -434,7 +426,7 @@ FunctionExpressionInvocation } test_functionExpression_call_argument() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class C { static C member() => C(); C call(int a) => this; @@ -475,7 +467,7 @@ FunctionExpressionInvocation } test_functionExpression_call_extension() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class C { static C member() => C(); } @@ -514,7 +506,7 @@ FunctionExpressionInvocation } test_functionExpression_call_generic() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class C { static C member() => C(); C call(T t) => this; @@ -567,7 +559,7 @@ FunctionExpressionInvocation } test_functionExpression_call_nested() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class C { static C member(C c) => C(); static C one() => C(); @@ -618,8 +610,7 @@ FunctionExpressionInvocation } test_functionExpression_nested() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C { static C member(C c) => C(); static C one() => C(); @@ -627,14 +618,14 @@ class C { void main() { C _ = .member(.one())(); +// ^^^^^^^^^^^^^^^ +// [diag.invocationOfNonFunctionExpression] The expression doesn't evaluate to a function, so it can't be invoked. } -''', - [error(diag.invocationOfNonFunctionExpression, 92, 15)], - ); +'''); } test_futureOr() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:async'; class C { @@ -667,7 +658,7 @@ DotShorthandInvocation } test_futureOr_nested() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:async'; class C { @@ -700,7 +691,7 @@ DotShorthandInvocation } test_mixin() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class C { static C member(int x) => C(x); int x; @@ -740,7 +731,7 @@ DotShorthandInvocation } test_nested() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class C { static C member() => C(1); static C memberType(U u) => C(u); @@ -808,8 +799,7 @@ DotShorthandInvocation } test_postfixOperator() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C { static C member() => C(1); int x; @@ -818,19 +808,17 @@ class C { void main() { C c = .member()++; +// ^^^^^^ +// [diag.dotShorthandUndefinedInvocation] The static method or constructor 'member' isn't defined for the context type '_'. +// ^^ +// [diag.illegalAssignmentToNonAssignable] Illegal assignment to non-assignable expression. print(c); } -''', - [ - error(diag.dotShorthandUndefinedInvocation, 87, 6), - error(diag.illegalAssignmentToNonAssignable, 95, 2), - ], - ); +'''); } test_prefixOperator() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C { static C member() => C(1); int x; @@ -839,14 +827,13 @@ class C { void main() { C c = ++.member(); +// ^^^^^^ +// [diag.dotShorthandUndefinedInvocation] The static method or constructor 'member' isn't defined for the context type '_'. +// ^ +// [diag.missingAssignableSelector] Missing selector such as '.identifier' or '[0]'. print(c); } -''', - [ - error(diag.dotShorthandUndefinedInvocation, 89, 6), - error(diag.missingAssignableSelector, 96, 1), - ], - ); +'''); } test_privateClass_otherLibrary_constructor() async { @@ -860,21 +847,19 @@ typedef Public = _Private; final Public p = _Private(); '''); - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'a.dart'; void main() { var x = p; x = .new(); +// ^^^ +// [diag.dotShorthandUndefinedInvocation] The static method or constructor 'new' isn't defined for the context type '_Private'. x = .named(); +// ^^^^^ +// [diag.dotShorthandUndefinedInvocation] The static method or constructor 'named' isn't defined for the context type '_Private'. print(x); } -''', - [ - error(diag.dotShorthandUndefinedInvocation, 51, 3), - error(diag.dotShorthandUndefinedInvocation, 65, 5), - ], - ); +'''); } test_privateClass_otherLibrary_invocation() async { @@ -887,21 +872,20 @@ typedef Public = _Private; final Public p = _Private(); '''); - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'a.dart'; void main() { var x = p; x = .instance(); +// ^^^^^^^^ +// [diag.dotShorthandUndefinedInvocation] The static method or constructor 'instance' isn't defined for the context type '_Private'. print(x); } -''', - [error(diag.dotShorthandUndefinedInvocation, 51, 8)], - ); +'''); } test_privateClass_sameLibrary_constructor() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class _Private { _Private(); } @@ -936,7 +920,7 @@ DotShorthandConstructorInvocation } test_privateClass_sameLibrary_invocation() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class _Private { static _Private instance() => _Private(); } @@ -979,17 +963,16 @@ typedef Public = _Private; final Public p = _Private.one; '''); - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'a.dart'; void main() { var x = p; x = .a(); +// ^ +// [diag.dotShorthandUndefinedInvocation] The static method or constructor 'a' isn't defined for the context type '_Private'. print(x); } -''', - [error(diag.dotShorthandUndefinedInvocation, 51, 1)], - ); +'''); } test_privateEnum_otherLibrary_invocation() async { @@ -1003,21 +986,20 @@ typedef Public = _Private; final Public p = _Private.one; '''); - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'a.dart'; void main() { var x = p; x = .instance(); +// ^^^^^^^^ +// [diag.dotShorthandUndefinedInvocation] The static method or constructor 'instance' isn't defined for the context type '_Private'. print(x); } -''', - [error(diag.dotShorthandUndefinedInvocation, 51, 8)], - ); +'''); } test_privateEnum_sameLibrary_constructor() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' enum _Private { one; factory _Private.a() => one; @@ -1053,7 +1035,7 @@ DotShorthandConstructorInvocation } test_privateEnum_sameLibrary_invocation() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' enum _Private { one; static _Private instance() => one; @@ -1096,21 +1078,19 @@ typedef Public = _Private; final Public p = _Private(1); '''); - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'a.dart'; void main() { var x = p; x = .new(1); +// ^^^ +// [diag.dotShorthandUndefinedInvocation] The static method or constructor 'new' isn't defined for the context type '_Private'. x = .named(1); +// ^^^^^ +// [diag.dotShorthandUndefinedInvocation] The static method or constructor 'named' isn't defined for the context type '_Private'. print(x); } -''', - [ - error(diag.dotShorthandUndefinedInvocation, 51, 3), - error(diag.dotShorthandUndefinedInvocation, 66, 5), - ], - ); +'''); } test_privateExtensionType_otherLibrary_invocation() async { @@ -1123,21 +1103,20 @@ typedef Public = _Private; final Public p = _Private(1); '''); - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'a.dart'; void main() { var x = p; x = .instance(); +// ^^^^^^^^ +// [diag.dotShorthandUndefinedInvocation] The static method or constructor 'instance' isn't defined for the context type '_Private'. print(x); } -''', - [error(diag.dotShorthandUndefinedInvocation, 51, 8)], - ); +'''); } test_privateExtensionType_sameLibrary_constructor() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' extension type _Private(int i) {} typedef Public = _Private; @@ -1175,7 +1154,7 @@ DotShorthandConstructorInvocation } test_privateExtensionType_sameLibrary_invocation() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' extension type _Private(int it) { static _Private instance() => _Private(0); } @@ -1218,21 +1197,20 @@ typedef Public = _Private; final Public p = C(); '''); - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'a.dart'; void main() { var x = p; x = .instance(); +// ^^^^^^^^ +// [diag.dotShorthandUndefinedInvocation] The static method or constructor 'instance' isn't defined for the context type '_Private'. print(x); } -''', - [error(diag.dotShorthandUndefinedInvocation, 51, 8)], - ); +'''); } test_privateMixin_sameLibrary_invocation() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' mixin _Private { static _Private instance() => C(); } @@ -1266,8 +1244,7 @@ DotShorthandInvocation } test_requiredParameters_missing() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C { static C member({required int x}) => C(x); int x; @@ -1276,15 +1253,15 @@ class C { void main() { C c = .member(); +// ^^^^^^ +// [diag.missingRequiredArgument] The named parameter 'x' is required, but there's no corresponding argument. print(c); } -''', - [error(diag.missingRequiredArgument, 103, 6)], - ); +'''); } test_typeParameters_inference() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class C { static C foo(X x) => new C(); C cast() => new C(); @@ -1318,8 +1295,7 @@ DotShorthandInvocation } test_typeParameters_notAssignable() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C { static C member() => C(1); @@ -1329,49 +1305,27 @@ class C { void main() { C c = .member(); +// ^^^^^^^^^ +// [diag.invalidAssignment] A value of type 'C' can't be assigned to a variable of type 'C'. print(c); } -''', - [error(diag.invalidAssignment, 105, 9)], - ); +'''); } test_undefinedInvocation_message() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' int f() => .foo(); -''', - [ - error( - diag.dotShorthandUndefinedInvocation, - 12, - 3, - messageContains: [ - "context type 'int'", - "static method or constructor 'foo'", - ], - ), - ], - ); +// ^^^ +// [diag.dotShorthandUndefinedInvocation] The static method or constructor 'foo' isn't defined for the context type 'int'. +'''); } test_undefinedInvocation_message_equalityRhs() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' bool f(int x) => x == .foo(); -''', - [ - error( - diag.dotShorthandUndefinedInvocation, - 23, - 3, - messageContains: [ - "context type 'int'", - "static method or constructor 'foo'", - ], - ), - ], - ); +// ^^^ +// [diag.dotShorthandUndefinedInvocation] The static method or constructor 'foo' isn't defined for the context type 'int'. +'''); } } diff --git a/pkg/analyzer/test/src/dart/resolution/dot_shorthand_property_access_test.dart b/pkg/analyzer/test/src/dart/resolution/dot_shorthand_property_access_test.dart index 0c7f1de896b..f81ac550e2c 100644 --- a/pkg/analyzer/test/src/dart/resolution/dot_shorthand_property_access_test.dart +++ b/pkg/analyzer/test/src/dart/resolution/dot_shorthand_property_access_test.dart @@ -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 'package:analyzer/src/diagnostic/diagnostic.dart' as diag; import 'package:test_reflective_loader/test_reflective_loader.dart'; import 'context_collection_resolution.dart'; @@ -19,7 +18,7 @@ main() { class DotShorthandPropertyAccessResolutionTest extends PubPackageResolutionTest { test_chain_method() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class C { static C get member => C(1); int x; @@ -47,7 +46,7 @@ DotShorthandPropertyAccess } test_chain_property() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class C { static C get member => C(1); int x; @@ -75,7 +74,7 @@ DotShorthandPropertyAccess } test_class_basic() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' class C { static C get member => C(1); int x; @@ -102,7 +101,7 @@ DotShorthandPropertyAccess } test_const_assert_class() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class Integer { static const Integer one = const Integer._(1); final int integer; @@ -130,7 +129,7 @@ DotShorthandPropertyAccess } test_const_assert_enum() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' enum Color { red, green, blue } class CAssert { @@ -153,7 +152,7 @@ DotShorthandPropertyAccess } test_const_class() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' class C { static const C member = const C._(1); final int x; @@ -181,7 +180,7 @@ DotShorthandPropertyAccess } test_const_enum() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' enum Color { red, green, blue } void main() { @@ -204,7 +203,7 @@ DotShorthandPropertyAccess } test_const_extensionType() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' extension type C(int x) { static const C member = const C._(1); const C._(this.x); @@ -230,7 +229,7 @@ DotShorthandPropertyAccess } test_enum_basic() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' enum C { red } void main() { @@ -253,7 +252,7 @@ DotShorthandPropertyAccess } test_equality() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' class C { static C get member => C(1); int x; @@ -282,7 +281,7 @@ DotShorthandPropertyAccess } test_equality_indexExpression() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class C { int x; C(this.x); @@ -308,7 +307,7 @@ DotShorthandPropertyAccess } test_equality_nullAssert() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class C { int x; C(this.x); @@ -334,7 +333,7 @@ DotShorthandPropertyAccess } test_equality_nullAssert_chain() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class C { int x; C(this.x); @@ -361,7 +360,7 @@ DotShorthandPropertyAccess } test_equality_pattern() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' enum Color { red, blue } void main() { @@ -384,63 +383,59 @@ DotShorthandPropertyAccess } test_error_context_invalid() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class C { } void main() { C Function() c = .member; +// ^^^^^^^ +// [diag.dotShorthandMissingContext] A dot shorthand can't be used where there is no context type. print(c); } -''', - [error(diag.dotShorthandMissingContext, 46, 7)], - ); +'''); } test_error_context_none() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' void main() { var c = .member; +// ^^^^^^^ +// [diag.dotShorthandMissingContext] A dot shorthand can't be used where there is no context type. print(c); } -''', - [error(diag.dotShorthandMissingContext, 24, 7)], - ); +'''); } test_error_unresolved() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class C { } void main() { C c = .getter; +// ^^^^^^ +// [diag.dotShorthandUndefinedGetter] The static getter 'getter' isn't defined for the context type 'C'. print(c); } -''', - [error(diag.dotShorthandUndefinedGetter, 36, 6)], - ); +'''); } test_error_unresolved_new() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class C { C.named(); } void main() { C c = .new; +// ^^^ +// [diag.dotShorthandUndefinedGetter] The static getter 'new' isn't defined for the context type 'C'. print(c); } -''', - [error(diag.dotShorthandUndefinedGetter, 49, 3)], - ); +'''); } test_extensionType() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' extension type C(int integer) { static C get one => C(1); } @@ -465,7 +460,7 @@ DotShorthandPropertyAccess } test_functionExpression_call_argument() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class C { static final C field = C(); C call(int a) => this; @@ -502,7 +497,7 @@ FunctionExpressionInvocation } test_functionExpression_call_extension_field() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class C { static final C field = C(); } @@ -537,7 +532,7 @@ FunctionExpressionInvocation } test_functionExpression_call_extension_getter() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class C { static C get getter => C(); } @@ -572,7 +567,7 @@ FunctionExpressionInvocation } test_functionExpression_call_field() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class C { static final C field = C(); C call() => this; @@ -604,7 +599,7 @@ FunctionExpressionInvocation } test_functionExpression_call_generic() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class C { static final C field = C(); C call(T t) => this; @@ -653,7 +648,7 @@ FunctionExpressionInvocation } test_functionExpression_call_getter() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class C { static C get getter => C(); C call() => this; @@ -685,37 +680,35 @@ FunctionExpressionInvocation } test_functionExpression_field() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C { static final C field = C(); } void main() { final C _ = .field(); +// ^^^^^^ +// [diag.invocationOfNonFunctionExpression] The expression doesn't evaluate to a function, so it can't be invoked. } -''', - [error(diag.invocationOfNonFunctionExpression, 71, 6)], - ); +'''); } test_functionExpression_getter() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C { static C get getter => C(); } void main() { final C _ = .getter(); +// ^^^^^^^ +// [diag.invocationOfNonFunctionExpression] The expression doesn't evaluate to a function, so it can't be invoked. } -''', - [error(diag.invocationOfNonFunctionExpression, 71, 7)], - ); +'''); } test_functionReference() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class C { static String foo() => "C<$X>"; @@ -748,7 +741,7 @@ DotShorthandPropertyAccess } test_futureOr() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' import 'dart:async'; enum C { red } @@ -773,7 +766,7 @@ DotShorthandPropertyAccess } test_futureOr_nested() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' import 'dart:async'; enum C { red } @@ -798,7 +791,7 @@ DotShorthandPropertyAccess } test_mixin() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class C { int x; C(this.x); @@ -833,8 +826,7 @@ DotShorthandPropertyAccess } test_postfixOperator() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C { static C get member => C(1); int x; @@ -843,19 +835,17 @@ class C { void main() { C c = .member++; +// ^^^^^^^ +// [diag.dotShorthandMissingContext] A dot shorthand can't be used where there is no context type. +// ^^ +// [diag.illegalAssignmentToNonAssignable] Illegal assignment to non-assignable expression. print(c); } -''', - [ - error(diag.dotShorthandMissingContext, 88, 7), - error(diag.illegalAssignmentToNonAssignable, 95, 2), - ], - ); +'''); } test_prefixOperator() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C { static C get member => C(1); int x; @@ -864,14 +854,13 @@ class C { void main() { C c = ++.member; +// ^^^^^^^ +// [diag.dotShorthandMissingContext] A dot shorthand can't be used where there is no context type. +// ^^^^^^ +// [diag.missingAssignableSelector] Missing selector such as '.identifier' or '[0]'. print(c); } -''', - [ - error(diag.dotShorthandMissingContext, 90, 7), - error(diag.missingAssignableSelector, 91, 6), - ], - ); +'''); } test_privateClass_otherLibrary() async { @@ -884,21 +873,20 @@ typedef Public = _Private; final Public p = _Private(); '''); - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'a.dart'; void main() { var x = p; x = .getter; +// ^^^^^^^ +// [diag.dotShorthandMissingContext] A dot shorthand can't be used where there is no context type. print(x); } -''', - [error(diag.dotShorthandMissingContext, 50, 7)], - ); +'''); } test_privateClass_sameLibrary() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class _Private { static _Private get getter => _Private(); } @@ -934,21 +922,20 @@ typedef Public = _Private; final Public p = _Private.one; '''); - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'a.dart'; void main() { var x = p; x = .two; +// ^^^^ +// [diag.dotShorthandMissingContext] A dot shorthand can't be used where there is no context type. print(x); } -''', - [error(diag.dotShorthandMissingContext, 50, 4)], - ); +'''); } test_privateEnum_sameLibrary() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' enum _Private { one, two } typedef Public = _Private; @@ -984,21 +971,20 @@ typedef Public = _Private; final Public p = _Private(1); '''); - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'a.dart'; void main() { var x = p; x = .getter; +// ^^^^^^^ +// [diag.dotShorthandMissingContext] A dot shorthand can't be used where there is no context type. print(x); } -''', - [error(diag.dotShorthandMissingContext, 50, 7)], - ); +'''); } test_privateExtensionType_sameLibrary() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' extension type _Private(int i) { static _Private get getter => _Private(0); } @@ -1037,21 +1023,20 @@ typedef Public = _Private; final Public p = C(); '''); - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'a.dart'; void main() { var x = p; x = .getter; +// ^^^^^^^ +// [diag.dotShorthandMissingContext] A dot shorthand can't be used where there is no context type. print(x); } -''', - [error(diag.dotShorthandMissingContext, 50, 7)], - ); +'''); } test_privateMixin_sameLibrary() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' mixin _Private { static _Private get getter => C(); } @@ -1081,7 +1066,7 @@ DotShorthandPropertyAccess } test_tearOff_constructor() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class C1 { C1.id(); @@ -1110,18 +1095,17 @@ DotShorthandPropertyAccess } test_tearOff_constructor_abstract() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' Function fn() { return .new; +// ^^^^ +// [diag.tearoffOfGenerativeConstructorOfAbstractClass] A generative constructor of an abstract class can't be torn off. } -''', - [error(diag.tearoffOfGenerativeConstructorOfAbstractClass, 25, 4)], - ); +'''); } test_tearOff_constructor_generic() async { - await assertNoErrorsInCode(r''' + await resolveTestCodeWithDiagnostics(r''' class C { T t; C(this.t); @@ -1154,7 +1138,7 @@ DotShorthandPropertyAccess } test_tearOff_constructor_new() async { - await assertNoErrorsInCode(''' + await resolveTestCodeWithDiagnostics(''' void main() { Object o = .new; print(o); @@ -1175,34 +1159,18 @@ DotShorthandPropertyAccess } test_undefinedGetter_message() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' int f() => .foo; -''', - [ - error( - diag.dotShorthandUndefinedGetter, - 12, - 3, - messageContains: ["static getter 'foo'", "context type 'int'"], - ), - ], - ); +// ^^^ +// [diag.dotShorthandUndefinedGetter] The static getter 'foo' isn't defined for the context type 'int'. +'''); } test_undefinedGetter_message_equalityRhs() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' bool f(int x) => x == .foo; -''', - [ - error( - diag.dotShorthandUndefinedGetter, - 23, - 3, - messageContains: ["static getter 'foo'", "context type 'int'"], - ), - ], - ); +// ^^^ +// [diag.dotShorthandUndefinedGetter] The static getter 'foo' isn't defined for the context type 'int'. +'''); } }