diff --git a/pkg/analyzer/test/generated/test_support.dart b/pkg/analyzer/test/generated/test_support.dart index ba69af035290..6461e3538c2e 100644 --- a/pkg/analyzer/test/generated/test_support.dart +++ b/pkg/analyzer/test/generated/test_support.dart @@ -28,15 +28,45 @@ class ExpectedContextMessage { /// The message text for the error. final String? text; - ExpectedContextMessage(this.file, this.offset, this.length, {this.text}); + /// A list of patterns that should be contained in the message test; empty if + /// the message contents should not be checked. + final List textContains; + + ExpectedContextMessage( + this.file, + this.offset, + this.length, { + this.text, + this.textContains = const [], + }); /// Return `true` if the [message] matches this description of what it's /// expected to be. bool matches(DiagnosticMessage message) { - return message.filePath == file.path && - message.offset == offset && - message.length == length && - (text == null || message.messageText(includeUrl: true) == text); + if (message.filePath != file.path) { + return false; + } + + if (message.offset != offset) { + return false; + } + + if (message.length != length) { + return false; + } + + var messageText = message.messageText(includeUrl: true); + if (text != null && messageText != text) { + return false; + } + + for (var pattern in textContains) { + if (!messageText.contains(pattern)) { + return false; + } + } + + return true; } } diff --git a/pkg/analyzer/test/src/dart/resolution/macro_test.dart b/pkg/analyzer/test/src/dart/resolution/macro_test.dart index 9aaf78dd1b6d..ba8917d3e6bd 100644 --- a/pkg/analyzer/test/src/dart/resolution/macro_test.dart +++ b/pkg/analyzer/test/src/dart/resolution/macro_test.dart @@ -6,6 +6,7 @@ import 'package:analyzer/src/error/codes.dart'; import 'package:analyzer/src/summary2/macro_application.dart'; import 'package:test_reflective_loader/test_reflective_loader.dart'; +import '../../../generated/test_support.dart'; import '../../summary/macros_environment.dart'; import 'context_collection_resolution.dart'; import 'resolution.dart'; @@ -93,7 +94,11 @@ class A {} 'Macro application failed due to a bug in the macro.', ], contextMessages: [ - message(testFile, 18, 10), + ExpectedContextMessage(testFile, 18, 10, textContains: [ + 'package:test/a.dart', + 'MyMacro', + 'unresolved', + ]), ], ), ]); @@ -870,7 +875,11 @@ class A {} 'Macro application failed due to a bug in the macro.' ], contextMessages: [ - message(testFile, 18, 10), + ExpectedContextMessage(testFile, 18, 10, textContains: [ + 'package:test/a.dart', + '12345', + 'MyMacro', + ]), ], ), ]); diff --git a/pkg/analyzer/test/src/summary/element_text.dart b/pkg/analyzer/test/src/summary/element_text.dart index 73c92b2aae50..9e9c6d8bda4e 100644 --- a/pkg/analyzer/test/src/summary/element_text.dart +++ b/pkg/analyzer/test/src/summary/element_text.dart @@ -43,7 +43,7 @@ String getLibraryText({ class ElementTextConfiguration { bool Function(Object) filter; - void Function(String message)? macroDiagnosticMessageValidator; + List? macroDiagnosticMessagePatterns; bool withAllSupertypes = false; bool withAugmentedWithoutAugmentation = false; bool withCodeRanges = false; @@ -789,9 +789,15 @@ class _ElementWriter { void writeMessage(MacroDiagnosticMessage object) { // Write the message. - final validator = configuration.macroDiagnosticMessageValidator; - if (validator != null) { - validator(object.message); + if (configuration.macroDiagnosticMessagePatterns case var patterns?) { + _sink.writelnWithIndent('contains'); + _sink.withIndent(() { + for (var pattern in patterns) { + if (object.message.contains(pattern)) { + _sink.writelnWithIndent(pattern); + } + } + }); } else { final message = object.message; const stackTraceText = '#0'; diff --git a/pkg/analyzer/test/src/summary/macro_test.dart b/pkg/analyzer/test/src/summary/macro_test.dart index 90b2200ccb97..624d8fc0c0a1 100644 --- a/pkg/analyzer/test/src/summary/macro_test.dart +++ b/pkg/analyzer/test/src/summary/macro_test.dart @@ -5387,15 +5387,12 @@ class A {} configuration ..withConstructors = false ..withMetadata = false - ..macroDiagnosticMessageValidator = (message) { - if (message.contains('#0')) { - // It's the context: stack trace with the underlying issue. - expect(message, contains('unresolved')); - } else { - // It's the main message. - expect(message, contains('failed due to a bug in the macro')); - } - }; + ..macroDiagnosticMessagePatterns = [ + 'Macro application failed due to a bug in the macro.', + 'package:test/a.dart', + 'MyMacro', + 'unresolved', + ]; checkElementText(library, r''' library imports @@ -5406,10 +5403,16 @@ library macroDiagnostics MacroDiagnostic message: MacroDiagnosticMessage + contains + Macro application failed due to a bug in the macro. target: ApplicationMacroDiagnosticTarget annotationIndex: 0 contextMessages MacroDiagnosticMessage + contains + package:test/a.dart + MyMacro + unresolved target: ApplicationMacroDiagnosticTarget annotationIndex: 0 severity: error