diff --git a/pkg/analyzer/test/src/diagnostics/could_not_infer_test.dart b/pkg/analyzer/test/src/diagnostics/could_not_infer_test.dart index 49e0c2340d4..bf62399e7ce 100644 --- a/pkg/analyzer/test/src/diagnostics/could_not_infer_test.dart +++ b/pkg/analyzer/test/src/diagnostics/could_not_infer_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 '../dart/resolution/context_collection_resolution.dart'; +import '../dart/resolution/node_text_expectations.dart'; main() { defineReflectiveSuite(() { defineReflectiveTests(CouldNotInferTest); + defineReflectiveTests(UpdateNodeTextExpectations); }); } @@ -18,10 +19,7 @@ main() { @reflectiveTest class CouldNotInferTest extends PubPackageResolutionTest { test_constructors_inferenceFBounded() async { - // Skipped migration to resolveTestCodeWithDiagnostics due to multi-line - // error messages for couldNotInfer which are not supported well by the tool. - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class C {} class P, U extends C> { @@ -29,32 +27,25 @@ class P, U extends C> { U u; P(this.t, this.u); P._(); +//^^^ +// [diag.notInitializedNonNullableInstanceFieldConstructor] Non-nullable instance field 't' must be initialized. +// [diag.notInitializedNonNullableInstanceFieldConstructor] Non-nullable instance field 'u' must be initialized. P get reversed => new P(u, t); } main() { P._(); +//^ +// [context 1] The raw type was instantiated as 'P, C>', and is not regular-bounded. +// [context 2] The raw type was instantiated as 'P, C>', and is not regular-bounded. +//^^^ +// [diag.couldNotInfer] Couldn't infer type parameter 'T'.\n\nTried to infer 'C' for 'T' which doesn't work:\n Type parameter 'T' is declared to extend 'C' producing 'C>'.\n\nConsider passing explicit type argument(s) to the generic. +// [diag.couldNotInfer] Couldn't infer type parameter 'U'.\n\nTried to infer 'C' for 'U' which doesn't work:\n Type parameter 'U' is declared to extend 'C' producing 'C>'.\n\nConsider passing explicit type argument(s) to the generic. +//^ +// [diag.typeArgumentNotMatchingBounds][context 1] 'C' doesn't conform to the bound 'C>' of the type parameter 'T'. +// [diag.typeArgumentNotMatchingBounds][context 2] 'C' doesn't conform to the bound 'C>' of the type parameter 'U'. } -''', - [ - error(diag.notInitializedNonNullableInstanceFieldConstructor, 94, 3), - error(diag.notInitializedNonNullableInstanceFieldConstructor, 94, 3), - error(diag.couldNotInfer, 154, 3), - error(diag.couldNotInfer, 154, 3), - error( - diag.typeArgumentNotMatchingBounds, - 154, - 1, - contextMessages: [message(testFile, 154, 1)], - ), - error( - diag.typeArgumentNotMatchingBounds, - 154, - 1, - contextMessages: [message(testFile, 154, 1)], - ), - ], - ); +'''); } test_constructors_inferFromArguments_argumentNotAssignable() async { @@ -156,38 +147,32 @@ void main() { } test_functionType_instantiatedToBounds() async { - // Skipped migration to resolveTestCodeWithDiagnostics due to multi-line - // error messages for couldNotInfer which are not supported well by the tool. - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A> {} void foo>() {} void f() { foo(); +//^^^ +// [diag.couldNotInfer] Couldn't infer type parameter 'Y'.\n'A' doesn't conform to the bound 'A>', instantiated from 'A' using type arguments [A, A]. } -''', - [error(diag.couldNotInfer, 85, 3)], - ); +'''); } test_functionType_optOutOfGenericMetadata() async { newFile('$testPackageLibPath/a.dart', ''' void f() {} '''); - // Skipped migration to resolveTestCodeWithDiagnostics due to multi-line - // error messages for couldNotInfer which are not supported well by the tool. - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' // @dart=2.12 import 'a.dart'; main() { [f]; +//^^^ +// [diag.couldNotInfer] Couldn't infer type parameter 'E'. Inferred candidate type void Function() has type parameters [X], but a function with type parameters cannot be used as a type argument. } -''', - [error(diag.couldNotInfer, 42, 3)], - ); +'''); } test_functionType_parameterIsBound_returnIsBound() async { @@ -286,21 +271,16 @@ void main() { } test_functionType_parametersAreSubtypes_returnIsOne() async { - // Skipped migration to resolveTestCodeWithDiagnostics due to multi-line - // error messages for couldNotInfer which are not supported well by the tool. - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' external T f(T a, T b); void g(int cb(int a, double b)) {} void main() { g(f); +// ^ +// [diag.couldNotInfer] Couldn't infer type parameter 'T'.\n\nTried to infer 'num' for 'T' which doesn't work:\n Function type declared as 'T Function(T, T)'\n used where 'int Function(int, double)' is required.\n\nConsider passing explicit type argument(s) to the generic. +// [diag.argumentTypeNotAssignable] The argument type 'num Function(num, num)' can't be assigned to the parameter type 'int Function(int, double)'. } -''', - [ - error(diag.couldNotInfer, 92, 1), - error(diag.argumentTypeNotAssignable, 92, 1), - ], - ); +'''); } test_genericMethods_correctlyRecognizeGenericUpperBound() async { @@ -318,10 +298,7 @@ main() { } test_instanceCreation_viaTypeAlias_notWellBounded() async { - // Skipped migration to resolveTestCodeWithDiagnostics due to multi-line - // error messages for couldNotInfer which are not supported well by the tool. - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class C { C(); factory C.foo() => C(); @@ -332,16 +309,16 @@ typedef A>> = C; void f() { A(); // Error. +//^ +// [diag.couldNotInfer] Couldn't infer type parameter 'X'.\n\nTried to infer 'C Function(C)' for 'X' which doesn't work:\n Type parameter 'X' is declared to extend 'C Function(C)' producing 'C Function(C)> Function(C Function(C)>)'.\n\nConsider passing explicit type argument(s) to the generic. A.foo(); // Error. +//^^^^^ +// [diag.couldNotInfer] Couldn't infer type parameter 'X'.\n\nTried to infer 'C Function(C)' for 'X' which doesn't work:\n Type parameter 'X' is declared to extend 'C Function(C)' producing 'C Function(C)> Function(C Function(C)>)'.\n\nConsider passing explicit type argument(s) to the generic. A.bar(); // Error. +//^^^^^ +// [diag.couldNotInfer] Couldn't infer type parameter 'X'.\n\nTried to infer 'C Function(C)' for 'X' which doesn't work:\n Type parameter 'X' is declared to extend 'C Function(C)' producing 'C Function(C)> Function(C Function(C)>)'.\n\nConsider passing explicit type argument(s) to the generic. } -''', - [ - error(diag.couldNotInfer, 152, 1), - error(diag.couldNotInfer, 169, 5), - error(diag.couldNotInfer, 190, 5), - ], - ); +'''); } test_method() async { diff --git a/pkg/analyzer_testing/lib/src/expected_diagnostics.dart b/pkg/analyzer_testing/lib/src/expected_diagnostics.dart index 62e57f3295a..43ccf843753 100644 --- a/pkg/analyzer_testing/lib/src/expected_diagnostics.dart +++ b/pkg/analyzer_testing/lib/src/expected_diagnostics.dart @@ -2,6 +2,8 @@ // 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 'dart:convert'; + import 'package:analyzer/diagnostic/diagnostic.dart'; import 'package:analyzer/file_system/file_system.dart'; import 'package:analyzer/source/line_info.dart'; @@ -323,7 +325,9 @@ final class _ExpectedDiagnosticsUpdater { static String _messageText(DiagnosticMessage message) { var text = message.messageText(includeUrl: false); - return _toPosixPaths(text).trim(); + text = _toPosixPaths(text).trim(); + text = LineSplitter.split(text).join(r'\n'); + return text; } static String _toPosixPaths(String message) {