Implementing switch expressions in flutter/test/ (#144580)

Migrates test code to new `switch` syntax.
This commit is contained in:
Nate 2024-03-14 15:20:44 -06:00 committed by GitHub
parent dff0343251
commit 17a27358a6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 115 additions and 189 deletions

View file

@ -2662,21 +2662,14 @@ class _TestPostRouteCancelState extends State<_TestPostRouteCancel> {
Widget build(BuildContext context) {
return CupertinoApp(
initialRoute: 'home',
onGenerateRoute: (RouteSettings settings) {
return CupertinoPageRoute<void>(
settings: settings,
builder: (BuildContext context) {
switch (settings.name) {
case 'home':
return _buildHome(context);
case 'sub':
return _buildSub(context);
default:
throw UnimplementedError();
}
},
);
},
onGenerateRoute: (RouteSettings settings) => CupertinoPageRoute<void>(
settings: settings,
builder: (BuildContext context) => switch (settings.name) {
'home' => _buildHome(context),
'sub' => _buildSub(context),
_ => throw UnimplementedError(),
},
),
);
}
}

View file

@ -1119,17 +1119,10 @@ void main() {
await tester.tap(find.byType(DropdownMenu<TestMenu>));
await tester.pump();
late final bool isMobile;
switch (themeData.platform) {
case TargetPlatform.android:
case TargetPlatform.iOS:
case TargetPlatform.fuchsia:
isMobile = true;
case TargetPlatform.macOS:
case TargetPlatform.linux:
case TargetPlatform.windows:
isMobile = false;
}
final bool isMobile = switch (themeData.platform) {
TargetPlatform.android || TargetPlatform.iOS || TargetPlatform.fuchsia => true,
TargetPlatform.macOS || TargetPlatform.linux || TargetPlatform.windows => false,
};
int expectedCount = isMobile ? 0 : 1;
// Test onSelected on key press

View file

@ -1259,12 +1259,10 @@ void main() {
}
final RenderBox renderBox = renderObject as RenderBox;
switch (axis) {
case Axis.horizontal:
return renderBox.size.width;
case Axis.vertical:
return renderBox.size.height;
}
return switch (axis) {
Axis.horizontal => renderBox.size.width,
Axis.vertical => renderBox.size.height,
};
}
group('animated: $animated, scrollDirection: $axis', () {

View file

@ -188,23 +188,16 @@ class FakeAndroidPlatformViewsController {
}
Future<dynamic> _onMethodCall(MethodCall call) {
switch (call.method) {
case 'create':
return _create(call);
case 'dispose':
return _dispose(call);
case 'resize':
return _resize(call);
case 'touch':
return _touch(call);
case 'setDirection':
return _setDirection(call);
case 'clearFocus':
return _clearFocus(call);
case 'offset':
return _offset(call);
}
return Future<dynamic>.sync(() => null);
return switch (call.method) {
'create' => _create(call),
'dispose' => _dispose(call),
'resize' => _resize(call),
'touch' => _touch(call),
'setDirection' => _setDirection(call),
'clearFocus' => _clearFocus(call),
'offset' => _offset(call),
_ => Future<dynamic>.sync(() => null),
};
}
Future<dynamic> _create(MethodCall call) async {
@ -400,17 +393,13 @@ class FakeIosPlatformViewsController {
}
Future<dynamic> _onMethodCall(MethodCall call) {
switch (call.method) {
case 'create':
return _create(call);
case 'dispose':
return _dispose(call);
case 'acceptGesture':
return _acceptGesture(call);
case 'rejectGesture':
return _rejectGesture(call);
}
return Future<dynamic>.sync(() => null);
return switch (call.method) {
'create' => _create(call),
'dispose' => _dispose(call),
'acceptGesture' => _acceptGesture(call),
'rejectGesture' => _rejectGesture(call),
_ => Future<dynamic>.sync(() => null),
};
}
Future<dynamic> _create(MethodCall call) async {
@ -503,17 +492,13 @@ class FakeMacosPlatformViewsController {
}
Future<dynamic> _onMethodCall(MethodCall call) {
switch (call.method) {
case 'create':
return _create(call);
case 'dispose':
return _dispose(call);
case 'acceptGesture':
return _acceptGesture(call);
case 'rejectGesture':
return _rejectGesture(call);
}
return Future<dynamic>.sync(() => null);
return switch (call.method) {
'create' => _create(call),
'dispose' => _dispose(call),
'acceptGesture' => _acceptGesture(call),
'rejectGesture' => _rejectGesture(call),
_ => Future<dynamic>.sync(() => null),
};
}
Future<dynamic> _create(MethodCall call) async {

View file

@ -2241,22 +2241,15 @@ void main() {
// How modifiers are interpreted depends upon the keyCode for GLFW.
int keyCodeForModifier(int modifier, {required bool isLeft}) {
switch (modifier) {
case GLFWKeyHelper.modifierAlt:
return isLeft ? 342 : 346;
case GLFWKeyHelper.modifierShift:
return isLeft ? 340 : 344;
case GLFWKeyHelper.modifierControl:
return isLeft ? 341 : 345;
case GLFWKeyHelper.modifierMeta:
return isLeft ? 343 : 347;
case GLFWKeyHelper.modifierNumericPad:
return 282;
case GLFWKeyHelper.modifierCapsLock:
return 280;
default:
return 65; // keyA
}
return switch (modifier) {
GLFWKeyHelper.modifierAlt => isLeft ? 342 : 346,
GLFWKeyHelper.modifierShift => isLeft ? 340 : 344,
GLFWKeyHelper.modifierControl => isLeft ? 341 : 345,
GLFWKeyHelper.modifierMeta => isLeft ? 343 : 347,
GLFWKeyHelper.modifierNumericPad => 282,
GLFWKeyHelper.modifierCapsLock => 280,
_ => 65, // keyA
};
}
test('modifier keys are recognized individually', () {
@ -2473,22 +2466,15 @@ void main() {
// How modifiers are interpreted depends upon the keyCode for GTK.
int keyCodeForModifier(int modifier, {required bool isLeft}) {
switch (modifier) {
case GtkKeyHelper.modifierMod1:
return 65513;
case GtkKeyHelper.modifierShift:
return isLeft ? 65505 : 65506;
case GtkKeyHelper.modifierControl:
return isLeft ? 65507 : 65508;
case GtkKeyHelper.modifierMeta:
return isLeft ? 65515 : 65516;
case GtkKeyHelper.modifierMod2:
return 65407;
case GtkKeyHelper.modifierCapsLock:
return 65509;
default:
return 65; // keyA
}
return switch (modifier) {
GtkKeyHelper.modifierShift => isLeft ? 65505 : 65506,
GtkKeyHelper.modifierControl => isLeft ? 65507 : 65508,
GtkKeyHelper.modifierMeta => isLeft ? 65515 : 65516,
GtkKeyHelper.modifierMod1 => 65513,
GtkKeyHelper.modifierMod2 => 65407,
GtkKeyHelper.modifierCapsLock => 65509,
_ => 65, // keyA
};
}
test('modifier keys are recognized individually', () {

View file

@ -119,32 +119,22 @@ Future<void> dismissElement(WidgetTester tester, Finder finder, { required AxisD
}
Future<void> dragElement(WidgetTester tester, Finder finder, { required AxisDirection gestureDirection, required double amount }) async {
Offset delta;
switch (gestureDirection) {
case AxisDirection.left:
delta = Offset(-amount, 0.0);
case AxisDirection.right:
delta = Offset(amount, 0.0);
case AxisDirection.up:
delta = Offset(0.0, -amount);
case AxisDirection.down:
delta = Offset(0.0, amount);
}
final Offset delta = switch (gestureDirection) {
AxisDirection.left => Offset(-amount, 0.0),
AxisDirection.right => Offset(amount, 0.0),
AxisDirection.up => Offset(0.0, -amount),
AxisDirection.down => Offset(0.0, amount),
};
await tester.drag(finder, delta);
}
Future<void> flingElement(WidgetTester tester, Finder finder, { required AxisDirection gestureDirection, double initialOffsetFactor = 0.0 }) async {
Offset delta;
switch (gestureDirection) {
case AxisDirection.left:
delta = const Offset(-300.0, 0.0);
case AxisDirection.right:
delta = const Offset(300.0, 0.0);
case AxisDirection.up:
delta = const Offset(0.0, -300.0);
case AxisDirection.down:
delta = const Offset(0.0, 300.0);
}
final Offset delta = switch (gestureDirection) {
AxisDirection.left => const Offset(-300, 0.0),
AxisDirection.right => const Offset(300, 0.0),
AxisDirection.up => const Offset(0.0, -300),
AxisDirection.down => const Offset(0.0, 300),
};
await tester.fling(finder, delta, 1000.0, initialOffset: delta * initialOffsetFactor);
}
@ -213,17 +203,12 @@ Future<void> checkFlingItemAfterMovement(
}
Future<void> rollbackElement(WidgetTester tester, Finder finder, { required AxisDirection gestureDirection, double initialOffsetFactor = 0.0 }) async {
Offset delta;
switch (gestureDirection) {
case AxisDirection.left:
delta = const Offset(-30.0, 0.0);
case AxisDirection.right:
delta = const Offset(30.0, 0.0);
case AxisDirection.up:
delta = const Offset(0.0, -30.0);
case AxisDirection.down:
delta = const Offset(0.0, 30.0);
}
final Offset delta = switch (gestureDirection) {
AxisDirection.left => const Offset(-30.0, 0.0),
AxisDirection.right => const Offset(30.0, 0.0),
AxisDirection.up => const Offset(0.0, -30.0),
AxisDirection.down => const Offset(0.0, 30.0),
};
await tester.fling(finder, delta, 1000.0, initialOffset: delta * initialOffsetFactor);
}

View file

@ -442,13 +442,11 @@ class FakePlatformViewRegistry implements ui_web.PlatformViewRegistry {
}
Future<dynamic> _onMethodCall(MethodCall call) {
switch (call.method) {
case 'create':
return _create(call);
case 'dispose':
return _dispose(call);
}
return Future<dynamic>.sync(() => null);
return switch (call.method) {
'create' => _create(call),
'dispose' => _dispose(call),
_ => Future<dynamic>.sync(() => null),
};
}
Future<dynamic> _create(MethodCall call) async {

View file

@ -38,23 +38,16 @@ class TestAssetBundle extends CachingAssetBundle {
@override
Future<ByteData> load(String key) {
late ByteData data;
switch (key) {
case 'AssetManifest.bin':
data = manifest;
case 'assets/image.png':
data = testByteData(1.0);
case 'assets/1.0x/image.png':
data = testByteData(10.0); // see "...with a main asset and a 1.0x asset"
case 'assets/1.5x/image.png':
data = testByteData(1.5);
case 'assets/2.0x/image.png':
data = testByteData(2.0);
case 'assets/3.0x/image.png':
data = testByteData(3.0);
case 'assets/4.0x/image.png':
data = testByteData(4.0);
}
final ByteData data = switch (key) {
'AssetManifest.bin' => manifest,
'assets/image.png' => testByteData(1.0),
'assets/1.0x/image.png' => testByteData(10.0), // see "...with a main asset and a 1.0x asset"
'assets/1.5x/image.png' => testByteData(1.5),
'assets/2.0x/image.png' => testByteData(2.0),
'assets/3.0x/image.png' => testByteData(3.0),
'assets/4.0x/image.png' => testByteData(4.0),
_ => throw ArgumentError('Unexpected key: $key'),
};
return SynchronousFuture<ByteData>(data);
}

View file

@ -199,12 +199,10 @@ void main() {
testWidgets('Check onstage/offstage handling of barriers around transitions', (WidgetTester tester) async {
await tester.pumpWidget(
MaterialApp(
onGenerateRoute: (RouteSettings settings) {
switch (settings.name) {
case '/': return TestRoute<void>(settings: settings, child: const Text('A'));
case '/1': return TestRoute<void>(settings: settings, barrierColor: const Color(0xFFFFFF00), child: const Text('B'));
}
return null;
onGenerateRoute: (RouteSettings settings) => switch (settings.name) {
'/' => TestRoute<void>(settings: settings, child: const Text('A')),
'/1' => TestRoute<void>(settings: settings, barrierColor: const Color(0xFFFFFF00), child: const Text('B')),
_ => null,
},
),
);

View file

@ -26,13 +26,10 @@ class TestOrientedBox extends SingleChildRenderObjectWidget {
const TestOrientedBox({ super.key, super.child });
Decoration _getDecoration(BuildContext context) {
final Orientation orientation = MediaQuery.orientationOf(context);
switch (orientation) {
case Orientation.landscape:
return const BoxDecoration(color: Color(0xFF00FF00));
case Orientation.portrait:
return const BoxDecoration(color: Color(0xFF0000FF));
}
return switch (MediaQuery.orientationOf(context)) {
Orientation.landscape => const BoxDecoration(color: Color(0xFF00FF00)),
Orientation.portrait => const BoxDecoration(color: Color(0xFF0000FF)),
};
}
@override

View file

@ -286,14 +286,11 @@ class _Diagonal extends RenderObjectWidget with SlottedMultiChildRenderObjectWid
@override
Widget? childForSlot(_DiagonalSlot? slot) {
switch (slot) {
case null:
return nullSlot;
case _DiagonalSlot.topLeft:
return topLeft;
case _DiagonalSlot.bottomRight:
return bottomRight;
}
return switch (slot) {
null => nullSlot,
_DiagonalSlot.topLeft => topLeft,
_DiagonalSlot.bottomRight => bottomRight,
};
}
@override

View file

@ -2008,14 +2008,17 @@ class TextSelectionControlsSpy extends TextSelectionControls {
@override
Widget buildHandle(BuildContext context, TextSelectionHandleType type, double textLineHeight, [VoidCallback? onTap]) {
switch (type) {
case TextSelectionHandleType.left:
return ElevatedButton(onPressed: onTap, child: Text('height ${textLineHeight.toInt()}', key: leftHandleKey));
case TextSelectionHandleType.right:
return ElevatedButton(onPressed: onTap, child: Text('height ${textLineHeight.toInt()}', key: rightHandleKey));
case TextSelectionHandleType.collapsed:
return ElevatedButton(onPressed: onTap, child: Text('height ${textLineHeight.toInt()}', key: collapsedHandleKey));
}
return ElevatedButton(
onPressed: onTap,
child: Text(
key: switch (type) {
TextSelectionHandleType.left => leftHandleKey,
TextSelectionHandleType.right => rightHandleKey,
TextSelectionHandleType.collapsed => collapsedHandleKey,
},
'height ${textLineHeight.toInt()}',
),
);
}
@override