Unverified Commit cfc08594 authored by Alexandre Ardhuin's avatar Alexandre Ardhuin Committed by GitHub

enable lint use_test_throws_matchers (#83943)

parent f890e9ca
...@@ -228,6 +228,7 @@ linter: ...@@ -228,6 +228,7 @@ linter:
- use_rethrow_when_possible - use_rethrow_when_possible
# - use_setters_to_change_properties # not yet tested # - use_setters_to_change_properties # not yet tested
# - use_string_buffers # has false positives: https://github.com/dart-lang/sdk/issues/34182 # - use_string_buffers # has false positives: https://github.com/dart-lang/sdk/issues/34182
- use_test_throws_matchers
# - use_to_and_as_if_applicable # has false positives, so we prefer to catch this by code-review # - use_to_and_as_if_applicable # has false positives, so we prefer to catch this by code-review
- valid_regexps - valid_regexps
- void_checks - void_checks
...@@ -14,22 +14,18 @@ void main() { ...@@ -14,22 +14,18 @@ void main() {
end: Object(), end: Object(),
); );
FlutterError? error; expect(
try { () => objectTween.transform(0.1),
objectTween.transform(0.1); throwsA(isA<FlutterError>().having(
} on FlutterError catch (err) { (FlutterError error) => error.diagnostics.map((DiagnosticsNode node) => node.toString()),
error = err; 'diagnostics',
} <String>[
if (error == null) {
fail('Expected Tween.transform to throw a FlutterError');
}
expect(error.diagnostics.map((DiagnosticsNode node) => node.toString()), <String>[
'Cannot lerp between "Instance of \'Object\'" and "Instance of \'Object\'".', 'Cannot lerp between "Instance of \'Object\'" and "Instance of \'Object\'".',
'The type Object might not fully implement `+`, `-`, and/or `*`. $kApiDocsLink', 'The type Object might not fully implement `+`, `-`, and/or `*`. $kApiDocsLink',
'There may be a dedicated "ObjectTween" for this type, or you may need to create one.', 'There may be a dedicated "ObjectTween" for this type, or you may need to create one.',
]); ],
)),
);
}); });
test('throws flutter error when tweening types that do not fully satisfy tween requirements - Color', () { test('throws flutter error when tweening types that do not fully satisfy tween requirements - Color', () {
...@@ -38,22 +34,18 @@ void main() { ...@@ -38,22 +34,18 @@ void main() {
end: const Color(0xFFFFFFFF), end: const Color(0xFFFFFFFF),
); );
FlutterError? error; expect(
try { () => colorTween.transform(0.1),
colorTween.transform(0.1); throwsA(isA<FlutterError>().having(
} on FlutterError catch (err) { (FlutterError error) => error.diagnostics.map((DiagnosticsNode node) => node.toString()),
error = err; 'diagnostics',
} <String>[
if (error == null) {
fail('Expected Tween.transform to throw a FlutterError');
}
expect(error.diagnostics.map((DiagnosticsNode node) => node.toString()), <String>[
'Cannot lerp between "Color(0xff000000)" and "Color(0xffffffff)".', 'Cannot lerp between "Color(0xff000000)" and "Color(0xffffffff)".',
'The type Color might not fully implement `+`, `-`, and/or `*`. $kApiDocsLink', 'The type Color might not fully implement `+`, `-`, and/or `*`. $kApiDocsLink',
'To lerp colors, consider ColorTween instead.', 'To lerp colors, consider ColorTween instead.',
]); ],
)),
);
}); });
test('throws flutter error when tweening types that do not fully satisfy tween requirements - Rect', () { test('throws flutter error when tweening types that do not fully satisfy tween requirements - Rect', () {
...@@ -62,22 +54,18 @@ void main() { ...@@ -62,22 +54,18 @@ void main() {
end: const Rect.fromLTWH(2, 2, 2, 2), end: const Rect.fromLTWH(2, 2, 2, 2),
); );
FlutterError? error; expect(
try { () => rectTween.transform(0.1),
rectTween.transform(0.1); throwsA(isA<FlutterError>().having(
} on FlutterError catch (err) { (FlutterError error) => error.diagnostics.map((DiagnosticsNode node) => node.toString()),
error = err; 'diagnostics',
} <String>[
if (error == null) {
fail('Expected Tween.transform to throw a FlutterError');
}
expect(error.diagnostics.map((DiagnosticsNode node) => node.toString()), <String>[
'Cannot lerp between "Rect.fromLTRB(0.0, 0.0, 10.0, 10.0)" and "Rect.fromLTRB(2.0, 2.0, 4.0, 4.0)".', 'Cannot lerp between "Rect.fromLTRB(0.0, 0.0, 10.0, 10.0)" and "Rect.fromLTRB(2.0, 2.0, 4.0, 4.0)".',
'The type Rect might not fully implement `+`, `-`, and/or `*`. $kApiDocsLink', 'The type Rect might not fully implement `+`, `-`, and/or `*`. $kApiDocsLink',
'To lerp rects, consider RectTween instead.', 'To lerp rects, consider RectTween instead.',
]); ],
)),
);
}); });
test('throws flutter error when tweening types that do not fully satisfy tween requirements - int', () { test('throws flutter error when tweening types that do not fully satisfy tween requirements - int', () {
...@@ -86,22 +74,18 @@ void main() { ...@@ -86,22 +74,18 @@ void main() {
end: 1, end: 1,
); );
FlutterError? error; expect(
try { () => colorTween.transform(0.1),
colorTween.transform(0.1); throwsA(isA<FlutterError>().having(
} on FlutterError catch (err) { (FlutterError error) => error.diagnostics.map((DiagnosticsNode node) => node.toString()),
error = err; 'diagnostics',
} <String>[
if (error == null) {
fail('Expected Tween.transform to throw a FlutterError');
}
expect(error.diagnostics.map((DiagnosticsNode node) => node.toString()), <String>[
'Cannot lerp between "0" and "1".', 'Cannot lerp between "0" and "1".',
'The type int returned a double after multiplication with a double value. $kApiDocsLink', 'The type int returned a double after multiplication with a double value. $kApiDocsLink',
'To lerp int values, consider IntTween or StepTween instead.', 'To lerp int values, consider IntTween or StepTween instead.',
]); ],
)),
);
}); });
test('Can chain tweens', () { test('Can chain tweens', () {
......
...@@ -371,7 +371,8 @@ void main() { ...@@ -371,7 +371,8 @@ void main() {
}); });
testWidgets('CrossAxisAlignment.baseline is not allowed', (WidgetTester tester) async { testWidgets('CrossAxisAlignment.baseline is not allowed', (WidgetTester tester) async {
try { expect(
() {
MaterialApp( MaterialApp(
home: Material( home: Material(
child: ExpansionTile( child: ExpansionTile(
...@@ -381,14 +382,12 @@ void main() { ...@@ -381,14 +382,12 @@ void main() {
), ),
), ),
); );
} on AssertionError catch (error) { },
expect(error.toString(), contains( throwsA(isA<AssertionError>().having((AssertionError error) => error.toString(), '.toString()', contains(
'CrossAxisAlignment.baseline is not supported since the expanded' 'CrossAxisAlignment.baseline is not supported since the expanded'
' children are aligned in a column, not a row. Try to use another constant.', ' children are aligned in a column, not a row. Try to use another constant.',
)); ))),
return; );
}
fail('AssertionError was not thrown when expandedCrossAxisAlignment is CrossAxisAlignment.baseline.');
}); });
testWidgets('expandedCrossAxisAlignment and expandedAlignment default values', (WidgetTester tester) async { testWidgets('expandedCrossAxisAlignment and expandedAlignment default values', (WidgetTester tester) async {
......
...@@ -485,11 +485,10 @@ void main() { ...@@ -485,11 +485,10 @@ void main() {
), ),
), ),
); );
try { await expectLater(
await tester.pumpWidget(boilerplate); () => tester.pumpWidget(boilerplate),
} catch (e) { returnsNormally,
fail('Expected no error, but got $e'); );
}
}); });
group('Accessibility (a11y/Semantics)', () { group('Accessibility (a11y/Semantics)', () {
......
...@@ -128,16 +128,15 @@ void main() { ...@@ -128,16 +128,15 @@ void main() {
]); ]);
}, },
); );
try { expect(
await channel.invokeMethod<dynamic>('sayHello', 'hello'); () => channel.invokeMethod<dynamic>('sayHello', 'hello'),
fail('Exception expected'); throwsA(
} on PlatformException catch (e) { isA<PlatformException>()
expect(e.code, equals('bad')); .having((PlatformException e) => e.code, 'code', equals('bad'))
expect(e.message, equals('Something happened')); .having((PlatformException e) => e.message, 'message', equals('Something happened'))
expect(e.details, equals(<String, dynamic>{'a': 42, 'b': 3.14})); .having((PlatformException e) => e.details, 'details', equals(<String, dynamic>{'a': 42, 'b': 3.14})),
} catch (e) { ),
fail('PlatformException expected'); );
}
}); });
test('can invoke unimplemented method', () async { test('can invoke unimplemented method', () async {
...@@ -145,15 +144,16 @@ void main() { ...@@ -145,15 +144,16 @@ void main() {
'ch7', 'ch7',
(ByteData? message) async => null, (ByteData? message) async => null,
); );
try { expect(
await channel.invokeMethod<void>('sayHello', 'hello'); () => channel.invokeMethod<void>('sayHello', 'hello'),
fail('Exception expected'); throwsA(
} on MissingPluginException catch (e) { isA<MissingPluginException>()
expect(e.message, contains('sayHello')); .having((MissingPluginException e) => e.message, 'message', allOf(
expect(e.message, contains('ch7')); contains('sayHello'),
} catch (e) { contains('ch7'),
fail('MissingPluginException expected'); )),
} ),
);
}); });
test('can invoke unimplemented method (optional)', () async { test('can invoke unimplemented method (optional)', () async {
...@@ -218,15 +218,14 @@ void main() { ...@@ -218,15 +218,14 @@ void main() {
await TestDefaultBinaryMessengerBinding.instance!.defaultBinaryMessenger.handlePlatformMessage('ch7', call, (ByteData? result) { await TestDefaultBinaryMessengerBinding.instance!.defaultBinaryMessenger.handlePlatformMessage('ch7', call, (ByteData? result) {
envelope = result; envelope = result;
}); });
try { expect(
jsonMethod.decodeEnvelope(envelope!); () => jsonMethod.decodeEnvelope(envelope!),
fail('Exception expected'); throwsA(
} on PlatformException catch (e) { isA<PlatformException>()
expect(e.code, equals('bad')); .having((PlatformException e) => e.code, 'code', equals('bad'))
expect(e.message, equals('sayHello failed')); .having((PlatformException e) => e.message, 'message', equals('sayHello failed')),
} catch (e) { ),
fail('PlatformException expected'); );
}
}); });
test('can handle method call with other error result', () async { test('can handle method call with other error result', () async {
...@@ -238,15 +237,14 @@ void main() { ...@@ -238,15 +237,14 @@ void main() {
await TestDefaultBinaryMessengerBinding.instance!.defaultBinaryMessenger.handlePlatformMessage('ch7', call, (ByteData? result) { await TestDefaultBinaryMessengerBinding.instance!.defaultBinaryMessenger.handlePlatformMessage('ch7', call, (ByteData? result) {
envelope = result; envelope = result;
}); });
try { expect(
jsonMethod.decodeEnvelope(envelope!); () => jsonMethod.decodeEnvelope(envelope!),
fail('Exception expected'); throwsA(
} on PlatformException catch (e) { isA<PlatformException>()
expect(e.code, equals('error')); .having((PlatformException e) => e.code, 'code', equals('error'))
expect(e.message, equals('Invalid argument(s): bad')); .having((PlatformException e) => e.message, 'message', equals('Invalid argument(s): bad')),
} catch (e) { ),
fail('PlatformException expected'); );
}
}); });
test('can check the mock handler', () async { test('can check the mock handler', () async {
......
...@@ -686,16 +686,20 @@ void main() { ...@@ -686,16 +686,20 @@ void main() {
}; };
} }
try { expect(
() async {
await TestDefaultBinaryMessengerBinding.instance!.defaultBinaryMessenger.handlePlatformMessage( await TestDefaultBinaryMessengerBinding.instance!.defaultBinaryMessenger.handlePlatformMessage(
SystemChannels.keyEvent.name, SystemChannels.keyEvent.name,
SystemChannels.keyEvent.codec.encodeMessage(keyEventMessage), SystemChannels.keyEvent.codec.encodeMessage(keyEventMessage),
(ByteData? data) { }, (ByteData? data) { },
); );
fail('Expected an exception, but did not get one.'); },
} on AssertionError catch (error) { throwsA(isA<AssertionError>().having(
expect(error.toString(), contains('Attempted to send a key down event when no keys are in keysPressed')); (AssertionError error) => error.toString(),
} '.toString()',
contains('Attempted to send a key down event when no keys are in keysPressed'),
)),
);
}); });
}); });
......
...@@ -1303,7 +1303,8 @@ void main() { ...@@ -1303,7 +1303,8 @@ void main() {
}); });
testWidgets('minLines cannot be greater than maxLines', (WidgetTester tester) async { testWidgets('minLines cannot be greater than maxLines', (WidgetTester tester) async {
try { expect(
() async {
await tester.pumpWidget( await tester.pumpWidget(
overlay( overlay(
child: SizedBox( child: SizedBox(
...@@ -1316,11 +1317,13 @@ void main() { ...@@ -1316,11 +1317,13 @@ void main() {
), ),
), ),
); );
} on AssertionError catch (e) { },
expect(e.toString(), contains("minLines can't be greater than maxLines")); throwsA(isA<AssertionError>().having(
return; (AssertionError error) => error.toString(),
} '.toString()',
fail('An assert should be triggered when minLines is greater than maxLines'); contains("minLines can't be greater than maxLines"),
)),
);
}); });
testWidgets('Selectable height with minLine', (WidgetTester tester) async { testWidgets('Selectable height with minLine', (WidgetTester tester) async {
......
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