Macro. Add 'textContains' to ExpectedContextMessage.

Similarly, use patterns for element text printer.
This makes the test a little more declarative.

Change-Id: I179973c3e226b19409665bf4f738e6ba36b42467
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/353803
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
This commit is contained in:
Konstantin Shcheglov 2024-02-22 20:43:11 +00:00 committed by Commit Queue
parent bd38fbada4
commit 3699d8b8d1
4 changed files with 68 additions and 20 deletions

View file

@ -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<Pattern> 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;
}
}

View file

@ -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',
]),
],
),
]);

View file

@ -43,7 +43,7 @@ String getLibraryText({
class ElementTextConfiguration {
bool Function(Object) filter;
void Function(String message)? macroDiagnosticMessageValidator;
List<Pattern>? 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';

View file

@ -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