Add matcher to find at least a given number of widgets (#102081) (#102342)

This commit is contained in:
Daniel Cardona Rojas 2022-04-22 17:24:10 -05:00 committed by GitHub
parent 6ea4aef827
commit a3a9a23e46
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 0 deletions

View File

@ -37,6 +37,7 @@ import 'widget_tester.dart' show WidgetTester;
/// * [findsWidgets], when you want the finder to find one or more widgets. /// * [findsWidgets], when you want the finder to find one or more widgets.
/// * [findsOneWidget], when you want the finder to find exactly one widget. /// * [findsOneWidget], when you want the finder to find exactly one widget.
/// * [findsNWidgets], when you want the finder to find a specific number of widgets. /// * [findsNWidgets], when you want the finder to find a specific number of widgets.
/// * [findsAtLeastNWidgets], when you want the finder to find at least a specific number of widgets.
const Matcher findsNothing = _FindsWidgetMatcher(null, 0); const Matcher findsNothing = _FindsWidgetMatcher(null, 0);
/// Asserts that the [Finder] locates at least one widget in the widget tree. /// Asserts that the [Finder] locates at least one widget in the widget tree.
@ -52,6 +53,7 @@ const Matcher findsNothing = _FindsWidgetMatcher(null, 0);
/// * [findsNothing], when you want the finder to not find anything. /// * [findsNothing], when you want the finder to not find anything.
/// * [findsOneWidget], when you want the finder to find exactly one widget. /// * [findsOneWidget], when you want the finder to find exactly one widget.
/// * [findsNWidgets], when you want the finder to find a specific number of widgets. /// * [findsNWidgets], when you want the finder to find a specific number of widgets.
/// * [findsAtLeastNWidgets], when you want the finder to find at least a specific number of widgets.
const Matcher findsWidgets = _FindsWidgetMatcher(1, null); const Matcher findsWidgets = _FindsWidgetMatcher(1, null);
/// Asserts that the [Finder] locates at exactly one widget in the widget tree. /// Asserts that the [Finder] locates at exactly one widget in the widget tree.
@ -67,6 +69,7 @@ const Matcher findsWidgets = _FindsWidgetMatcher(1, null);
/// * [findsNothing], when you want the finder to not find anything. /// * [findsNothing], when you want the finder to not find anything.
/// * [findsWidgets], when you want the finder to find one or more widgets. /// * [findsWidgets], when you want the finder to find one or more widgets.
/// * [findsNWidgets], when you want the finder to find a specific number of widgets. /// * [findsNWidgets], when you want the finder to find a specific number of widgets.
/// * [findsAtLeastNWidgets], when you want the finder to find at least a specific number of widgets.
const Matcher findsOneWidget = _FindsWidgetMatcher(1, 1); const Matcher findsOneWidget = _FindsWidgetMatcher(1, 1);
/// Asserts that the [Finder] locates the specified number of widgets in the widget tree. /// Asserts that the [Finder] locates the specified number of widgets in the widget tree.
@ -82,8 +85,25 @@ const Matcher findsOneWidget = _FindsWidgetMatcher(1, 1);
/// * [findsNothing], when you want the finder to not find anything. /// * [findsNothing], when you want the finder to not find anything.
/// * [findsWidgets], when you want the finder to find one or more widgets. /// * [findsWidgets], when you want the finder to find one or more widgets.
/// * [findsOneWidget], when you want the finder to find exactly one widget. /// * [findsOneWidget], when you want the finder to find exactly one widget.
/// * [findsAtLeastNWidgets], when you want the finder to find at least a specific number of widgets.
Matcher findsNWidgets(int n) => _FindsWidgetMatcher(n, n); Matcher findsNWidgets(int n) => _FindsWidgetMatcher(n, n);
/// Asserts that the [Finder] locates at least a number of widgets in the widget tree.
///
/// ## Sample code
///
/// ```dart
/// expect(find.text('Save'), findsAtLeastNWidgets(2));
/// ```
///
/// See also:
///
/// * [findsNothing], when you want the finder to not find anything.
/// * [findsWidgets], when you want the finder to find one or more widgets.
/// * [findsOneWidget], when you want the finder to find exactly one widget.
/// * [findsNWidgets], when you want the finder to find a specific number of widgets.
Matcher findsAtLeastNWidgets(int n) => _FindsWidgetMatcher(n, null);
/// Asserts that the [Finder] locates a single widget that has at /// Asserts that the [Finder] locates a single widget that has at
/// least one [Offstage] widget ancestor. /// least one [Offstage] widget ancestor.
/// ///

View File

@ -674,6 +674,42 @@ void main() {
handle.dispose(); handle.dispose();
}); });
}); });
group('findsAtLeastNWidgets', () {
Widget boilerplate(Widget child) {
return Directionality(
textDirection: TextDirection.ltr,
child: child,
);
}
testWidgets('succeeds when finds more then the specified count',
(WidgetTester tester) async {
await tester.pumpWidget(boilerplate(Column(
children: const <Widget>[Text('1'), Text('2'), Text('3')],
)));
expect(find.byType(Text), findsAtLeastNWidgets(2));
});
testWidgets('succeeds when finds the exact specified count',
(WidgetTester tester) async {
await tester.pumpWidget(boilerplate(Column(
children: const <Widget>[Text('1'), Text('2')],
)));
expect(find.byType(Text), findsAtLeastNWidgets(2));
});
testWidgets('fails when finds less then specified count',
(WidgetTester tester) async {
await tester.pumpWidget(boilerplate(Column(
children: const <Widget>[Text('1'), Text('2')],
)));
expect(find.byType(Text), isNot(findsAtLeastNWidgets(3)));
});
});
} }
enum _ComparatorBehavior { enum _ComparatorBehavior {