Skip to content

Commit be89f23

Browse files
stereotype441Commit Queue
authored andcommitted
[analyzer] Fix output generation upon test failure to use camelCase.
When `GatheringDiagnosticListener.assertErrors` detects a test failure, it outputs a suggestion for how to change the test expectations to match the current behavior. Since error code constants are in `camelCase` now, but `DiagnosticCode.uniqueName` still takes the form `ClassName.SCREAMING_CAPS` or `ClassName.snake_case`, this logic needs to be updated to convert the text after the `.` to `camelCase`. Change-Id: I6a6a696478e0443aab5184ac3331af9aac29a9aa Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/445407 Reviewed-by: Johnni Winther <[email protected]> Commit-Queue: Paul Berry <[email protected]>
1 parent 315009e commit be89f23

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

pkg/analyzer/test/generated/test_support.dart

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import 'package:analyzer/instrumentation/instrumentation.dart';
1212
import 'package:analyzer/source/line_info.dart';
1313
import 'package:analyzer/source/source.dart';
1414
import 'package:analyzer/src/generated/engine.dart';
15+
import 'package:analyzer/src/utilities/extensions/string.dart';
1516
import 'package:test/test.dart';
1617

1718
/// A description of a message that is expected to be reported with an error.
@@ -271,7 +272,7 @@ class GatheringDiagnosticListener implements DiagnosticListener {
271272
for (Diagnostic actual in diagnostics) {
272273
List<DiagnosticMessage> contextMessages = actual.contextMessages;
273274
buffer.write(' error(');
274-
buffer.write(actual.diagnosticCode);
275+
buffer.write(actual.diagnosticCode.constantName);
275276
buffer.write(', ');
276277
buffer.write(actual.offset);
277278
buffer.write(', ');
@@ -554,3 +555,19 @@ class TestSourceWithUri extends TestSource {
554555
return false;
555556
}
556557
}
558+
559+
extension on DiagnosticCode {
560+
/// The name of the constant in the analyzer package (or other related
561+
/// package) that represents this diagnostic code.
562+
///
563+
/// This string is used when generating test failure messages that suggest how
564+
/// to change test expectations to match the current behavior.
565+
///
566+
/// For example, if the unique name is `TestClass.MY_ERROR`, this method will
567+
/// return `TestClass.myError`.
568+
String get constantName => switch (uniqueName.split('.')) {
569+
[var className, var snakeCaseName] =>
570+
'$className.${snakeCaseName.toCamelCase()}',
571+
_ => throw StateError('Malformed DiagnosticCode: $uniqueName'),
572+
};
573+
}

0 commit comments

Comments
 (0)