Skip to content

Commit ffd6c48

Browse files
FMorschelCommit Queue
authored andcommitted
[analyzer_testing] Duplicates ExpectedContextMessage to start migrating
This is a first step towards migrating and merging tests helper classes to one central location. In a following CL, the old `ExpectedError` and `ExpectedContextMessage` will be removed and all clients will be migrated to the version under this package. Bug: #61557 Change-Id: I7fb8a83cbd659192a22848ace99ebafed5dc2b88 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/451542 Commit-Queue: Paul Berry <[email protected]> Auto-Submit: Felipe Morschel <[email protected]> Reviewed-by: Paul Berry <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent 421a968 commit ffd6c48

File tree

3 files changed

+145
-25
lines changed

3 files changed

+145
-25
lines changed

pkg/analyzer_testing/api.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package:analyzer_testing/analysis_rule/analysis_rule.dart:
2-
error (function: ExpectedDiagnostic Function(DiagnosticCode, int, int, {Pattern? messageContains}))
2+
contextMessage (function: ExpectedContextMessage Function(File, int, int, {String? text, List<Pattern> textContains}))
3+
error (function: ExpectedDiagnostic Function(DiagnosticCode, int, int, {List<ExpectedContextMessage>? contextMessages, Pattern? correctionContains, Pattern? messageContains}))
34
AnalysisRuleTest (class extends PubPackageResolutionTest):
45
new (constructor: AnalysisRuleTest Function())
56
analysisRule (getter: String)
67
assertNoPubspecDiagnostics (method: Future<void> Function(String))
78
assertPubspecDiagnostics (method: Future<void> Function(String, List<ExpectedDiagnostic>))
89
correctionMessage (method: String Function(List<Diagnostic>))
9-
lint (method: ExpectedDiagnostic Function(int, int, {Pattern? correctionContains, Pattern? messageContains, String? name}))
10+
lint (method: ExpectedDiagnostic Function(int, int, {List<ExpectedContextMessage>? contextMessages, Pattern? correctionContains, Pattern? messageContains, String? name}))
1011
setUp (method: void Function())
1112
unexpectedMessage (method: String Function(List<Diagnostic>))
1213
package:analyzer_testing/experiments/experiments.dart:
@@ -60,6 +61,7 @@ package:analyzer_testing/resource_provider_mixin.dart:
6061
toUri (method: Uri Function(String))
6162
toUriStr (method: String Function(String))
6263
package:analyzer_testing/src/analysis_rule/pub_package_resolution.dart:
64+
ExpectedContextMessage (non-public)
6365
ExpectedDiagnostic (non-public)
6466
PubPackageResolutionTest (non-public)
6567
package:analyzer_testing/utilities/extensions/resource_provider.dart:

pkg/analyzer_testing/lib/analysis_rule/analysis_rule.dart

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,28 @@ import 'package:analyzer/analysis_rule/pubspec.dart';
1212
import 'package:analyzer/diagnostic/diagnostic.dart';
1313
import 'package:analyzer/error/error.dart';
1414
import 'package:analyzer/error/listener.dart';
15+
import 'package:analyzer/file_system/file_system.dart';
1516
import 'package:analyzer/source/file_source.dart';
1617
import 'package:analyzer/src/lint/pub.dart'; // ignore: implementation_imports
1718
import 'package:analyzer/src/lint/registry.dart'; // ignore: implementation_imports
1819
import 'package:analyzer_testing/src/analysis_rule/pub_package_resolution.dart';
1920
import 'package:analyzer_testing/utilities/utilities.dart';
2021
import 'package:meta/meta.dart';
2122

23+
ExpectedContextMessage contextMessage(
24+
File file,
25+
int offset,
26+
int length, {
27+
String? text,
28+
List<Pattern> textContains = const [],
29+
}) => ExpectedContextMessage(
30+
file,
31+
offset,
32+
length,
33+
text: text,
34+
textContains: textContains,
35+
);
36+
2237
/// Returns an [ExpectedDiagnostic] with the given arguments.
2338
///
2439
/// Just a short-named helper for use in the `assert*Diagnostics` methods.
@@ -27,7 +42,16 @@ ExpectedDiagnostic error(
2742
int offset,
2843
int length, {
2944
Pattern? messageContains,
30-
}) => ExpectedError(code, offset, length, messageContains: messageContains);
45+
Pattern? correctionContains,
46+
List<ExpectedContextMessage>? contextMessages,
47+
}) => ExpectedError(
48+
code,
49+
offset,
50+
length,
51+
messageContains: messageContains,
52+
correctionContains: correctionContains,
53+
contextMessages: contextMessages,
54+
);
3155

3256
/// A base class for analysis rule tests that use test_reflective_loader.
3357
abstract class AnalysisRuleTest extends PubPackageResolutionTest {
@@ -91,12 +115,14 @@ abstract class AnalysisRuleTest extends PubPackageResolutionTest {
91115
Pattern? messageContains,
92116
Pattern? correctionContains,
93117
String? name,
118+
List<ExpectedContextMessage>? contextMessages,
94119
}) => ExpectedLint(
95120
name ?? analysisRule,
96121
offset,
97122
length,
98123
messageContains: messageContains,
99124
correctionContains: correctionContains,
125+
contextMessages: contextMessages,
100126
);
101127

102128
@mustCallSuper

pkg/analyzer_testing/lib/src/analysis_rule/pub_package_resolution.dart

Lines changed: 114 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,66 @@ import 'package:test/test.dart';
2525

2626
typedef DiagnosticMatcher = bool Function(Diagnostic diagnostic);
2727

28+
/// A description of a message that is expected to be reported with an error.
29+
class ExpectedContextMessage {
30+
/// The path of the file with which the message is associated.
31+
final File file;
32+
33+
/// The offset of the beginning of the error's region.
34+
final int offset;
35+
36+
/// The offset of the beginning of the error's region.
37+
final int length;
38+
39+
/// The message text for the error.
40+
final String? text;
41+
42+
/// A list of patterns that should be contained in the message test; empty if
43+
/// the message contents should not be checked.
44+
final List<Pattern> textContains;
45+
46+
ExpectedContextMessage(
47+
this.file,
48+
this.offset,
49+
this.length, {
50+
this.text,
51+
this.textContains = const [],
52+
});
53+
54+
/// Return `true` if the [message] matches this description of what it's
55+
/// expected to be.
56+
bool matches(DiagnosticMessage message) {
57+
if (message.filePath != file.path) {
58+
return false;
59+
}
60+
if (message.offset != offset) {
61+
return false;
62+
}
63+
if (message.length != length) {
64+
return false;
65+
}
66+
var messageText = message.messageText(includeUrl: true);
67+
if (text != null && messageText != text) {
68+
return false;
69+
}
70+
for (var pattern in textContains) {
71+
if (!messageText.contains(pattern)) {
72+
return false;
73+
}
74+
}
75+
return true;
76+
}
77+
}
78+
2879
/// A description of a diagnostic that is expected to be reported.
2980
class ExpectedDiagnostic {
30-
final DiagnosticMatcher _diagnosticMatcher;
81+
final DiagnosticMatcher diagnosticMatcher;
3182

3283
/// The offset of the beginning of the diagnostic's region.
33-
final int _offset;
84+
final int offset;
3485

3586
/// The length of the diagnostic's region.
36-
final int _length;
87+
final int length;
3788

3889
/// A pattern that should be contained in the diagnostic message or `null` if
3990
/// the message contents should not be checked.
@@ -43,20 +94,26 @@ class ExpectedDiagnostic {
4394
/// `null` if the correction message contents should not be checked.
4495
final Pattern? _correctionContains;
4596

97+
/// The list of context messages that are expected to be associated with the
98+
/// error, or `null` if the context messages should not be checked.
99+
final List<ExpectedContextMessage>? _contextMessages;
100+
46101
ExpectedDiagnostic(
47-
this._diagnosticMatcher,
48-
this._offset,
49-
this._length, {
102+
this.diagnosticMatcher,
103+
this.offset,
104+
this.length, {
50105
Pattern? messageContains,
51106
Pattern? correctionContains,
52-
}) : _messageContains = messageContains,
107+
List<ExpectedContextMessage>? contextMessages,
108+
}) : _contextMessages = contextMessages,
109+
_messageContains = messageContains,
53110
_correctionContains = correctionContains;
54111

55112
/// Whether the [diagnostic] matches this description of what it's expected to be.
56113
bool matches(Diagnostic diagnostic) {
57-
if (!_diagnosticMatcher(diagnostic)) return false;
58-
if (diagnostic.offset != _offset) return false;
59-
if (diagnostic.length != _length) return false;
114+
if (!diagnosticMatcher(diagnostic)) return false;
115+
if (diagnostic.offset != offset) return false;
116+
if (diagnostic.length != length) return false;
60117
if (_messageContains != null &&
61118
!diagnostic.message.contains(_messageContains)) {
62119
return false;
@@ -68,6 +125,17 @@ class ExpectedDiagnostic {
68125
return false;
69126
}
70127
}
128+
if (_contextMessages != null) {
129+
var actualContextMessages = diagnostic.contextMessages.toList();
130+
if (actualContextMessages.length != _contextMessages.length) {
131+
return false;
132+
}
133+
for (int i = 0; i < _contextMessages.length; i++) {
134+
if (!_contextMessages[i].matches(actualContextMessages[i])) {
135+
return false;
136+
}
137+
}
138+
}
71139

72140
return true;
73141
}
@@ -77,13 +145,18 @@ class ExpectedDiagnostic {
77145
final class ExpectedError extends ExpectedDiagnostic {
78146
final DiagnosticCode _code;
79147

80-
ExpectedError(this._code, int offset, int length, {Pattern? messageContains})
81-
: super(
82-
(error) => error.diagnosticCode == _code,
83-
offset,
84-
length,
85-
messageContains: messageContains,
86-
);
148+
ExpectedError(
149+
this._code,
150+
int offset,
151+
int length, {
152+
super.messageContains,
153+
super.correctionContains,
154+
super.contextMessages,
155+
}) : super(
156+
(diagnostic) => diagnostic.diagnosticCode == _code,
157+
offset,
158+
length,
159+
);
87160
}
88161

89162
/// A description of an expected lint rule violation.
@@ -96,7 +169,12 @@ final class ExpectedLint extends ExpectedDiagnostic {
96169
int length, {
97170
super.messageContains,
98171
super.correctionContains,
99-
}) : super((error) => error.diagnosticCode.name == _lintName, offset, length);
172+
super.contextMessages,
173+
}) : super(
174+
(diagnostic) => diagnostic.diagnosticCode.name == _lintName,
175+
offset,
176+
length,
177+
);
100178
}
101179

