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>[
'Cannot lerp between "Instance of \'Object\'" and "Instance of \'Object\'".',
if (error == null) { 'The type Object might not fully implement `+`, `-`, and/or `*`. $kApiDocsLink',
fail('Expected Tween.transform to throw a FlutterError'); 'There may be a dedicated "ObjectTween" for this type, or you may need to create one.',
} ],
)),
expect(error.diagnostics.map((DiagnosticsNode node) => node.toString()), <String>[ );
'Cannot lerp between "Instance of \'Object\'" and "Instance of \'Object\'".',
'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.',
]);
}); });
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>[
'Cannot lerp between "Color(0xff000000)" and "Color(0xffffffff)".',
if (error == null) { 'The type Color might not fully implement `+`, `-`, and/or `*`. $kApiDocsLink',
fail('Expected Tween.transform to throw a FlutterError'); 'To lerp colors, consider ColorTween instead.',
} ],
)),
expect(error.diagnostics.map((DiagnosticsNode node) => node.toString()), <String>[ );
'Cannot lerp between "Color(0xff000000)" and "Color(0xffffffff)".',
'The type Color might not fully implement `+`, `-`, and/or `*`. $kApiDocsLink',
'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>[
'Cannot lerp between "Rect.fromLTRB(0.0, 0.0, 10.0, 10.0)" and "Rect.fromLTRB(2.0, 2.0, 4.0, 4.0)".',
if (error == null) { 'The type Rect might not fully implement `+`, `-`, and/or `*`. $kApiDocsLink',
fail('Expected Tween.transform to throw a FlutterError'); 'To lerp rects, consider RectTween instead.',
} ],
)),
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)".',
'The type Rect might not fully implement `+`, `-`, and/or `*`. $kApiDocsLink',
'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>[
'Cannot lerp between "0" and "1".',
if (error == null) { 'The type int returned a double after multiplication with a double value. $kApiDocsLink',
fail('Expected Tween.transform to throw a FlutterError'); 'To lerp int values, consider IntTween or StepTween instead.',
} ],
)),
expect(error.diagnostics.map((DiagnosticsNode node) => node.toString()), <String>[ );
'Cannot lerp between "0" and "1".',
'The type int returned a double after multiplication with a double value. $kApiDocsLink',
'To lerp int values, consider IntTween or StepTween instead.',
]);
}); });
test('Can chain tweens', () { test('Can chain tweens', () {
......
...@@ -371,24 +371,23 @@ void main() { ...@@ -371,24 +371,23 @@ void main() {
}); });
testWidgets('CrossAxisAlignment.baseline is not allowed', (WidgetTester tester) async { testWidgets('CrossAxisAlignment.baseline is not allowed', (WidgetTester tester) async {
try { expect(
MaterialApp( () {
home: Material( MaterialApp(
child: ExpansionTile( home: Material(
initiallyExpanded: true, child: ExpansionTile(
title: const Text('title'), initiallyExpanded: true,
expandedCrossAxisAlignment: CrossAxisAlignment.baseline, title: const Text('title'),
expandedCrossAxisAlignment: CrossAxisAlignment.baseline,
),
), ),
), );
); },
} on AssertionError catch (error) { throwsA(isA<AssertionError>().having((AssertionError error) => error.toString(), '.toString()', contains(
expect(error.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(
await TestDefaultBinaryMessengerBinding.instance!.defaultBinaryMessenger.handlePlatformMessage( () async {
SystemChannels.keyEvent.name, await TestDefaultBinaryMessengerBinding.instance!.defaultBinaryMessenger.handlePlatformMessage(
SystemChannels.keyEvent.codec.encodeMessage(keyEventMessage), SystemChannels.keyEvent.name,
(ByteData? data) { }, SystemChannels.keyEvent.codec.encodeMessage(keyEventMessage),
); (ByteData? data) { },
fail('Expected an exception, but did not get one.'); );
} on AssertionError catch (error) { },
expect(error.toString(), contains('Attempted to send a key down event when no keys are in keysPressed')); throwsA(isA<AssertionError>().having(
} (AssertionError error) => error.toString(),
'.toString()',
contains('Attempted to send a key down event when no keys are in keysPressed'),
)),
);
}); });
}); });
......
...@@ -1303,24 +1303,27 @@ void main() { ...@@ -1303,24 +1303,27 @@ void main() {
}); });
testWidgets('minLines cannot be greater than maxLines', (WidgetTester tester) async { testWidgets('minLines cannot be greater than maxLines', (WidgetTester tester) async {
try { expect(
await tester.pumpWidget( () async {
overlay( await tester.pumpWidget(
child: SizedBox( overlay(
width: 300.0, child: SizedBox(
child: SelectableText( width: 300.0,
'abcd', child: SelectableText(
minLines: 4, 'abcd',
maxLines: 3, minLines: 4,
maxLines: 3,
),
), ),
), ),
), );
); },
} on AssertionError catch (e) { throwsA(isA<AssertionError>().having(
expect(e.toString(), contains("minLines can't be greater than maxLines")); (AssertionError error) => error.toString(),
return; '.toString()',
} contains("minLines can't be greater than maxLines"),
fail('An assert should be triggered when minLines is 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