Skip to content

Commit 1804a38

Browse files
srawlinsCommit Queue
authored andcommitted
analyzer: Use Diagnostic instead of deprecated AnalysisError in tests
Change-Id: I0fdc8664f1bfd0b887dedd3c7399046ec6ccb3b5 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/426901 Commit-Queue: Samuel Rawlins <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent e1bb6e9 commit 1804a38

21 files changed

+166
-151
lines changed

pkg/analyzer/test/error/error_listener_test.dart

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'package:analyzer/error/error.dart';
5+
import 'package:analyzer/diagnostic/diagnostic.dart';
66
import 'package:analyzer/error/listener.dart';
77
import 'package:test/test.dart';
88
import 'package:test_reflective_loader/test_reflective_loader.dart';
@@ -17,29 +17,29 @@ main() {
1717
class RecordingErrorListenerTest {
1818
test_orderedAsReported() {
1919
var listener = RecordingErrorListener();
20-
listener.onError(_MockAnalysisError(expectedIndex: 0, hashCode: 1));
21-
listener.onError(_MockAnalysisError(expectedIndex: 1, hashCode: 10));
22-
listener.onError(_MockAnalysisError(expectedIndex: 2, hashCode: -50));
23-
listener.onError(_MockAnalysisError(expectedIndex: 3, hashCode: 20));
24-
listener.onError(_MockAnalysisError(expectedIndex: 4, hashCode: 1));
20+
listener.onError(_MockDiagnostic(expectedIndex: 0, hashCode: 1));
21+
listener.onError(_MockDiagnostic(expectedIndex: 1, hashCode: 10));
22+
listener.onError(_MockDiagnostic(expectedIndex: 2, hashCode: -50));
23+
listener.onError(_MockDiagnostic(expectedIndex: 3, hashCode: 20));
24+
listener.onError(_MockDiagnostic(expectedIndex: 4, hashCode: 1));
2525

2626
// Expect the errors are returned in the order they are reported, and not
2727
// affected by their hashcodes.
2828
expect(
29-
listener.errors.cast<_MockAnalysisError>().map((e) => e.expectedIndex),
29+
listener.errors.cast<_MockDiagnostic>().map((e) => e.expectedIndex),
3030
[0, 1, 2, 3, 4],
3131
);
3232
}
3333
}
3434

35-
/// An [AnalysisError] that allows setting an explicit hash code.
36-
class _MockAnalysisError implements AnalysisError {
35+
/// An [Diagnostic] that allows setting an explicit hash code.
36+
class _MockDiagnostic implements Diagnostic {
3737
@override
3838
int hashCode;
3939

4040
int expectedIndex;
4141

42-
_MockAnalysisError({required this.expectedIndex, required this.hashCode});
42+
_MockDiagnostic({required this.expectedIndex, required this.hashCode});
4343

4444
@override
4545
dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);

pkg/analyzer/test/generated/test_support.dart

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class ExpectedError {
112112

113113
/// Return `true` if the [error] matches this description of what it's
114114
/// expected to be.
115-
bool matches(AnalysisError error) {
115+
bool matches(Diagnostic error) {
116116
if (error.offset != offset ||
117117
error.length != length ||
118118
error.errorCode != code) {
@@ -151,7 +151,7 @@ class GatheringErrorListener implements AnalysisErrorListener {
151151
final bool checkRanges;
152152

153153
/// A list containing the errors that were collected.
154-
final List<AnalysisError> _errors = [];
154+
final List<Diagnostic> _errors = [];
155155

156156
/// A table mapping sources to the line information for the source.
157157
final Map<Source, LineInfo> _lineInfoMap = {};
@@ -160,14 +160,14 @@ class GatheringErrorListener implements AnalysisErrorListener {
160160
GatheringErrorListener({this.checkRanges = true});
161161

162162
/// Return the errors that were collected.
163-
List<AnalysisError> get errors => _errors;
163+
List<Diagnostic> get errors => _errors;
164164

165165
/// Return `true` if at least one error has been gathered.
166166
bool get hasErrors => _errors.isNotEmpty;
167167

168168
/// Add the given [errors] to this listener.
169-
void addAll(List<AnalysisError> errors) {
170-
for (AnalysisError error in errors) {
169+
void addAll(List<Diagnostic> errors) {
170+
for (Diagnostic error in errors) {
171171
onError(error);
172172
}
173173
}
@@ -184,7 +184,7 @@ class GatheringErrorListener implements AnalysisErrorListener {
184184
//
185185
// Match actual errors to expected errors.
186186
//
187-
List<AnalysisError> unmatchedActual = errors.toList();
187+
List<Diagnostic> unmatchedActual = errors.toList();
188188
List<ExpectedError> unmatchedExpected = expectedErrors.toList();
189189
int actualIndex = 0;
190190
while (actualIndex < unmatchedActual.length) {
@@ -242,7 +242,7 @@ class GatheringErrorListener implements AnalysisErrorListener {
242242
buffer.writeln();
243243
}
244244
buffer.writeln('Found but did not expect:');
245-
for (AnalysisError actual in unmatchedActual) {
245+
for (Diagnostic actual in unmatchedActual) {
246246
buffer.write(' ');
247247
buffer.write(actual.errorCode);
248248
buffer.write(' [');
@@ -265,7 +265,7 @@ class GatheringErrorListener implements AnalysisErrorListener {
265265
buffer.writeln('To accept the current state, expect no errors.');
266266
} else {
267267
buffer.writeln('To accept the current state, expect:');
268-
for (AnalysisError actual in errors) {
268+
for (Diagnostic actual in errors) {
269269
List<DiagnosticMessage> contextMessages = actual.contextMessages;
270270
buffer.write(' error(');
271271
buffer.write(actual.errorCode);
@@ -328,11 +328,11 @@ class GatheringErrorListener implements AnalysisErrorListener {
328328
//
329329
// Compute the actual number of each type of error.
330330
//
331-
Map<DiagnosticCode, List<AnalysisError>> errorsByCode =
332-
<DiagnosticCode, List<AnalysisError>>{};
333-
for (AnalysisError error in _errors) {
331+
Map<DiagnosticCode, List<Diagnostic>> errorsByCode =
332+
<DiagnosticCode, List<Diagnostic>>{};
333+
for (Diagnostic error in _errors) {
334334
errorsByCode
335-
.putIfAbsent(error.errorCode, () => <AnalysisError>[])
335+
.putIfAbsent(error.errorCode, () => <Diagnostic>[])
336336
.add(error);
337337
}
338338
//
@@ -365,9 +365,9 @@ class GatheringErrorListener implements AnalysisErrorListener {
365365
//
366366
errorsByCode.forEach((
367367
DiagnosticCode code,
368-
List<AnalysisError> actualErrors,
368+
List<Diagnostic> actualDiagnostics,
369369
) {
370-
int actualCount = actualErrors.length;
370+
int actualCount = actualDiagnostics.length;
371371
if (buffer.length == 0) {
372372
buffer.write("Expected ");
373373
} else {
@@ -378,8 +378,8 @@ class GatheringErrorListener implements AnalysisErrorListener {
378378
buffer.write(", found ");
379379
buffer.write(actualCount);
380380
buffer.write(" (");
381-
for (int i = 0; i < actualErrors.length; i++) {
382-
AnalysisError error = actualErrors[i];
381+
for (int i = 0; i < actualDiagnostics.length; i++) {
382+
Diagnostic error = actualDiagnostics[i];
383383
if (i > 0) {
384384
buffer.write(", ");
385385
}
@@ -408,7 +408,7 @@ class GatheringErrorListener implements AnalysisErrorListener {
408408
}
409409
int actualErrorCount = 0;
410410
int actualWarningCount = 0;
411-
for (AnalysisError error in _errors) {
411+
for (Diagnostic error in _errors) {
412412
if (error.errorCode.errorSeverity == DiagnosticSeverity.ERROR) {
413413
actualErrorCount++;
414414
} else {
@@ -436,7 +436,7 @@ class GatheringErrorListener implements AnalysisErrorListener {
436436
LineInfo? getLineInfo(Source source) => _lineInfoMap[source];
437437

438438
@override
439-
void onError(AnalysisError error) {
439+
void onError(Diagnostic error) {
440440
_errors.add(error);
441441
}
442442

pkg/analyzer/test/id_tests/constant_test.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import 'package:analyzer/dart/ast/ast.dart';
1111
import 'package:analyzer/dart/constant/value.dart';
1212
import 'package:analyzer/dart/element/element.dart';
1313
import 'package:analyzer/dart/element/type.dart';
14-
import 'package:analyzer/error/error.dart';
14+
import 'package:analyzer/diagnostic/diagnostic.dart';
1515
import 'package:analyzer/src/dart/analysis/experiments.dart';
1616
import 'package:analyzer/src/dart/analysis/testing_data.dart';
1717
import 'package:analyzer/src/error/codes.dart';
@@ -55,9 +55,9 @@ class ConstantsDataComputer extends DataComputer<String> {
5555
TestConfig config,
5656
TestingData testingData,
5757
Id id,
58-
List<AnalysisError> errors,
58+
List<Diagnostic> diagnostics,
5959
) {
60-
var errorCodes = errors
60+
var errorCodes = diagnostics
6161
.map((e) => e.errorCode)
6262
.where(
6363
(errorCode) =>

pkg/analyzer/test/id_tests/definite_assignment_test.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import 'package:_fe_analyzer_shared/src/testing/id.dart' show ActualData, Id;
88
import 'package:_fe_analyzer_shared/src/testing/id_testing.dart';
99
import 'package:analyzer/dart/ast/ast.dart';
1010
import 'package:analyzer/dart/element/element.dart';
11-
import 'package:analyzer/error/error.dart';
11+
import 'package:analyzer/diagnostic/diagnostic.dart';
1212
import 'package:analyzer/src/dart/analysis/testing_data.dart';
1313
import 'package:analyzer/src/dart/resolver/flow_analysis_visitor.dart';
1414
import 'package:analyzer/src/error/codes.dart';
@@ -49,9 +49,9 @@ class _DefiniteAssignmentDataComputer extends DataComputer<String> {
4949
TestConfig config,
5050
TestingData testingData,
5151
Id id,
52-
List<AnalysisError> errors,
52+
List<Diagnostic> diagnostics,
5353
) {
54-
var errorCodes = errors
54+
var errorCodes = diagnostics
5555
.map((e) => e.errorCode)
5656
.where(
5757
(errorCode) =>

pkg/analyzer/test/id_tests/definite_unassignment_test.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import 'package:_fe_analyzer_shared/src/testing/id.dart' show ActualData, Id;
88
import 'package:_fe_analyzer_shared/src/testing/id_testing.dart';
99
import 'package:analyzer/dart/ast/ast.dart';
1010
import 'package:analyzer/dart/element/element.dart';
11-
import 'package:analyzer/error/error.dart';
11+
import 'package:analyzer/diagnostic/diagnostic.dart';
1212
import 'package:analyzer/src/dart/analysis/testing_data.dart';
1313
import 'package:analyzer/src/dart/resolver/flow_analysis_visitor.dart';
1414
import 'package:analyzer/src/error/codes.dart';
@@ -49,9 +49,9 @@ class _DefiniteAssignmentDataComputer extends DataComputer<String> {
4949
TestConfig config,
5050
TestingData testingData,
5151
Id id,
52-
List<AnalysisError> errors,
52+
List<Diagnostic> diagnostics,
5353
) {
54-
var errorCodes = errors
54+
var errorCodes = diagnostics
5555
.map((e) => e.errorCode)
5656
.where(
5757
(errorCode) =>

pkg/analyzer/test/id_tests/inheritance_test.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import 'package:_fe_analyzer_shared/src/testing/id_testing.dart';
99
import 'package:analyzer/dart/ast/ast.dart';
1010
import 'package:analyzer/dart/element/element.dart';
1111
import 'package:analyzer/dart/element/type.dart';
12-
import 'package:analyzer/error/error.dart';
12+
import 'package:analyzer/diagnostic/diagnostic.dart';
1313
import 'package:analyzer/src/dart/analysis/testing_data.dart';
1414
import 'package:analyzer/src/dart/element/inheritance_manager3.dart';
1515
import 'package:analyzer/src/util/ast_data_extractor.dart';
@@ -63,9 +63,9 @@ class _InheritanceDataComputer extends DataComputer<String> {
6363
TestConfig config,
6464
TestingData testingData,
6565
Id id,
66-
List<AnalysisError> errors,
66+
List<Diagnostic> diagnostics,
6767
) {
68-
return errors.map((e) => e.errorCode).join(',');
68+
return diagnostics.map((e) => e.errorCode).join(',');
6969
}
7070

7171
@override

pkg/analyzer/test/source/error_processor_test.dart

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:analyzer/dart/analysis/analysis_options.dart';
6+
import 'package:analyzer/diagnostic/diagnostic.dart';
67
import 'package:analyzer/error/error.dart';
78
import 'package:analyzer/source/error_processor.dart';
89
import 'package:analyzer/src/analysis_options/analysis_options_provider.dart';
@@ -16,7 +17,7 @@ import '../generated/test_support.dart';
1617
import '../src/util/yaml_test.dart';
1718

1819
main() {
19-
AnalysisError invalid_assignment = AnalysisError.tmp(
20+
Diagnostic invalid_assignment = Diagnostic.tmp(
2021
source: TestSource(),
2122
offset: 0,
2223
length: 1,
@@ -27,7 +28,7 @@ main() {
2728
],
2829
);
2930

30-
AnalysisError assignment_of_do_not_store = AnalysisError.tmp(
31+
Diagnostic assignment_of_do_not_store = Diagnostic.tmp(
3132
source: TestSource(),
3233
offset: 0,
3334
length: 1,
@@ -37,7 +38,7 @@ main() {
3738
],
3839
);
3940

40-
AnalysisError unused_local_variable = AnalysisError.tmp(
41+
Diagnostic unused_local_variable = Diagnostic.tmp(
4142
source: TestSource(),
4243
offset: 0,
4344
length: 1,
@@ -47,7 +48,7 @@ main() {
4748
],
4849
);
4950

50-
AnalysisError use_of_void_result = AnalysisError.tmp(
51+
Diagnostic use_of_void_result = Diagnostic.tmp(
5152
source: TestSource(),
5253
offset: 0,
5354
length: 1,
@@ -56,7 +57,7 @@ main() {
5657

5758
// We in-line a lint code here in order to avoid adding a dependency on the
5859
// linter package.
59-
AnalysisError annotate_overrides = AnalysisError.tmp(
60+
Diagnostic annotate_overrides = Diagnostic.tmp(
6061
source: TestSource(),
6162
offset: 0,
6263
length: 1,
@@ -188,7 +189,7 @@ class _TestContext {
188189
);
189190
}
190191

191-
ErrorProcessor? getProcessor(AnalysisError error) {
192-
return ErrorProcessor.getProcessor(analysisOptions, error);
192+
ErrorProcessor? getProcessor(Diagnostic diagnostic) {
193+
return ErrorProcessor.getProcessor(analysisOptions, diagnostic);
193194
}
194195
}

pkg/analyzer/test/src/dart/analysis/driver_caching_test.dart

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import 'dart:async';
66

77
import 'package:analyzer/dart/analysis/results.dart';
88
import 'package:analyzer/dart/element/element.dart';
9-
import 'package:analyzer/error/error.dart';
9+
import 'package:analyzer/diagnostic/diagnostic.dart';
1010
import 'package:analyzer/file_system/file_system.dart';
1111
import 'package:analyzer/src/dart/analysis/driver.dart';
1212
import 'package:analyzer/src/error/codes.dart';
@@ -51,7 +51,7 @@ int b = a;
5151
''');
5252

5353
// `strict-cast: false`, so no errors.
54-
assertErrorsInList(await _computeTestFileErrors(), []);
54+
assertErrorsInList(await _computeTestFileDiagnostics(), []);
5555

5656
// Configure `strict-casts: true`.
5757
await disposeAnalysisContextCollection();
@@ -60,7 +60,7 @@ int b = a;
6060
);
6161

6262
// `strict-cast: true`, so has errors.
63-
assertErrorsInList(await _computeTestFileErrors(), [
63+
assertErrorsInList(await _computeTestFileDiagnostics(), [
6464
error(CompileTimeErrorCode.INVALID_ASSIGNMENT, 23, 1),
6565
]);
6666
}
@@ -273,7 +273,7 @@ import 'package:aaa/a.dart';
273273

274274
// We don't have a dependency on `package:aaa`, so there is a lint.
275275
_assertHasLintReported(
276-
await _computeTestFileErrors(),
276+
await _computeTestFileDiagnostics(),
277277
'depend_on_referenced_packages',
278278
);
279279

@@ -290,7 +290,7 @@ import 'package:aaa/a.dart';
290290
);
291291

292292
// With dependency on `package:aaa` added, no lint is reported.
293-
expect(await _computeTestFileErrors(), isEmpty);
293+
expect(await _computeTestFileDiagnostics(), isEmpty);
294294

295295
// Lints don't affect summaries, nothing should be linked.
296296
_assertNoLinkedCycles();
@@ -343,9 +343,9 @@ void f() {
343343
}
344344
}
345345

346-
void _assertHasLintReported(List<AnalysisError> errors, String name) {
346+
void _assertHasLintReported(List<Diagnostic> diagnostics, String name) {
347347
var matching =
348-
errors.where((element) {
348+
diagnostics.where((element) {
349349
var errorCode = element.errorCode;
350350
return errorCode is LintCode && errorCode.name == name;
351351
}).toList();
@@ -358,11 +358,11 @@ void f() {
358358

359359
/// Note that we intentionally use this method, we don't want to use
360360
/// [resolveFile] instead. Resolving a file will force to produce its
361-
/// resolved AST, and as a result to recompute the errors.
361+
/// resolved AST, and as a result to recompute the diagnostics.
362362
///
363-
/// But this method is used to check returning errors from the cache, or
363+
/// But this method is used to check returning diagnostics from the cache, or
364364
/// recomputing when the cache key is expected to be different.
365-
Future<List<AnalysisError>> _computeTestFileErrors() async {
365+
Future<List<Diagnostic>> _computeTestFileDiagnostics() async {
366366
var errorsResult =
367367
await contextFor(testFile).currentSession.getErrors(testFile.path)
368368
as ErrorsResult;

0 commit comments

Comments
 (0)