Unverified Commit 5ddc00cc authored by Ian Hickson's avatar Ian Hickson Committed by GitHub

Use `EnumName.name` where possible. (#94496)

parent c567f843
...@@ -74,7 +74,7 @@ class _MyStatefulWidgetState extends State<MyStatefulWidget> { ...@@ -74,7 +74,7 @@ class _MyStatefulWidgetState extends State<MyStatefulWidget> {
}, },
value: UnfocusDisposition.values[index], value: UnfocusDisposition.values[index],
), ),
Text(describeEnum(UnfocusDisposition.values[index])), Text(UnfocusDisposition.values[index].name),
], ],
); );
}), }),
......
...@@ -1568,13 +1568,13 @@ abstract class DiagnosticsNode { ...@@ -1568,13 +1568,13 @@ abstract class DiagnosticsNode {
if (!showSeparator) if (!showSeparator)
'showSeparator': showSeparator, 'showSeparator': showSeparator,
if (level != DiagnosticLevel.info) if (level != DiagnosticLevel.info)
'level': describeEnum(level), 'level': level.name,
if (showName == false) if (showName == false)
'showName': showName, 'showName': showName,
if (emptyBodyDescription != null) if (emptyBodyDescription != null)
'emptyBodyDescription': emptyBodyDescription, 'emptyBodyDescription': emptyBodyDescription,
if (style != DiagnosticsTreeStyle.sparse) if (style != DiagnosticsTreeStyle.sparse)
'style': describeEnum(style!), 'style': style!.name,
if (allowTruncate) if (allowTruncate)
'allowTruncate': allowTruncate, 'allowTruncate': allowTruncate,
if (hasChildren) if (hasChildren)
...@@ -2303,6 +2303,12 @@ class IterableProperty<T> extends DiagnosticsProperty<Iterable<T>> { ...@@ -2303,6 +2303,12 @@ class IterableProperty<T> extends DiagnosticsProperty<Iterable<T>> {
/// The enum value is displayed with the class name stripped. For example: /// The enum value is displayed with the class name stripped. For example:
/// [HitTestBehavior.deferToChild] is shown as `deferToChild`. /// [HitTestBehavior.deferToChild] is shown as `deferToChild`.
/// ///
/// This class can be used with classes that appear like enums but are not
/// "real" enums, so long as their `toString` implementation, in debug mode,
/// returns a string consisting of the class name followed by the value name. It
/// can also be used with nullable properties; the null value is represented as
/// `null`.
///
/// See also: /// See also:
/// ///
/// * [DiagnosticsProperty] which documents named parameters common to all /// * [DiagnosticsProperty] which documents named parameters common to all
...@@ -2702,7 +2708,7 @@ class DiagnosticsProperty<T> extends DiagnosticsNode { ...@@ -2702,7 +2708,7 @@ class DiagnosticsProperty<T> extends DiagnosticsNode {
if (exception != null) if (exception != null)
json['exception'] = exception.toString(); json['exception'] = exception.toString();
json['propertyType'] = propertyType.toString(); json['propertyType'] = propertyType.toString();
json['defaultLevel'] = describeEnum(_defaultLevel); json['defaultLevel'] = _defaultLevel.name;
if (value is Diagnosticable || value is DiagnosticsNode) if (value is Diagnosticable || value is DiagnosticsNode)
json['isDiagnosticableValue'] = true; json['isDiagnosticableValue'] = true;
if (v is num) if (v is num)
...@@ -3016,11 +3022,19 @@ String shortHash(Object? object) { ...@@ -3016,11 +3022,19 @@ String shortHash(Object? object) {
/// * [Object.runtimeType], the [Type] of an object. /// * [Object.runtimeType], the [Type] of an object.
String describeIdentity(Object? object) => '${objectRuntimeType(object, '<optimized out>')}#${shortHash(object)}'; String describeIdentity(Object? object) => '${objectRuntimeType(object, '<optimized out>')}#${shortHash(object)}';
// This method exists as a workaround for https://github.com/dart-lang/sdk/issues/30021
/// Returns a short description of an enum value. /// Returns a short description of an enum value.
/// ///
/// Strips off the enum class name from the `enumEntry.toString()`. /// Strips off the enum class name from the `enumEntry.toString()`.
/// ///
/// For real enums, this is redundant with calling the `name` getter on the enum
/// value (see [EnumName.name]), a feature that was added to Dart 2.15.
///
/// This function can also be used with classes whose `toString` return a value
/// in the same form as an enum (the class name, a dot, then the value name).
/// For example, it's used with [SemanticsAction], which is written to appear to
/// be an enum but is actually a bespoke class so that the index values can be
/// set as powers of two instead of as sequential integers.
///
/// {@tool snippet} /// {@tool snippet}
/// ///
/// ```dart /// ```dart
...@@ -3031,10 +3045,13 @@ String describeIdentity(Object? object) => '${objectRuntimeType(object, '<optimi ...@@ -3031,10 +3045,13 @@ String describeIdentity(Object? object) => '${objectRuntimeType(object, '<optimi
/// void validateDescribeEnum() { /// void validateDescribeEnum() {
/// assert(Day.monday.toString() == 'Day.monday'); /// assert(Day.monday.toString() == 'Day.monday');
/// assert(describeEnum(Day.monday) == 'monday'); /// assert(describeEnum(Day.monday) == 'monday');
/// assert(Day.monday.name == 'monday'); // preferred for real enums
/// } /// }
/// ``` /// ```
/// {@end-tool} /// {@end-tool}
String describeEnum(Object enumEntry) { String describeEnum(Object enumEntry) {
if (enumEntry is Enum)
return enumEntry.name;
final String description = enumEntry.toString(); final String description = enumEntry.toString();
final int indexOfDot = description.indexOf('.'); final int indexOfDot = description.indexOf('.');
assert( assert(
......
...@@ -148,7 +148,7 @@ class ImageConfiguration { ...@@ -148,7 +148,7 @@ class ImageConfiguration {
if (platform != null) { if (platform != null) {
if (hasArguments) if (hasArguments)
result.write(', '); result.write(', ');
result.write('platform: ${describeEnum(platform!)}'); result.write('platform: ${platform!.name}');
hasArguments = true; hasArguments = true;
} }
result.write(')'); result.write(')');
......
...@@ -1378,7 +1378,7 @@ class TextStyle with Diagnosticable { ...@@ -1378,7 +1378,7 @@ class TextStyle with Diagnosticable {
if (decoration != null || decorationColor != null || decorationStyle != null || decorationThickness != null) { if (decoration != null || decorationColor != null || decorationStyle != null || decorationThickness != null) {
final List<String> decorationDescription = <String>[]; final List<String> decorationDescription = <String>[];
if (decorationStyle != null) if (decorationStyle != null)
decorationDescription.add(describeEnum(decorationStyle!)); decorationDescription.add(decorationStyle!.name);
// Hide decorationColor from the default text view as it is shown in the // Hide decorationColor from the default text view as it is shown in the
// terse decoration summary as well. // terse decoration summary as well.
......
...@@ -605,7 +605,7 @@ class MediaQueryData { ...@@ -605,7 +605,7 @@ class MediaQueryData {
'disableAnimations: $disableAnimations', 'disableAnimations: $disableAnimations',
'invertColors: $invertColors', 'invertColors: $invertColors',
'boldText: $boldText', 'boldText: $boldText',
'navigationMode: ${describeEnum(navigationMode)}', 'navigationMode: ${navigationMode.name}',
'gestureSettings: $gestureSettings', 'gestureSettings: $gestureSettings',
]; ];
return '${objectRuntimeType(this, 'MediaQueryData')}(${properties.join(', ')})'; return '${objectRuntimeType(this, 'MediaQueryData')}(${properties.join(', ')})';
......
...@@ -717,7 +717,7 @@ void main() { ...@@ -717,7 +717,7 @@ void main() {
await expectLater( await expectLater(
find.byKey(const ValueKey<int>(1)), find.byKey(const ValueKey<int>(1)),
matchesGoldenFile( matchesGoldenFile(
'text_field_cursor_test.cupertino_${describeEnum(debugDefaultTargetPlatformOverride!).toLowerCase()}.1.png', 'text_field_cursor_test.cupertino_${debugDefaultTargetPlatformOverride!.name.toLowerCase()}.1.png',
), ),
); );
}, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.iOS, TargetPlatform.macOS })); }, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.iOS, TargetPlatform.macOS }));
......
...@@ -59,10 +59,10 @@ void validateNodeJsonSerializationHelper(Map<String, Object?> json, DiagnosticsN ...@@ -59,10 +59,10 @@ void validateNodeJsonSerializationHelper(Map<String, Object?> json, DiagnosticsN
expect(json['name'], equals(node.name)); expect(json['name'], equals(node.name));
expect(json['showSeparator'] ?? true, equals(node.showSeparator)); expect(json['showSeparator'] ?? true, equals(node.showSeparator));
expect(json['description'], equals(node.toDescription())); expect(json['description'], equals(node.toDescription()));
expect(json['level'] ?? describeEnum(DiagnosticLevel.info), equals(describeEnum(node.level))); expect(json['level'] ?? DiagnosticLevel.info.name, equals(node.level.name));
expect(json['showName'] ?? true, equals(node.showName)); expect(json['showName'] ?? true, equals(node.showName));
expect(json['emptyBodyDescription'], equals(node.emptyBodyDescription)); expect(json['emptyBodyDescription'], equals(node.emptyBodyDescription));
expect(json['style'] ?? describeEnum(DiagnosticsTreeStyle.sparse), equals(describeEnum(node.style!))); expect(json['style'] ?? DiagnosticsTreeStyle.sparse.name, equals(node.style!.name));
expect(json['type'], equals(node.runtimeType.toString())); expect(json['type'], equals(node.runtimeType.toString()));
expect(json['hasChildren'] ?? false, equals(node.getChildren().isNotEmpty)); expect(json['hasChildren'] ?? false, equals(node.getChildren().isNotEmpty));
} }
......
...@@ -100,8 +100,8 @@ void main() { ...@@ -100,8 +100,8 @@ void main() {
center = tester.getCenter(title); center = tester.getCenter(title);
size = tester.getSize(title); size = tester.getSize(title);
expect(center.dx, greaterThan(400 - size.width / 2.0), reason: 'on ${describeEnum(platform)}'); expect(center.dx, greaterThan(400 - size.width / 2.0), reason: 'on ${platform.name}');
expect(center.dx, lessThan(400 + size.width / 2.0), reason: 'on ${describeEnum(platform)}'); expect(center.dx, lessThan(400 + size.width / 2.0), reason: 'on ${platform.name}');
// One action is still centered. // One action is still centered.
...@@ -121,8 +121,8 @@ void main() { ...@@ -121,8 +121,8 @@ void main() {
center = tester.getCenter(title); center = tester.getCenter(title);
size = tester.getSize(title); size = tester.getSize(title);
expect(center.dx, greaterThan(400 - size.width / 2.0), reason: 'on ${describeEnum(platform)}'); expect(center.dx, greaterThan(400 - size.width / 2.0), reason: 'on ${platform.name}');
expect(center.dx, lessThan(400 + size.width / 2.0), reason: 'on ${describeEnum(platform)}'); expect(center.dx, lessThan(400 + size.width / 2.0), reason: 'on ${platform.name}');
// Two actions is left aligned again. // Two actions is left aligned again.
...@@ -143,7 +143,7 @@ void main() { ...@@ -143,7 +143,7 @@ void main() {
center = tester.getCenter(title); center = tester.getCenter(title);
size = tester.getSize(title); size = tester.getSize(title);
expect(center.dx, lessThan(400 - size.width / 2.0), reason: 'on ${describeEnum(platform)}'); expect(center.dx, lessThan(400 - size.width / 2.0), reason: 'on ${platform.name}');
} }
}); });
......
...@@ -2138,11 +2138,11 @@ void main() { ...@@ -2138,11 +2138,11 @@ void main() {
expect(find.byType(Slider), findsOneWidget); expect(find.byType(Slider), findsOneWidget);
expect(find.byType(CupertinoSlider), findsOneWidget); expect(find.byType(CupertinoSlider), findsOneWidget);
expect(value, 0.5, reason: 'on ${describeEnum(platform)}'); expect(value, 0.5, reason: 'on ${platform.name}');
final TestGesture gesture = await tester.startGesture(tester.getCenter(find.byType(CupertinoSlider))); final TestGesture gesture = await tester.startGesture(tester.getCenter(find.byType(CupertinoSlider)));
// Drag to the right end of the track. // Drag to the right end of the track.
await gesture.moveBy(const Offset(600.0, 0.0)); await gesture.moveBy(const Offset(600.0, 0.0));
expect(value, 1.0, reason: 'on ${describeEnum(platform)}'); expect(value, 1.0, reason: 'on ${platform.name}');
await gesture.up(); await gesture.up();
} }
...@@ -2153,11 +2153,11 @@ void main() { ...@@ -2153,11 +2153,11 @@ void main() {
expect(find.byType(Slider), findsOneWidget); expect(find.byType(Slider), findsOneWidget);
expect(find.byType(CupertinoSlider), findsNothing); expect(find.byType(CupertinoSlider), findsNothing);
expect(value, 0.5, reason: 'on ${describeEnum(platform)}'); expect(value, 0.5, reason: 'on ${platform.name}');
final TestGesture gesture = await tester.startGesture(tester.getCenter(find.byType(Slider))); final TestGesture gesture = await tester.startGesture(tester.getCenter(find.byType(Slider)));
// Drag to the right end of the track. // Drag to the right end of the track.
await gesture.moveBy(const Offset(600.0, 0.0)); await gesture.moveBy(const Offset(600.0, 0.0));
expect(value, 1.0, reason: 'on ${describeEnum(platform)}'); expect(value, 1.0, reason: 'on ${platform.name}');
await gesture.up(); await gesture.up();
} }
}); });
......
...@@ -198,10 +198,10 @@ void main() { ...@@ -198,10 +198,10 @@ void main() {
value = false; value = false;
await tester.pumpWidget(buildFrame(platform)); await tester.pumpWidget(buildFrame(platform));
expect(find.byType(CupertinoSwitch), findsOneWidget); expect(find.byType(CupertinoSwitch), findsOneWidget);
expect(value, isFalse, reason: 'on ${describeEnum(platform)}'); expect(value, isFalse, reason: 'on ${platform.name}');
await tester.tap(find.byType(SwitchListTile)); await tester.tap(find.byType(SwitchListTile));
expect(value, isTrue, reason: 'on ${describeEnum(platform)}'); expect(value, isTrue, reason: 'on ${platform.name}');
} }
for (final TargetPlatform platform in <TargetPlatform>[ TargetPlatform.android, TargetPlatform.fuchsia, TargetPlatform.linux, TargetPlatform.windows ]) { for (final TargetPlatform platform in <TargetPlatform>[ TargetPlatform.android, TargetPlatform.fuchsia, TargetPlatform.linux, TargetPlatform.windows ]) {
...@@ -210,9 +210,9 @@ void main() { ...@@ -210,9 +210,9 @@ void main() {
await tester.pumpAndSettle(); // Finish the theme change animation. await tester.pumpAndSettle(); // Finish the theme change animation.
expect(find.byType(CupertinoSwitch), findsNothing); expect(find.byType(CupertinoSwitch), findsNothing);
expect(value, isFalse, reason: 'on ${describeEnum(platform)}'); expect(value, isFalse, reason: 'on ${platform.name}');
await tester.tap(find.byType(SwitchListTile)); await tester.tap(find.byType(SwitchListTile));
expect(value, isTrue, reason: 'on ${describeEnum(platform)}'); expect(value, isTrue, reason: 'on ${platform.name}');
} }
}); });
......
...@@ -752,14 +752,14 @@ void main() { ...@@ -752,14 +752,14 @@ void main() {
for (final TargetPlatform platform in <TargetPlatform>[ TargetPlatform.iOS, TargetPlatform.macOS ]) { for (final TargetPlatform platform in <TargetPlatform>[ TargetPlatform.iOS, TargetPlatform.macOS ]) {
value = false; value = false;
await tester.pumpWidget(buildFrame(platform)); await tester.pumpWidget(buildFrame(platform));
expect(find.byType(CupertinoSwitch), findsOneWidget, reason: 'on ${describeEnum(platform)}'); expect(find.byType(CupertinoSwitch), findsOneWidget, reason: 'on ${platform.name}');
final CupertinoSwitch adaptiveSwitch = tester.widget(find.byType(CupertinoSwitch)); final CupertinoSwitch adaptiveSwitch = tester.widget(find.byType(CupertinoSwitch));
expect(adaptiveSwitch.trackColor, inactiveTrackColor, reason: 'on ${describeEnum(platform)}'); expect(adaptiveSwitch.trackColor, inactiveTrackColor, reason: 'on ${platform.name}');
expect(value, isFalse, reason: 'on ${describeEnum(platform)}'); expect(value, isFalse, reason: 'on ${platform.name}');
await tester.tap(find.byType(Switch)); await tester.tap(find.byType(Switch));
expect(value, isTrue, reason: 'on ${describeEnum(platform)}'); expect(value, isTrue, reason: 'on ${platform.name}');
} }
for (final TargetPlatform platform in <TargetPlatform>[ TargetPlatform.android, TargetPlatform.fuchsia, TargetPlatform.linux, TargetPlatform.windows ]) { for (final TargetPlatform platform in <TargetPlatform>[ TargetPlatform.android, TargetPlatform.fuchsia, TargetPlatform.linux, TargetPlatform.windows ]) {
...@@ -767,9 +767,9 @@ void main() { ...@@ -767,9 +767,9 @@ void main() {
await tester.pumpWidget(buildFrame(platform)); await tester.pumpWidget(buildFrame(platform));
await tester.pumpAndSettle(); // Finish the theme change animation. await tester.pumpAndSettle(); // Finish the theme change animation.
expect(find.byType(CupertinoSwitch), findsNothing); expect(find.byType(CupertinoSwitch), findsNothing);
expect(value, isFalse, reason: 'on ${describeEnum(platform)}'); expect(value, isFalse, reason: 'on ${platform.name}');
await tester.tap(find.byType(Switch)); await tester.tap(find.byType(Switch));
expect(value, isTrue, reason: 'on ${describeEnum(platform)}'); expect(value, isTrue, reason: 'on ${platform.name}');
} }
}); });
......
...@@ -641,7 +641,7 @@ void main() { ...@@ -641,7 +641,7 @@ void main() {
await expectLater( await expectLater(
find.byKey(const ValueKey<int>(1)), find.byKey(const ValueKey<int>(1)),
matchesGoldenFile( matchesGoldenFile(
'text_field_cursor_test_${describeEnum(debugDefaultTargetPlatformOverride!).toLowerCase()}.material.1.png', 'text_field_cursor_test_${debugDefaultTargetPlatformOverride!.name.toLowerCase()}.material.1.png',
), ),
); );
}, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.iOS, TargetPlatform.macOS })); }, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.iOS, TargetPlatform.macOS }));
......
...@@ -5054,7 +5054,7 @@ void main() { ...@@ -5054,7 +5054,7 @@ void main() {
testWidgets( testWidgets(
'keyboard shortcuts respect read-only', 'keyboard shortcuts respect read-only',
(WidgetTester tester) async { (WidgetTester tester) async {
final String platform = describeEnum(defaultTargetPlatform).toLowerCase(); final String platform = defaultTargetPlatform.name.toLowerCase();
final TextEditingController controller = TextEditingController(text: testText); final TextEditingController controller = TextEditingController(text: testText);
controller.selection = const TextSelection( controller.selection = const TextSelection(
baseOffset: 0, baseOffset: 0,
......
...@@ -823,7 +823,7 @@ class _IncludesNodeWith extends Matcher { ...@@ -823,7 +823,7 @@ class _IncludesNodeWith extends Matcher {
if (label != null) 'label "$label"', if (label != null) 'label "$label"',
if (value != null) 'value "$value"', if (value != null) 'value "$value"',
if (hint != null) 'hint "$hint"', if (hint != null) 'hint "$hint"',
if (textDirection != null) ' (${describeEnum(textDirection!)})', if (textDirection != null) ' (${textDirection!.name})',
if (actions != null) 'actions "${actions!.join(', ')}"', if (actions != null) 'actions "${actions!.join(', ')}"',
if (flags != null) 'flags "${flags!.join(', ')}"', if (flags != null) 'flags "${flags!.join(', ')}"',
if (scrollPosition != null) 'scrollPosition "$scrollPosition"', if (scrollPosition != null) 'scrollPosition "$scrollPosition"',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment