Skip to content

Commit

Permalink
Macro. Add 'textContains' to ExpectedContextMessage.
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
scheglov authored and pull[bot] committed Feb 22, 2024
1 parent ef68d43 commit c2743b4
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 20 deletions.
40 changes: 35 additions & 5 deletions pkg/analyzer/test/generated/test_support.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
13 changes: 11 additions & 2 deletions pkg/analyzer/test/src/dart/resolution/macro_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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',
]),
],
),
]);
Expand Down Expand Up @@ -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',
]),
],
),
]);
Expand Down
14 changes: 10 additions & 4 deletions pkg/analyzer/test/src/summary/element_text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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';
Expand Down
21 changes: 12 additions & 9 deletions pkg/analyzer/test/src/summary/macro_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit c2743b4

Please sign in to comment.