diff --git a/dev/bots/test/prepare_package_test.dart b/dev/bots/test/prepare_package_test.dart index b26f410ae8..126a890561 100644 --- a/dev/bots/test/prepare_package_test.dart +++ b/dev/bots/test/prepare_package_test.dart @@ -24,14 +24,15 @@ void main() { return processRunner.runProcess(commandLine); })(['this_executable_better_not_exist_2857632534321']), throwsA(isA())); - try { - await processRunner.runProcess(['this_executable_better_not_exist_2857632534321']); - } on PreparePackageException catch (e) { - expect( - e.message, + + await expectLater( + () => processRunner.runProcess(['this_executable_better_not_exist_2857632534321']), + throwsA(isA().having( + (PreparePackageException error) => error.message, + 'message', contains('ProcessException: Failed to find "this_executable_better_not_exist_2857632534321" in the search path'), - ); - } + )), + ); }); for (final String platformName in ['macos', 'linux', 'windows']) { final FakePlatform platform = FakePlatform( diff --git a/dev/integration_tests/ui/test_driver/driver_test.dart b/dev/integration_tests/ui/test_driver/driver_test.dart index 8919b427aa..2f8c96672f 100644 --- a/dev/integration_tests/ui/test_driver/driver_test.dart +++ b/dev/integration_tests/ui/test_driver/driver_test.dart @@ -26,12 +26,14 @@ void main() { }); test('waitForAbsent should time out waiting for text "present" to disappear', () async { - try { - await driver.waitForAbsent(presentText, timeout: const Duration(seconds: 1)); - fail('expected DriverError'); - } on DriverError catch (error) { - expect(error.message, contains('Timeout while executing waitForAbsent')); - } + await expectLater( + () => driver.waitForAbsent(presentText, timeout: const Duration(seconds: 1)), + throwsA(isA().having( + (DriverError error) => error.message, + 'message', + contains('Timeout while executing waitForAbsent'), + )), + ); }); test('waitForAbsent should resolve when text "present" disappears', () async { @@ -51,12 +53,14 @@ void main() { }); test('waitFor times out waiting for "present" to reappear', () async { - try { - await driver.waitFor(presentText, timeout: const Duration(seconds: 1)); - fail('expected DriverError'); - } on DriverError catch (error) { - expect(error.message, contains('Timeout while executing waitFor')); - } + await expectLater( + () => driver.waitFor(presentText, timeout: const Duration(seconds: 1)), + throwsA(isA().having( + (DriverError error) => error.message, + 'message', + contains('Timeout while executing waitFor'), + )), + ); }); test('waitFor should resolve when text "present" reappears', () async { diff --git a/packages/flutter/test/cupertino/bottom_tab_bar_test.dart b/packages/flutter/test/cupertino/bottom_tab_bar_test.dart index f768ea09e2..a7dc8b8630 100644 --- a/packages/flutter/test/cupertino/bottom_tab_bar_test.dart +++ b/packages/flutter/test/cupertino/bottom_tab_bar_test.dart @@ -30,20 +30,21 @@ Future pumpWidgetWithBoilerplate(WidgetTester tester, Widget widget) async Future main() async { testWidgets('Need at least 2 tabs', (WidgetTester tester) async { - try { - await pumpWidgetWithBoilerplate(tester, CupertinoTabBar( + await expectLater( + () => pumpWidgetWithBoilerplate(tester, CupertinoTabBar( items: [ BottomNavigationBarItem( icon: ImageIcon(MemoryImage(Uint8List.fromList(kTransparentImage))), label: 'Tab 1', ), ], - )); - fail('Should not be possible to create a tab bar with just one item'); - } on AssertionError catch (e) { - expect(e.toString(), contains('items.length')); - // Exception expected. - } + )), + throwsA(isA().having( + (AssertionError error) => error.toString(), + '.toString()', + contains('items.length'), + )), + ); }); testWidgets('Active and inactive colors', (WidgetTester tester) async { diff --git a/packages/flutter/test/cupertino/segmented_control_test.dart b/packages/flutter/test/cupertino/segmented_control_test.dart index 065a978b4e..d68a34c1f7 100644 --- a/packages/flutter/test/cupertino/segmented_control_test.dart +++ b/packages/flutter/test/cupertino/segmented_control_test.dart @@ -85,35 +85,37 @@ void main() { }); testWidgets('Need at least 2 children', (WidgetTester tester) async { - final Map children = {}; - try { - await tester.pumpWidget( + await expectLater( + () => tester.pumpWidget( boilerplate( child: CupertinoSegmentedControl( - children: children, + children: const {}, onValueChanged: (int newValue) { }, ), ), - ); - fail('Should not be possible to create a segmented control with no children'); - } on AssertionError catch (e) { - expect(e.toString(), contains('children.length')); - } - try { - children[0] = const Text('Child 1'); + ), + throwsA(isA().having( + (AssertionError error) => error.toString(), + '.toString()', + contains('children.length'), + )), + ); - await tester.pumpWidget( + await expectLater( + () => tester.pumpWidget( boilerplate( child: CupertinoSegmentedControl( - children: children, + children: const {0: Text('Child 1')}, onValueChanged: (int newValue) { }, ), ), - ); - fail('Should not be possible to create a segmented control with just one child'); - } on AssertionError catch (e) { - expect(e.toString(), contains('children.length')); - } + ), + throwsA(isA().having( + (AssertionError error) => error.toString(), + '.toString()', + contains('children.length'), + )), + ); }); testWidgets('Padding works', (WidgetTester tester) async { @@ -208,8 +210,8 @@ void main() { children[0] = const Text('Child 1'); children[1] = const Text('Child 2'); - try { - await tester.pumpWidget( + await expectLater( + () => tester.pumpWidget( boilerplate( child: CupertinoSegmentedControl( children: children, @@ -217,14 +219,13 @@ void main() { groupValue: 2, ), ), - ); - fail( - 'Should not be possible to create segmented control in which ' - 'value is not the key of one of the children widgets', - ); - } on AssertionError catch (e) { - expect(e.toString(), contains('children')); - } + ), + throwsA(isA().having( + (AssertionError error) => error.toString(), + '.toString()', + contains('children'), + )), + ); }); testWidgets('Widgets have correct default text/icon styles, change correctly on selection', (WidgetTester tester) async { diff --git a/packages/flutter/test/cupertino/sliding_segmented_control_test.dart b/packages/flutter/test/cupertino/sliding_segmented_control_test.dart index f37ddcce47..eca9f7ac82 100644 --- a/packages/flutter/test/cupertino/sliding_segmented_control_test.dart +++ b/packages/flutter/test/cupertino/sliding_segmented_control_test.dart @@ -73,50 +73,56 @@ void main() { }); testWidgets('Need at least 2 children', (WidgetTester tester) async { - final Map children = {}; groupValue = null; - try { - await tester.pumpWidget( + await expectLater( + () => tester.pumpWidget( CupertinoSlidingSegmentedControl( - children: children, + children: const {}, groupValue: groupValue, onValueChanged: defaultCallback, ), - ); - fail('Should not be possible to create a segmented control with no children'); - } on AssertionError catch (e) { - expect(e.toString(), contains('children.length')); - } - try { - children[0] = const Text('Child 1'); + ), + throwsA(isA().having( + (AssertionError error) => error.toString(), + '.toString()', + contains('children.length'), + )), + ); - await tester.pumpWidget( + await expectLater( + () => tester.pumpWidget( CupertinoSlidingSegmentedControl( - children: children, + children: const {0: Text('Child 1')}, groupValue: groupValue, onValueChanged: defaultCallback, ), - ); - fail('Should not be possible to create a segmented control with just one child'); - } on AssertionError catch (e) { - expect(e.toString(), contains('children.length')); - } + ), + throwsA(isA().having( + (AssertionError error) => error.toString(), + '.toString()', + contains('children.length'), + )), + ); groupValue = -1; - try { - children[1] = const Text('Child 2'); - children[2] = const Text('Child 3'); - await tester.pumpWidget( + await expectLater( + () => tester.pumpWidget( CupertinoSlidingSegmentedControl( - children: children, + children: const { + 0: Text('Child 1'), + 1: Text('Child 2'), + 2: Text('Child 3'), + }, groupValue: groupValue, onValueChanged: defaultCallback, ), - ); - fail('Should not be possible to create a segmented control with a groupValue pointing to a non-existent child'); - } on AssertionError catch (e) { - expect(e.toString(), contains('groupValue must be either null or one of the keys in the children map')); - } + ), + throwsA(isA().having( + (AssertionError error) => error.toString(), + '.toString()', + contains('groupValue must be either null or one of the keys in the children map'), + )), + ); }); testWidgets('Padding works', (WidgetTester tester) async { diff --git a/packages/flutter/test/foundation/debug_test.dart b/packages/flutter/test/foundation/debug_test.dart index d5effab542..f089652355 100644 --- a/packages/flutter/test/foundation/debug_test.dart +++ b/packages/flutter/test/foundation/debug_test.dart @@ -37,16 +37,14 @@ void main() { }); test('returns failing future if action throws', () async { - try { - await debugInstrumentAction('throws', () async { + await expectLater( + () => debugInstrumentAction('throws', () async { await Future.delayed(Duration.zero); throw 'Error'; - }); - fail('Error expected but not thrown'); - } on String catch (error) { - expect(error, 'Error'); - expect(printBuffer.toString(), matches(r'^Action "throws" took .+')); - } + }), + throwsA('Error'), + ); + expect(printBuffer.toString(), matches(r'^Action "throws" took .+')); }); }); } diff --git a/packages/flutter/test/material/dropdown_form_field_test.dart b/packages/flutter/test/material/dropdown_form_field_test.dart index 61b008758a..b1f5d27ce2 100644 --- a/packages/flutter/test/material/dropdown_form_field_test.dart +++ b/packages/flutter/test/material/dropdown_form_field_test.dart @@ -576,8 +576,8 @@ void main() { ); }).toList(); - try { - await tester.pumpWidget( + await expectLater( + () => tester.pumpWidget( MaterialApp( home: Scaffold( body: DropdownButtonFormField( @@ -587,15 +587,13 @@ void main() { ), ), ), - ); - - fail('Should not be possible to have duplicate item value'); - } on AssertionError catch (error) { - expect( - error.toString(), + ), + throwsA(isA().having( + (AssertionError error) => error.toString(), + '.toString()', contains("There should be exactly one item with [DropdownButton]'s value"), - ); - } + )), + ); }); testWidgets('DropdownButtonFormField value should only appear in one menu item', (WidgetTester tester) async { @@ -607,8 +605,8 @@ void main() { ); }).toList(); - try { - await tester.pumpWidget( + await expectLater( + () => tester.pumpWidget( MaterialApp( home: Scaffold( body: DropdownButton( @@ -618,15 +616,13 @@ void main() { ), ), ), - ); - - fail('Should not be possible to have no items with passed in value'); - } on AssertionError catch (error) { - expect( - error.toString(), + ), + throwsA(isA().having( + (AssertionError error) => error.toString(), + '.toString()', contains("There should be exactly one item with [DropdownButton]'s value"), - ); - } + )), + ); }); testWidgets('DropdownButtonFormField - selectedItemBuilder builds custom buttons', (WidgetTester tester) async { diff --git a/packages/flutter/test/material/dropdown_test.dart b/packages/flutter/test/material/dropdown_test.dart index f2f292e88c..c9d7d0abfa 100644 --- a/packages/flutter/test/material/dropdown_test.dart +++ b/packages/flutter/test/material/dropdown_test.dart @@ -445,8 +445,8 @@ void main() { ); }).toList(); - try { - await tester.pumpWidget( + await expectLater( + () => tester.pumpWidget( MaterialApp( home: Scaffold( body: DropdownButton( @@ -456,15 +456,13 @@ void main() { ), ), ), - ); - - fail('Should not be possible to have duplicate item value'); - } on AssertionError catch (error) { - expect( - error.toString(), + ), + throwsA(isA().having( + (AssertionError error) => error.toString(), + '.toString()', contains("There should be exactly one item with [DropdownButton]'s value"), - ); - } + )), + ); }); testWidgets('DropdownButton value should only appear in one menu item', (WidgetTester tester) async { @@ -476,8 +474,8 @@ void main() { ); }).toList(); - try { - await tester.pumpWidget( + await expectLater( + () => tester.pumpWidget( MaterialApp( home: Scaffold( body: DropdownButton( @@ -487,15 +485,13 @@ void main() { ), ), ), - ); - - fail('Should not be possible to have no items with passed in value'); - } on AssertionError catch (error) { - expect( - error.toString(), + ), + throwsA(isA().having( + (AssertionError error) => error.toString(), + '.toString()', contains("There should be exactly one item with [DropdownButton]'s value"), - ); - } + )), + ); }); testWidgets('Dropdown form field uses form field state', (WidgetTester tester) async { diff --git a/packages/flutter/test/material/toggle_buttons_test.dart b/packages/flutter/test/material/toggle_buttons_test.dart index d68fb2f84d..7bfe2903e0 100644 --- a/packages/flutter/test/material/toggle_buttons_test.dart +++ b/packages/flutter/test/material/toggle_buttons_test.dart @@ -171,8 +171,8 @@ void main() { testWidgets( 'children and isSelected properties have to be the same length', (WidgetTester tester) async { - try { - await tester.pumpWidget( + await expectLater( + () => tester.pumpWidget( Material( child: boilerplate( child: ToggleButtons( @@ -184,15 +184,16 @@ void main() { ), ), ), - ); - fail( - 'Should not be possible to create a toggle button with mismatching ' - 'children.length and isSelected.length.', - ); - } on AssertionError catch (e) { - expect(e.toString(), contains('children.length')); - expect(e.toString(), contains('isSelected.length')); - } + ), + throwsA(isA().having( + (AssertionError error) => error.toString(), + '.toString()', + allOf( + contains('children.length'), + contains('isSelected.length'), + ), + )), + ); }, ); diff --git a/packages/flutter/test/rendering/box_test.dart b/packages/flutter/test/rendering/box_test.dart index 7357ab462a..8737c99082 100644 --- a/packages/flutter/test/rendering/box_test.dart +++ b/packages/flutter/test/rendering/box_test.dart @@ -968,30 +968,41 @@ void main() { ); expect(isHit, isTrue); expect(ran, isTrue); + isHit = false; ran = false; - try { - isHit = result.addWithOutOfBandPosition( - paintTransform: MatrixUtils.forceToPoint(Offset.zero), // cannot be inverted - hitTest: (BoxHitTestResult result) { - fail('non-invertible transform should be caught'); - }, - ); - fail('no exception thrown'); - } on AssertionError catch (e) { - expect(e.message, 'paintTransform must be invertible.'); - } + expect( + () { + isHit = result.addWithOutOfBandPosition( + paintTransform: MatrixUtils.forceToPoint(Offset.zero), // cannot be inverted + hitTest: (BoxHitTestResult result) { + fail('non-invertible transform should be caught'); + }, + ); + }, + throwsA(isA().having( + (AssertionError error) => error.message, + 'message', + 'paintTransform must be invertible.', + )), + ); + expect(isHit, isFalse); - try { - isHit = result.addWithOutOfBandPosition( - hitTest: (BoxHitTestResult result) { - fail('addWithOutOfBandPosition should need some transformation of some sort'); - }, - ); - fail('no exception thrown'); - } on AssertionError catch (e) { - expect(e.message, 'Exactly one transform or offset argument must be provided.'); - } + expect( + () { + isHit = result.addWithOutOfBandPosition( + hitTest: (BoxHitTestResult result) { + fail('addWithOutOfBandPosition should need some transformation of some sort'); + }, + ); + }, + throwsA(isA().having( + (AssertionError error) => error.message, + 'message', + 'Exactly one transform or offset argument must be provided.', + )), + ); + expect(isHit, isFalse); }); test('error message', () { diff --git a/packages/flutter/test/services/restoration_bucket_test.dart b/packages/flutter/test/services/restoration_bucket_test.dart index 58ef32e16f..1f5c7bb41a 100644 --- a/packages/flutter/test/services/restoration_bucket_test.dart +++ b/packages/flutter/test/services/restoration_bucket_test.dart @@ -181,19 +181,20 @@ void main() { expect(child2.read('foo'), isNull); // Value does not exist in this child. // child1 is not given up before running finalizers. - try { - manager.doSerialization(); - fail('expected error'); - } on FlutterError catch (e) { - expect( - e.message, - 'Multiple owners claimed child RestorationBuckets with the same IDs.\n' - 'The following IDs were claimed multiple times from the parent RestorationBucket(restorationId: root, owner: MockManager):\n' - ' * "child1" was claimed by:\n' - ' * SecondClaim\n' - ' * FirstClaim (current owner)', - ); - } + expect( + () => manager.doSerialization(), + throwsA(isA().having( + (FlutterError error) => error.message, + 'message', + equals( + 'Multiple owners claimed child RestorationBuckets with the same IDs.\n' + 'The following IDs were claimed multiple times from the parent RestorationBucket(restorationId: root, owner: MockManager):\n' + ' * "child1" was claimed by:\n' + ' * SecondClaim\n' + ' * FirstClaim (current owner)', + ), + )), + ); }); test('claim child that is already claimed does not throw if given up', () { diff --git a/packages/flutter/test/widgets/list_wheel_scroll_view_test.dart b/packages/flutter/test/widgets/list_wheel_scroll_view_test.dart index ff65f49d91..f1b8fea54a 100644 --- a/packages/flutter/test/widgets/list_wheel_scroll_view_test.dart +++ b/packages/flutter/test/widgets/list_wheel_scroll_view_test.dart @@ -50,16 +50,18 @@ void main() { group('construction check', () { testWidgets('ListWheelScrollView needs positive diameter ratio', (WidgetTester tester) async { - try { - ListWheelScrollView( + expect( + () => ListWheelScrollView( diameterRatio: nonconst(-2.0), itemExtent: 20.0, children: const [], - ); - fail('Expected failure with negative diameterRatio'); - } on AssertionError catch (exception) { - expect(exception.message, contains("You can't set a diameterRatio of 0")); - } + ), + throwsA(isA().having( + (AssertionError error) => error.message, + 'message', + contains("You can't set a diameterRatio of 0"), + )), + ); }); testWidgets('ListWheelScrollView can have zero child', (WidgetTester tester) async { diff --git a/packages/flutter/test/widgets/page_view_test.dart b/packages/flutter/test/widgets/page_view_test.dart index 1257db136e..9973b8d0d8 100644 --- a/packages/flutter/test/widgets/page_view_test.dart +++ b/packages/flutter/test/widgets/page_view_test.dart @@ -756,15 +756,14 @@ void main() { testWidgets('PageView can restore page', (WidgetTester tester) async { final PageController controller = PageController(); - try { - controller.page; - fail('Accessing page before attaching should fail.'); - } on AssertionError catch (e) { - expect( - e.message, - 'PageController.page cannot be accessed before a PageView is built with it.', - ); - } + expect( + () => controller.page, + throwsA(isA().having( + (AssertionError error) => error.message, + 'message', + equals('PageController.page cannot be accessed before a PageView is built with it.'), + )), + ); final PageStorageBucket bucket = PageStorageBucket(); await tester.pumpWidget(Directionality( textDirection: TextDirection.ltr, @@ -791,15 +790,14 @@ void main() { child: Container(), ), ); - try { - controller.page; - fail('Accessing page after detaching all PageViews should fail.'); - } on AssertionError catch (e) { - expect( - e.message, - 'PageController.page cannot be accessed before a PageView is built with it.', - ); - } + expect( + () => controller.page, + throwsA(isA().having( + (AssertionError error) => error.message, + 'message', + equals('PageController.page cannot be accessed before a PageView is built with it.'), + )), + ); await tester.pumpWidget(Directionality( textDirection: TextDirection.ltr, child: PageStorage( diff --git a/packages/flutter_driver/test/src/real_tests/flutter_driver_test.dart b/packages/flutter_driver/test/src/real_tests/flutter_driver_test.dart index d10aab65db..c2954be054 100644 --- a/packages/flutter_driver/test/src/real_tests/flutter_driver_test.dart +++ b/packages/flutter_driver/test/src/real_tests/flutter_driver_test.dart @@ -531,13 +531,14 @@ void main() { fakeClient.responses['waitFor'] = makeFakeResponse({ 'message': 'This is a failure', }, isError: true); - try { - await driver.waitFor(find.byTooltip('foo')); - fail('expected an exception'); - } catch (error) { - expect(error, isA()); - expect((error as DriverError).message, 'Error in Flutter application: {message: This is a failure}'); - } + await expectLater( + () => driver.waitFor(find.byTooltip('foo')), + throwsA(isA().having( + (DriverError error) => error.message, + 'message', + 'Error in Flutter application: {message: This is a failure}', + )), + ); }); test('uncaught remote error', () async { diff --git a/packages/flutter_tools/test/commands.shard/hermetic/attach_test.dart b/packages/flutter_tools/test/commands.shard/hermetic/attach_test.dart index 95632f942c..af7304cb03 100644 --- a/packages/flutter_tools/test/commands.shard/hermetic/attach_test.dart +++ b/packages/flutter_tools/test/commands.shard/hermetic/attach_test.dart @@ -819,12 +819,10 @@ class LoggerInterrupted implements Exception { Future expectLoggerInterruptEndsTask(Future task, StreamLogger logger) async { logger.interrupt(); // an exception during the task should cause it to fail... - try { - await task; - expect(false, isTrue); // (shouldn't reach here) - } on ToolExit catch (error) { - expect(error.exitCode, 2); // ...with exit code 2. - } + await expectLater( + () => task, + throwsA(isA().having((ToolExit error) => error.exitCode, 'exitCode', 2)), + ); } VMServiceConnector getFakeVmServiceFactory({ diff --git a/packages/flutter_tools/test/commands.shard/hermetic/logs_test.dart b/packages/flutter_tools/test/commands.shard/hermetic/logs_test.dart index c8ab807094..c6550bdffa 100644 --- a/packages/flutter_tools/test/commands.shard/hermetic/logs_test.dart +++ b/packages/flutter_tools/test/commands.shard/hermetic/logs_test.dart @@ -23,12 +23,10 @@ void main() { testUsingContext('fail with a bad device id', () async { final LogsCommand command = LogsCommand(); - try { - await createTestCommandRunner(command).run(['-d', 'abc123', 'logs']); - fail('Expect exception'); - } on ToolExit catch (e) { - expect(e.exitCode ?? 1, 1); - } + await expectLater( + () => createTestCommandRunner(command).run(['-d', 'abc123', 'logs']), + throwsA(isA().having((ToolExit error) => error.exitCode, 'exitCode', anyOf(isNull, 1))), + ); }); }); } diff --git a/packages/flutter_tools/test/commands.shard/hermetic/run_test.dart b/packages/flutter_tools/test/commands.shard/hermetic/run_test.dart index e94973c1da..5854c81c02 100644 --- a/packages/flutter_tools/test/commands.shard/hermetic/run_test.dart +++ b/packages/flutter_tools/test/commands.shard/hermetic/run_test.dart @@ -51,12 +51,10 @@ void main() { testUsingContext('fails when target not found', () async { final RunCommand command = RunCommand(); - try { - await createTestCommandRunner(command).run(['run', '-t', 'abc123', '--no-pub']); - fail('Expect exception'); - } on ToolExit catch (e) { - expect(e.exitCode ?? 1, 1); - } + expect( + () => createTestCommandRunner(command).run(['run', '-t', 'abc123', '--no-pub']), + throwsA(isA().having((ToolExit error) => error.exitCode, 'exitCode', anyOf(isNull, 1))), + ); }, overrides: { FileSystem: () => fileSystem, ProcessManager: () => FakeProcessManager.any(), @@ -69,18 +67,20 @@ void main() { fileSystem.file('.packages').createSync(); final RunCommand command = RunCommand(); - try { - await createTestCommandRunner(command).run([ + await expectLater( + () => createTestCommandRunner(command).run([ 'run', '--use-application-binary=app/bar/faz', '--fast-start', '--no-pub', '--show-test-device', - ]); - fail('Expect exception'); - } on Exception catch (e) { - expect(e.toString(), isNot(contains('--fast-start is not supported with --use-application-binary'))); - } + ]), + throwsA(isA().having( + (Exception exception) => exception.toString(), + 'toString', + isNot(contains('--fast-start is not supported with --use-application-binary')), + )), + ); }, overrides: { FileSystem: () => fileSystem, ProcessManager: () => FakeProcessManager.any(), @@ -97,15 +97,13 @@ void main() { ..createSync(recursive: true); final RunCommand command = RunCommand(); - try { - await createTestCommandRunner(command).run([ + await expectLater( + () => createTestCommandRunner(command).run([ 'run', '--no-pub', - ]); - fail('Expect exception'); - } on Exception catch (e) { - expect(e, isA()); - } + ]), + throwsA(isA()), + ); final BufferLogger bufferLogger = globals.logger as BufferLogger; expect( bufferLogger.statusText, @@ -124,16 +122,17 @@ void main() { .createSync(recursive: true); final RunCommand command = RunCommand(); - try { - await createTestCommandRunner(command).run([ + await expectLater( + () => createTestCommandRunner(command).run([ 'run', '--no-pub', - ]); - fail('Expect exception'); - } on Exception catch (e) { - expect(e, isA()); - expect(e.toString(), contains('No pubspec.yaml file found')); - } + ]), + throwsA(isA().having( + (ToolExit error) => error.toString(), + 'toString()', + contains('No pubspec.yaml file found'), + )), + ); }, overrides: { FileSystem: () => fileSystem, ProcessManager: () => FakeProcessManager.any(), @@ -180,16 +179,14 @@ void main() { (Invocation invocation) => Future>.value(noDevices) ); - try { - await createTestCommandRunner(command).run([ + await expectLater( + () => createTestCommandRunner(command).run([ 'run', '--no-pub', '--no-hot', - ]); - fail('Expect exception'); - } on ToolExit catch (e) { - expect(e.message, null); - } + ]), + throwsA(isA().having((ToolExit error) => error.message, 'message', isNull)), + ); expect( testLogger.statusText, @@ -256,16 +253,14 @@ void main() { (Invocation invocation) => Future>.value([]), ); - try { - await createTestCommandRunner(command).run([ + await expectLater( + () => createTestCommandRunner(command).run([ 'run', '--no-pub', '--no-hot', - ]); - fail('Expect exception'); - } on ToolExit catch (e) { - expect(e.message, null); - } + ]), + throwsA(isA().having((ToolExit error) => error.message, 'message', isNull)), + ); expect( testLogger.statusText, diff --git a/packages/flutter_tools/test/commands.shard/hermetic/shell_completion_test.dart b/packages/flutter_tools/test/commands.shard/hermetic/shell_completion_test.dart index 5f45b6856e..d2fda26922 100644 --- a/packages/flutter_tools/test/commands.shard/hermetic/shell_completion_test.dart +++ b/packages/flutter_tools/test/commands.shard/hermetic/shell_completion_test.dart @@ -61,15 +61,16 @@ void main() { final ShellCompletionCommand command = ShellCompletionCommand(); const String outputFile = 'bash-setup.sh'; globals.fs.file(outputFile).createSync(); - try { - await createTestCommandRunner(command).run( + await expectLater( + () => createTestCommandRunner(command).run( ['bash-completion', outputFile], - ); - fail('Expect ToolExit exception'); - } on ToolExit catch (error) { - expect(error.exitCode ?? 1, 1); - expect(error.message, contains('Use --overwrite')); - } + ), + throwsA( + isA() + .having((ToolExit error) => error.exitCode, 'exitCode', anyOf(isNull, 1)) + .having((ToolExit error) => error.message, 'message', contains('Use --overwrite')), + ), + ); expect(globals.fs.isFileSync(outputFile), isTrue); expect(globals.fs.file(outputFile).readAsStringSync(), isEmpty); }, overrides: { diff --git a/packages/flutter_tools/test/general.shard/base/context_test.dart b/packages/flutter_tools/test/general.shard/base/context_test.dart index 1aebfce76b..630125e3ec 100644 --- a/packages/flutter_tools/test/general.shard/base/context_test.dart +++ b/packages/flutter_tools/test/general.shard/base/context_test.dart @@ -161,13 +161,18 @@ void main() { double: () => context.get()! * 1.0, }, ); - try { - await value; - fail('ContextDependencyCycleException expected but not thrown.'); - } on ContextDependencyCycleException catch (e) { - expect(e.cycle, [String, double, int]); - expect(e.toString(), 'Dependency cycle detected: String -> double -> int'); - } + expect( + () => value, + throwsA( + isA() + .having((ContextDependencyCycleException error) => error.cycle, 'cycle', [String, double, int]) + .having( + (ContextDependencyCycleException error) => error.toString(), + 'toString()', + 'Dependency cycle detected: String -> double -> int', + ), + ), + ); }); }); diff --git a/packages/flutter_tools/test/general.shard/base/process_test.dart b/packages/flutter_tools/test/general.shard/base/process_test.dart index 9249d86977..12fef9d22a 100644 --- a/packages/flutter_tools/test/general.shard/base/process_test.dart +++ b/packages/flutter_tools/test/general.shard/base/process_test.dart @@ -211,13 +211,14 @@ void main() { exitCode: 1, stderr: stderr, )); - try { - processUtils.runSync(['kaboom'], throwOnError: true); - fail('ProcessException expected.'); - } on ProcessException catch (e) { - expect(e, isA()); - expect(e.message.contains(stderr), false); - } + expect( + () => processUtils.runSync(['kaboom'], throwOnError: true), + throwsA(isA().having( + (ProcessException error) => error.message, + 'message', + isNot(contains(stderr)), + )), + ); }); testWithoutContext('throws with stderr in exception on failure with verboseExceptions', () async { diff --git a/packages/flutter_tools/test/general.shard/dart/pub_get_test.dart b/packages/flutter_tools/test/general.shard/dart/pub_get_test.dart index f0bbf753c3..c6a9617ecd 100644 --- a/packages/flutter_tools/test/general.shard/dart/pub_get_test.dart +++ b/packages/flutter_tools/test/general.shard/dart/pub_get_test.dart @@ -406,12 +406,10 @@ void main() { botDetector: const BotDetectorAlwaysNo(), processManager: processManager, ); - try { - await pub.get(context: PubContext.flutterTests); - throw AssertionError('pubGet did not fail'); - } on ToolExit catch (error) { - expect(error.message, 'pub get failed (66; err3)'); - } + await expectLater( + () => pub.get(context: PubContext.flutterTests), + throwsA(isA().having((ToolExit error) => error.message, 'message', 'pub get failed (66; err3)')), + ); expect(logger.statusText, 'Running "flutter pub get" in /...\n' 'out1\n' diff --git a/packages/flutter_tools/test/web.shard/expression_evaluation_web_test.dart b/packages/flutter_tools/test/web.shard/expression_evaluation_web_test.dart index f315f504cc..9c8fa68352 100644 --- a/packages/flutter_tools/test/web.shard/expression_evaluation_web_test.dart +++ b/packages/flutter_tools/test/web.shard/expression_evaluation_web_test.dart @@ -174,13 +174,14 @@ void main() { } Future failToEvaluateExpression(FlutterTestDriver flutter) async { - ObjRef res; - try { - res = await flutter.evaluateInFrame('"test"'); - } on RPCError catch (e) { - expect(e.message, contains('Expression evaluation is not supported for this configuration')); - } - expect(res, null); + await expectLater( + () => flutter.evaluateInFrame('"test"'), + throwsA(isA().having( + (RPCError error) => error.message, + 'message', + contains('Expression evaluation is not supported for this configuration'), + )), + ); } Future checkStaticScope(FlutterTestDriver flutter) async {