Skip to content

Commit c2743b4

Browse files
scheglovpull[bot]
authored andcommitted
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 <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent ef68d43 commit c2743b4

File tree

4 files changed

+68
-20
lines changed

4 files changed

+68
-20
lines changed

pkg/analyzer/test/generated/test_support.dart

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,45 @@ class ExpectedContextMessage {
2828
/// The message text for the error.
2929
final String? text;
3030

31-
ExpectedContextMessage(this.file, this.offset, this.length, {this.text});
31+
/// A list of patterns that should be contained in the message test; empty if
32+
/// the message contents should not be checked.
33+
final List<Pattern> textContains;
34+
35+
ExpectedContextMessage(
36+
this.file,
37+
this.offset,
38+
this.length, {
39+
this.text,
40+
this.textContains = const [],
41+
});
3242

3343
/// Return `true` if the [message] matches this description of what it's
3444
/// expected to be.
3545
bool matches(DiagnosticMessage message) {
36-
return message.filePath == file.path &&
37-
message.offset == offset &&
38-
message.length == length &&
39-
(text == null || message.messageText(includeUrl: true) == text);
46+
if (message.filePath != file.path) {
47+
return false;
48+
}
49+
50+
if (message.offset != offset) {
51+
return false;
52+
}
53+
54+
if (message.length != length) {
55+
return false;
56+
}
57+
58+
var messageText = message.messageText(includeUrl: true);
59+
if (text != null && messageText != text) {
60+
return false;
61+
}
62+
63+
for (var pattern in textContains) {
64+
if (!messageText.contains(pattern)) {
65+
return false;
66+
}
67+
}
68+
69+
return true;
4070
}
4171
}
4272

pkg/analyzer/test/src/dart/resolution/macro_test.dart

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'package:analyzer/src/error/codes.dart';
66
import 'package:analyzer/src/summary2/macro_application.dart';
77
import 'package:test_reflective_loader/test_reflective_loader.dart';
88

9+
import '../../../generated/test_support.dart';
910
import '../../summary/macros_environment.dart';
1011
import 'context_collection_resolution.dart';
1112
import 'resolution.dart';
@@ -93,7 +94,11 @@ class A {}
9394
'Macro application failed due to a bug in the macro.',
9495
],
9596
contextMessages: [
96-
message(testFile, 18, 10),
97+
ExpectedContextMessage(testFile, 18, 10, textContains: [
98+
'package:test/a.dart',
99+
'MyMacro',
100+
'unresolved',
101+
]),
97102
],
98103
),
99104
]);
@@ -870,7 +875,11 @@ class A {}
870875
'Macro application failed due to a bug in the macro.'
871876
],
872877
contextMessages: [
873-
message(testFile, 18, 10),
878+
ExpectedContextMessage(testFile, 18, 10, textContains: [
879+
'package:test/a.dart',
880+
'12345',
881+
'MyMacro',
882+
]),
874883
],
875884
),
876885
]);

pkg/analyzer/test/src/summary/element_text.dart

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ String getLibraryText({
4343

4444
class ElementTextConfiguration {
4545
bool Function(Object) filter;
46-
void Function(String message)? macroDiagnosticMessageValidator;
46+
List<Pattern>? macroDiagnosticMessagePatterns;
4747
bool withAllSupertypes = false;
4848
bool withAugmentedWithoutAugmentation = false;
4949
bool withCodeRanges = false;
@@ -789,9 +789,15 @@ class _ElementWriter {
789789

790790
void writeMessage(MacroDiagnosticMessage object) {
791791
// Write the message.
792-
final validator = configuration.macroDiagnosticMessageValidator;
793-
if (validator != null) {
794-
validator(object.message);
792+
if (configuration.macroDiagnosticMessagePatterns case var patterns?) {
793+
_sink.writelnWithIndent('contains');
794+
_sink.withIndent(() {
795+
for (var pattern in patterns) {
796+
if (object.message.contains(pattern)) {
797+
_sink.writelnWithIndent(pattern);
798+
}
799+
}
800+
});
795801
} else {
796802
final message = object.message;
797803
const stackTraceText = '#0';

pkg/analyzer/test/src/summary/macro_test.dart

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5387,15 +5387,12 @@ class A {}
53875387
configuration
53885388
..withConstructors = false
53895389
..withMetadata = false
5390-
..macroDiagnosticMessageValidator = (message) {
5391-
if (message.contains('#0')) {
5392-
// It's the context: stack trace with the underlying issue.
5393-
expect(message, contains('unresolved'));
5394-
} else {
5395-
// It's the main message.
5396-
expect(message, contains('failed due to a bug in the macro'));
5397-
}
5398-
};
5390+
..macroDiagnosticMessagePatterns = [
5391+
'Macro application failed due to a bug in the macro.',
5392+
'package:test/a.dart',
5393+
'MyMacro',
5394+
'unresolved',
5395+
];
53995396
checkElementText(library, r'''
54005397
library
54015398
imports
@@ -5406,10 +5403,16 @@ library
54065403
macroDiagnostics
54075404
MacroDiagnostic
54085405
message: MacroDiagnosticMessage
5406+
contains
5407+
Macro application failed due to a bug in the macro.
54095408
target: ApplicationMacroDiagnosticTarget
54105409
annotationIndex: 0
54115410
contextMessages
54125411
MacroDiagnosticMessage
5412+
contains
5413+
package:test/a.dart
5414+
MyMacro
5415+
unresolved
54135416
target: ApplicationMacroDiagnosticTarget
54145417
annotationIndex: 0
54155418
severity: error

0 commit comments

Comments
 (0)