Skip to content

Commit 17a2735

Browse files
Implementing switch expressions in flutter/test/ (#144580)
Migrates test code to new `switch` syntax.
1 parent dff0343 commit 17a2735

12 files changed

+115
-189
lines changed

packages/flutter/test/cupertino/route_test.dart

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2662,21 +2662,14 @@ class _TestPostRouteCancelState extends State<_TestPostRouteCancel> {
26622662
Widget build(BuildContext context) {
26632663
return CupertinoApp(
26642664
initialRoute: 'home',
2665-
onGenerateRoute: (RouteSettings settings) {
2666-
return CupertinoPageRoute<void>(
2667-
settings: settings,
2668-
builder: (BuildContext context) {
2669-
switch (settings.name) {
2670-
case 'home':
2671-
return _buildHome(context);
2672-
case 'sub':
2673-
return _buildSub(context);
2674-
default:
2675-
throw UnimplementedError();
2676-
}
2677-
},
2678-
);
2679-
},
2665+
onGenerateRoute: (RouteSettings settings) => CupertinoPageRoute<void>(
2666+
settings: settings,
2667+
builder: (BuildContext context) => switch (settings.name) {
2668+
'home' => _buildHome(context),
2669+
'sub' => _buildSub(context),
2670+
_ => throw UnimplementedError(),
2671+
},
2672+
),
26802673
);
26812674
}
26822675
}

packages/flutter/test/material/dropdown_menu_test.dart

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,17 +1119,10 @@ void main() {
11191119
await tester.tap(find.byType(DropdownMenu<TestMenu>));
11201120
await tester.pump();
11211121

1122-
late final bool isMobile;
1123-
switch (themeData.platform) {
1124-
case TargetPlatform.android:
1125-
case TargetPlatform.iOS:
1126-
case TargetPlatform.fuchsia:
1127-
isMobile = true;
1128-
case TargetPlatform.macOS:
1129-
case TargetPlatform.linux:
1130-
case TargetPlatform.windows:
1131-
isMobile = false;
1132-
}
1122+
final bool isMobile = switch (themeData.platform) {
1123+
TargetPlatform.android || TargetPlatform.iOS || TargetPlatform.fuchsia => true,
1124+
TargetPlatform.macOS || TargetPlatform.linux || TargetPlatform.windows => false,
1125+
};
11331126
int expectedCount = isMobile ? 0 : 1;
11341127

11351128
// Test onSelected on key press

packages/flutter/test/rendering/viewport_test.dart

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,12 +1259,10 @@ void main() {
12591259
}
12601260

12611261
final RenderBox renderBox = renderObject as RenderBox;
1262-
switch (axis) {
1263-
case Axis.horizontal:
1264-
return renderBox.size.width;
1265-
case Axis.vertical:
1266-
return renderBox.size.height;
1267-
}
1262+
return switch (axis) {
1263+
Axis.horizontal => renderBox.size.width,
1264+
Axis.vertical => renderBox.size.height,
1265+
};
12681266
}
12691267

12701268
group('animated: $animated, scrollDirection: $axis', () {

packages/flutter/test/services/fake_platform_views.dart

Lines changed: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -188,23 +188,16 @@ class FakeAndroidPlatformViewsController {
188188
}
189189

190190
Future<dynamic> _onMethodCall(MethodCall call) {
191-
switch (call.method) {
192-
case 'create':
193-
return _create(call);
194-
case 'dispose':
195-
return _dispose(call);
196-
case 'resize':
197-
return _resize(call);
198-
case 'touch':
199-
return _touch(call);
200-
case 'setDirection':
201-
return _setDirection(call);
202-
case 'clearFocus':
203-
return _clearFocus(call);
204-
case 'offset':
205-
return _offset(call);
206-
}
207-
return Future<dynamic>.sync(() => null);
191+
return switch (call.method) {
192+
'create' => _create(call),
193+
'dispose' => _dispose(call),
194+
'resize' => _resize(call),
195+
'touch' => _touch(call),
196+
'setDirection' => _setDirection(call),
197+
'clearFocus' => _clearFocus(call),
198+
'offset' => _offset(call),
199+
_ => Future<dynamic>.sync(() => null),
200+
};
208201
}
209202

210203
Future<dynamic> _create(MethodCall call) async {
@@ -400,17 +393,13 @@ class FakeIosPlatformViewsController {
400393
}
401394

402395
Future<dynamic> _onMethodCall(MethodCall call) {
403-
switch (call.method) {
404-
case 'create':
405-
return _create(call);
406-
case 'dispose':
407-
return _dispose(call);
408-
case 'acceptGesture':
409-
return _acceptGesture(call);
410-
case 'rejectGesture':
411-
return _rejectGesture(call);
412-
}
413-
return Future<dynamic>.sync(() => null);
396+
return switch (call.method) {
397+
'create' => _create(call),
398+
'dispose' => _dispose(call),
399+
'acceptGesture' => _acceptGesture(call),
400+
'rejectGesture' => _rejectGesture(call),
401+
_ => Future<dynamic>.sync(() => null),
402+
};
414403
}
415404

416405
Future<dynamic> _create(MethodCall call) async {
@@ -503,17 +492,13 @@ class FakeMacosPlatformViewsController {
503492
}
504493

505494
Future<dynamic> _onMethodCall(MethodCall call) {
506-
switch (call.method) {
507-
case 'create':
508-
return _create(call);
509-
case 'dispose':
510-
return _dispose(call);
511-
case 'acceptGesture':
512-
return _acceptGesture(call);
513-
case 'rejectGesture':
514-
return _rejectGesture(call);
515-
}
516-
return Future<dynamic>.sync(() => null);
495+
return switch (call.method) {
496+
'create' => _create(call),
497+
'dispose' => _dispose(call),
498+
'acceptGesture' => _acceptGesture(call),
499+
'rejectGesture' => _rejectGesture(call),
500+
_ => Future<dynamic>.sync(() => null),
501+
};
517502
}
518503

519504
Future<dynamic> _create(MethodCall call) async {

packages/flutter/test/services/raw_keyboard_test.dart

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2241,22 +2241,15 @@ void main() {
22412241

22422242
// How modifiers are interpreted depends upon the keyCode for GLFW.
22432243
int keyCodeForModifier(int modifier, {required bool isLeft}) {
2244-
switch (modifier) {
2245-
case GLFWKeyHelper.modifierAlt:
2246-
return isLeft ? 342 : 346;
2247-
case GLFWKeyHelper.modifierShift:
2248-
return isLeft ? 340 : 344;
2249-
case GLFWKeyHelper.modifierControl:
2250-
return isLeft ? 341 : 345;
2251-
case GLFWKeyHelper.modifierMeta:
2252-
return isLeft ? 343 : 347;
2253-
case GLFWKeyHelper.modifierNumericPad:
2254-
return 282;
2255-
case GLFWKeyHelper.modifierCapsLock:
2256-
return 280;
2257-
default:
2258-
return 65; // keyA
2259-
}
2244+
return switch (modifier) {
2245+
GLFWKeyHelper.modifierAlt => isLeft ? 342 : 346,
2246+
GLFWKeyHelper.modifierShift => isLeft ? 340 : 344,
2247+
GLFWKeyHelper.modifierControl => isLeft ? 341 : 345,
2248+
GLFWKeyHelper.modifierMeta => isLeft ? 343 : 347,
2249+
GLFWKeyHelper.modifierNumericPad => 282,
2250+
GLFWKeyHelper.modifierCapsLock => 280,
2251+
_ => 65, // keyA
2252+
};
22602253
}
22612254

22622255
test('modifier keys are recognized individually', () {
@@ -2473,22 +2466,15 @@ void main() {
24732466

24742467
// How modifiers are interpreted depends upon the keyCode for GTK.
24752468
int keyCodeForModifier(int modifier, {required bool isLeft}) {
2476-
switch (modifier) {
2477-
case GtkKeyHelper.modifierMod1:
2478-
return 65513;
2479-
case GtkKeyHelper.modifierShift:
2480-
return isLeft ? 65505 : 65506;
2481-
case GtkKeyHelper.modifierControl:
2482-
return isLeft ? 65507 : 65508;
2483-
case GtkKeyHelper.modifierMeta:
2484-
return isLeft ? 65515 : 65516;
2485-
case GtkKeyHelper.modifierMod2:
2486-
return 65407;
2487-
case GtkKeyHelper.modifierCapsLock:
2488-
return 65509;
2489-
default:
2490-
return 65; // keyA
2491-
}
2469+
return switch (modifier) {
2470+
GtkKeyHelper.modifierShift => isLeft ? 65505 : 65506,
2471+
GtkKeyHelper.modifierControl => isLeft ? 65507 : 65508,
2472+
GtkKeyHelper.modifierMeta => isLeft ? 65515 : 65516,
2473+
GtkKeyHelper.modifierMod1 => 65513,
2474+
GtkKeyHelper.modifierMod2 => 65407,
2475+
GtkKeyHelper.modifierCapsLock => 65509,
2476+
_ => 65, // keyA
2477+
};
24922478
}
24932479

24942480
test('modifier keys are recognized individually', () {

packages/flutter/test/widgets/dismissible_test.dart

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -119,32 +119,22 @@ Future<void> dismissElement(WidgetTester tester, Finder finder, { required AxisD
119119
}
120120

121121
Future<void> dragElement(WidgetTester tester, Finder finder, { required AxisDirection gestureDirection, required double amount }) async {
122-
Offset delta;
123-
switch (gestureDirection) {
124-
case AxisDirection.left:
125-
delta = Offset(-amount, 0.0);
126-
case AxisDirection.right:
127-
delta = Offset(amount, 0.0);
128-
case AxisDirection.up:
129-
delta = Offset(0.0, -amount);
130-
case AxisDirection.down:
131-
delta = Offset(0.0, amount);
132-
}
122+
final Offset delta = switch (gestureDirection) {
123+
AxisDirection.left => Offset(-amount, 0.0),
124+
AxisDirection.right => Offset(amount, 0.0),
125+
AxisDirection.up => Offset(0.0, -amount),
126+
AxisDirection.down => Offset(0.0, amount),
127+
};
133128
await tester.drag(finder, delta);
134129
}
135130

136131
Future<void> flingElement(WidgetTester tester, Finder finder, { required AxisDirection gestureDirection, double initialOffsetFactor = 0.0 }) async {
137-
Offset delta;
138-
switch (gestureDirection) {
139-
case AxisDirection.left:
140-
delta = const Offset(-300.0, 0.0);
141-
case AxisDirection.right:
142-
delta = const Offset(300.0, 0.0);
143-
case AxisDirection.up:
144-
delta = const Offset(0.0, -300.0);
145-
case AxisDirection.down:
146-
delta = const Offset(0.0, 300.0);
147-
}
132+
final Offset delta = switch (gestureDirection) {
133+
AxisDirection.left => const Offset(-300, 0.0),
134+
AxisDirection.right => const Offset(300, 0.0),
135+
AxisDirection.up => const Offset(0.0, -300),
136+
AxisDirection.down => const Offset(0.0, 300),
137+
};
148138
await tester.fling(finder, delta, 1000.0, initialOffset: delta * initialOffsetFactor);
149139
}
150140

@@ -213,17 +203,12 @@ Future<void> checkFlingItemAfterMovement(
213203
}
214204

215205
Future<void> rollbackElement(WidgetTester tester, Finder finder, { required AxisDirection gestureDirection, double initialOffsetFactor = 0.0 }) async {
216-
Offset delta;
217-
switch (gestureDirection) {
218-
case AxisDirection.left:
219-
delta = const Offset(-30.0, 0.0);
220-
case AxisDirection.right:
221-
delta = const Offset(30.0, 0.0);
222-
case AxisDirection.up:
223-
delta = const Offset(0.0, -30.0);
224-
case AxisDirection.down:
225-
delta = const Offset(0.0, 30.0);
226-
}
206+
final Offset delta = switch (gestureDirection) {
207+
AxisDirection.left => const Offset(-30.0, 0.0),
208+
AxisDirection.right => const Offset(30.0, 0.0),
209+
AxisDirection.up => const Offset(0.0, -30.0),
210+
AxisDirection.down => const Offset(0.0, 30.0),
211+
};
227212
await tester.fling(finder, delta, 1000.0, initialOffset: delta * initialOffsetFactor);
228213
}
229214

packages/flutter/test/widgets/html_element_view_test.dart

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -442,13 +442,11 @@ class FakePlatformViewRegistry implements ui_web.PlatformViewRegistry {
442442
}
443443

444444
Future<dynamic> _onMethodCall(MethodCall call) {
445-
switch (call.method) {
446-
case 'create':
447-
return _create(call);
448-
case 'dispose':
449-
return _dispose(call);
450-
}
451-
return Future<dynamic>.sync(() => null);
445+
return switch (call.method) {
446+
'create' => _create(call),
447+
'dispose' => _dispose(call),
448+
_ => Future<dynamic>.sync(() => null),
449+
};
452450
}
453451

454452
Future<dynamic> _create(MethodCall call) async {

packages/flutter/test/widgets/image_resolution_test.dart

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,16 @@ class TestAssetBundle extends CachingAssetBundle {
3838

3939
@override
4040
Future<ByteData> load(String key) {
41-
late ByteData data;
42-
switch (key) {
43-
case 'AssetManifest.bin':
44-
data = manifest;
45-
case 'assets/image.png':
46-
data = testByteData(1.0);
47-
case 'assets/1.0x/image.png':
48-
data = testByteData(10.0); // see "...with a main asset and a 1.0x asset"
49-
case 'assets/1.5x/image.png':
50-
data = testByteData(1.5);
51-
case 'assets/2.0x/image.png':
52-
data = testByteData(2.0);
53-
case 'assets/3.0x/image.png':
54-
data = testByteData(3.0);
55-
case 'assets/4.0x/image.png':
56-
data = testByteData(4.0);
57-
}
41+
final ByteData data = switch (key) {
42+
'AssetManifest.bin' => manifest,
43+
'assets/image.png' => testByteData(1.0),
44+
'assets/1.0x/image.png' => testByteData(10.0), // see "...with a main asset and a 1.0x asset"
45+
'assets/1.5x/image.png' => testByteData(1.5),
46+
'assets/2.0x/image.png' => testByteData(2.0),
47+
'assets/3.0x/image.png' => testByteData(3.0),
48+
'assets/4.0x/image.png' => testByteData(4.0),
49+
_ => throw ArgumentError('Unexpected key: $key'),
50+
};
5851
return SynchronousFuture<ByteData>(data);
5952
}
6053

packages/flutter/test/widgets/page_forward_transitions_test.dart

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,12 +199,10 @@ void main() {
199199
testWidgets('Check onstage/offstage handling of barriers around transitions', (WidgetTester tester) async {
200200
await tester.pumpWidget(
201201
MaterialApp(
202-
onGenerateRoute: (RouteSettings settings) {
203-
switch (settings.name) {
204-
case '/': return TestRoute<void>(settings: settings, child: const Text('A'));
205-
case '/1': return TestRoute<void>(settings: settings, barrierColor: const Color(0xFFFFFF00), child: const Text('B'));
206-
}
207-
return null;
202+
onGenerateRoute: (RouteSettings settings) => switch (settings.name) {
203+
'/' => TestRoute<void>(settings: settings, child: const Text('A')),
204+
'/1' => TestRoute<void>(settings: settings, barrierColor: const Color(0xFFFFFF00), child: const Text('B')),
205+
_ => null,
208206
},
209207
),
210208
);

packages/flutter/test/widgets/render_object_widget_test.dart

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,10 @@ class TestOrientedBox extends SingleChildRenderObjectWidget {
2626
const TestOrientedBox({ super.key, super.child });
2727

2828
Decoration _getDecoration(BuildContext context) {
29-
final Orientation orientation = MediaQuery.orientationOf(context);
30-
switch (orientation) {
31-
case Orientation.landscape:
32-
return const BoxDecoration(color: Color(0xFF00FF00));
33-
case Orientation.portrait:
34-
return const BoxDecoration(color: Color(0xFF0000FF));
35-
}
29+
return switch (MediaQuery.orientationOf(context)) {
30+
Orientation.landscape => const BoxDecoration(color: Color(0xFF00FF00)),
31+
Orientation.portrait => const BoxDecoration(color: Color(0xFF0000FF)),
32+
};
3633
}
3734

3835
@override

0 commit comments

Comments
 (0)