102180
class PubPackageResolutionTest with MockPackagesMixin, ResourceProviderMixin {
@@ -111,6 +189,9 @@ class PubPackageResolutionTest with MockPackagesMixin, ResourceProviderMixin {
111189

112190
AnalysisContextCollectionImpl? _analysisContextCollection;
113191

192+
/// The test file being analyzed.
193+
late File testFile = newFile(_testFilePath, '');
194+
114195
/// The analysis result that is used in various `assertDiagnostics` methods.
115196
late ResolvedUnitResult result;
116197

@@ -331,7 +412,18 @@ class PubPackageResolutionTest with MockPackagesMixin, ResourceProviderMixin {
331412
} else {
332413
buffer.write(' error(${actual.diagnosticCode}, ');
333414
}
334-
buffer.write('${actual.offset}, ${actual.length}),');
415+
buffer.write('${actual.offset}, ${actual.length},');
416+
if (actual.contextMessages.isNotEmpty) {
417+
buffer.write(' contextMessages: [');
418+
for (var contextMessage in actual.contextMessages) {
419+
buffer.write('contextMessage(');
420+
buffer.write("newFile('${contextMessage.filePath}'), ");
421+
buffer.write('${contextMessage.offset}, ${contextMessage.length},');
422+
buffer.write('), ');
423+
}
424+
buffer.write('],');
425+
}
426+
buffer.write('),');
335427
}
336428

337429
return buffer.toString();
@@ -351,8 +443,8 @@ class PubPackageResolutionTest with MockPackagesMixin, ResourceProviderMixin {
351443
if (expected is ExpectedLint) {
352444
buffer.write(expected._lintName);
353445
}
354-
buffer.write(' [${expected._offset}, ');
355-
buffer.write(expected._length);
446+
buffer.write(' [${expected.offset}, ');
447+
buffer.write(expected.length);
356448
if (expected._messageContains case Pattern messageContains) {
357449
buffer.write(', messageContains: ');
358450
buffer.write(json.encode(messageContains.toString()));
@@ -484,7 +576,7 @@ class PubPackageResolutionTest with MockPackagesMixin, ResourceProviderMixin {
484576
}
485577

486578
void _addTestFile(String content) {
487-
newFile(_testFilePath, content);
579+
testFile.writeAsStringSync(content);
488580
}
489581

490582
DriverBasedAnalysisContext _contextFor(String path) {

0 commit comments

Comments
 (0)