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:
- use_rethrow_when_possible
# - use_setters_to_change_properties # not yet tested
# - 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
- valid_regexps
- void_checks
......@@ -14,22 +14,18 @@ void main() {
end: Object(),
);
FlutterError? error;
try {
objectTween.transform(0.1);
} on FlutterError catch (err) {
error = err;
}
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\'".',
'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.',
]);
expect(
() => objectTween.transform(0.1),
throwsA(isA<FlutterError>().having(
(FlutterError error) => error.diagnostics.map((DiagnosticsNode node) => node.toString()),
'diagnostics',
<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', () {
......@@ -38,22 +34,18 @@ void main() {
end: const Color(0xFFFFFFFF),
);
FlutterError? error;
try {
colorTween.transform(0.1);
} on FlutterError catch (err) {
error = err;
}
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)".',
'The type Color might not fully implement `+`, `-`, and/or `*`. $kApiDocsLink',
'To lerp colors, consider ColorTween instead.',
]);
expect(
() => colorTween.transform(0.1),
throwsA(isA<FlutterError>().having(
(FlutterError error) => error.diagnostics.map((DiagnosticsNode node) => node.toString()),
'diagnostics',
<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', () {
......@@ -62,22 +54,18 @@ void main() {
end: const Rect.fromLTWH(2, 2, 2, 2),
);
FlutterError? error;
try {
rectTween.transform(0.1);
} on FlutterError catch (err) {
error = err;
}
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)".',
'The type Rect might not fully implement `+`, `-`, and/or `*`. $kApiDocsLink',
'To lerp rects, consider RectTween instead.',
]);
expect(
() => rectTween.transform(0.1),
throwsA(isA<FlutterError>().having(
(FlutterError error) => error.diagnostics.map((DiagnosticsNode node) => node.toString()),
'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)".',
'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', () {
......@@ -86,22 +74,18 @@ void main() {
end: 1,
);
FlutterError? error;
try {
colorTween.transform(0.1);
} on FlutterError catch (err) {
error = err;
}
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".',
'The type int returned a double after multiplication with a double value. $kApiDocsLink',
'To lerp int values, consider IntTween or StepTween instead.',
]);
expect(
() => colorTween.transform(0.1),
throwsA(isA<FlutterError>().having(
(FlutterError error) => error.diagnostics.map((DiagnosticsNode node) => node.toString()),
'diagnostics',
<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', () {
......
......@@ -371,24 +371,23 @@ void main() {
});
testWidgets('CrossAxisAlignment.baseline is not allowed', (WidgetTester tester) async {
try {
MaterialApp(
home: Material(
child: ExpansionTile(
initiallyExpanded: true,
title: const Text('title'),
expandedCrossAxisAlignment: CrossAxisAlignment.baseline,
expect(
() {
MaterialApp(
home: Material(
child: ExpansionTile(
initiallyExpanded: true,
title: const Text('title'),
expandedCrossAxisAlignment: CrossAxisAlignment.baseline,
),
),
),
);
} 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'
' 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 {
......
......@@ -485,11 +485,10 @@ void main() {
),
),
);
try {
await tester.pumpWidget(boilerplate);
} catch (e) {
fail('Expected no error, but got $e');
}
await expectLater(
() => tester.pumpWidget(boilerplate),
returnsNormally,
);
});
group('Accessibility (a11y/Semantics)', () {
......
......@@ -128,16 +128,15 @@ void main() {
]);
},
);
try {
await channel.invokeMethod<dynamic>('sayHello', 'hello');
fail('Exception expected');
} on PlatformException catch (e) {
expect(e.code, equals('bad'));
expect(e.message, equals('Something happened'));
expect(e.details, equals(<String, dynamic>{'a': 42, 'b': 3.14}));
} catch (e) {
fail('PlatformException expected');
}
expect(
() => channel.invokeMethod<dynamic>('sayHello', 'hello'),
throwsA(
isA<PlatformException>()
.having((PlatformException e) => e.code, 'code', equals('bad'))
.having((PlatformException e) => e.message, 'message', equals('Something happened'))
.having((PlatformException e) => e.details, 'details', equals(<String, dynamic>{'a': 42, 'b': 3.14})),
),
);
});
test('can invoke unimplemented method', () async {
......@@ -145,15 +144,16 @@ void main() {
'ch7',
(ByteData? message) async => null,
);
try {
await channel.invokeMethod<void>('sayHello', 'hello');
fail('Exception expected');
} on MissingPluginException catch (e) {
expect(e.message, contains('sayHello'));
expect(e.message, contains('ch7'));
} catch (e) {
fail('MissingPluginException expected');
}
expect(
() => channel.invokeMethod<void>('sayHello', 'hello'),
throwsA(
isA<MissingPluginException>()
.having((MissingPluginException e) => e.message, 'message', allOf(
contains('sayHello'),
contains('ch7'),
)),
),
);
});
test('can invoke unimplemented method (optional)', () async {
......@@ -218,15 +218,14 @@ void main() {
await TestDefaultBinaryMessengerBinding.instance!.defaultBinaryMessenger.handlePlatformMessage('ch7', call, (ByteData? result) {
envelope = result;
});
try {
jsonMethod.decodeEnvelope(envelope!);
fail('Exception expected');
} on PlatformException catch (e) {
expect(e.code, equals('bad'));
expect(e.message, equals('sayHello failed'));
} catch (e) {
fail('PlatformException expected');
}
expect(
() => jsonMethod.decodeEnvelope(envelope!),
throwsA(
isA<PlatformException>()
.having((PlatformException e) => e.code, 'code', equals('bad'))
.having((PlatformException e) => e.message, 'message', equals('sayHello failed')),
),
);
});
test('can handle method call with other error result', () async {
......@@ -238,15 +237,14 @@ void main() {
await TestDefaultBinaryMessengerBinding.instance!.defaultBinaryMessenger.handlePlatformMessage('ch7', call, (ByteData? result) {
envelope = result;
});
try {
jsonMethod.decodeEnvelope(envelope!);
fail('Exception expected');
} on PlatformException catch (e) {
expect(e.code, equals('error'));
expect(e.message, equals('Invalid argument(s): bad'));
} catch (e) {
fail('PlatformException expected');
}
expect(
() => jsonMethod.decodeEnvelope(envelope!),
throwsA(
isA<PlatformException>()
.having((PlatformException e) => e.code, 'code', equals('error'))
.having((PlatformException e) => e.message, 'message', equals('Invalid argument(s): bad')),
),
);
});
test('can check the mock handler', () async {
......
......@@ -686,16 +686,20 @@ void main() {
};
}
try {
await TestDefaultBinaryMessengerBinding.instance!.defaultBinaryMessenger.handlePlatformMessage(
SystemChannels.keyEvent.name,
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'));
}
expect(
() async {
await TestDefaultBinaryMessengerBinding.instance!.defaultBinaryMessenger.handlePlatformMessage(
SystemChannels.keyEvent.name,
SystemChannels.keyEvent.codec.encodeMessage(keyEventMessage),
(ByteData? data) { },
);
},
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() {
});
testWidgets('minLines cannot be greater than maxLines', (WidgetTester tester) async {
try {
await tester.pumpWidget(
overlay(
child: SizedBox(
width: 300.0,
child: SelectableText(
'abcd',
minLines: 4,
maxLines: 3,
expect(
() async {
await tester.pumpWidget(
overlay(
child: SizedBox(
width: 300.0,
child: SelectableText(
'abcd',
minLines: 4,
maxLines: 3,
),
),
),
),
);
} on AssertionError catch (e) {
expect(e.toString(), contains("minLines can't be greater than maxLines"));
return;
}
fail('An assert should be triggered when minLines is greater than maxLines');
);
},
throwsA(isA<AssertionError>().having(
(AssertionError error) => error.toString(),
'.toString()',
contains("minLines can't be greater than maxLines"),
)),
);
});
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