Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit a3a9a23

Browse files
Add matcher to find at least a given number of widgets (#102081) (#102342)
1 parent 6ea4aef commit a3a9a23

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

packages/flutter_test/lib/src/matchers.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import 'widget_tester.dart' show WidgetTester;
3737
/// * [findsWidgets], when you want the finder to find one or more widgets.
3838
/// * [findsOneWidget], when you want the finder to find exactly one widget.
3939
/// * [findsNWidgets], when you want the finder to find a specific number of widgets.
40+
/// * [findsAtLeastNWidgets], when you want the finder to find at least a specific number of widgets.
4041
const Matcher findsNothing = _FindsWidgetMatcher(null, 0);
4142

4243
/// Asserts that the [Finder] locates at least one widget in the widget tree.
@@ -52,6 +53,7 @@ const Matcher findsNothing = _FindsWidgetMatcher(null, 0);
5253
/// * [findsNothing], when you want the finder to not find anything.
5354
/// * [findsOneWidget], when you want the finder to find exactly one widget.
5455
/// * [findsNWidgets], when you want the finder to find a specific number of widgets.
56+
/// * [findsAtLeastNWidgets], when you want the finder to find at least a specific number of widgets.
5557
const Matcher findsWidgets = _FindsWidgetMatcher(1, null);
5658

5759
/// Asserts that the [Finder] locates at exactly one widget in the widget tree.
@@ -67,6 +69,7 @@ const Matcher findsWidgets = _FindsWidgetMatcher(1, null);
6769
/// * [findsNothing], when you want the finder to not find anything.
6870
/// * [findsWidgets], when you want the finder to find one or more widgets.
6971
/// * [findsNWidgets], when you want the finder to find a specific number of widgets.
72+
/// * [findsAtLeastNWidgets], when you want the finder to find at least a specific number of widgets.
7073
const Matcher findsOneWidget = _FindsWidgetMatcher(1, 1);
7174

7275
/// Asserts that the [Finder] locates the specified number of widgets in the widget tree.
@@ -82,8 +85,25 @@ const Matcher findsOneWidget = _FindsWidgetMatcher(1, 1);
8285
/// * [findsNothing], when you want the finder to not find anything.
8386
/// * [findsWidgets], when you want the finder to find one or more widgets.
8487
/// * [findsOneWidget], when you want the finder to find exactly one widget.
88+
/// * [findsAtLeastNWidgets], when you want the finder to find at least a specific number of widgets.
8589
Matcher findsNWidgets(int n) => _FindsWidgetMatcher(n, n);
8690

91+
/// Asserts that the [Finder] locates at least a number of widgets in the widget tree.
92+
///
93+
/// ## Sample code
94+
///
95+
/// ```dart
96+
/// expect(find.text('Save'), findsAtLeastNWidgets(2));
97+
/// ```
98+
///
99+
/// See also:
100+
///
101+
/// * [findsNothing], when you want the finder to not find anything.
102+
/// * [findsWidgets], when you want the finder to find one or more widgets.
103+
/// * [findsOneWidget], when you want the finder to find exactly one widget.
104+
/// * [findsNWidgets], when you want the finder to find a specific number of widgets.
105+
Matcher findsAtLeastNWidgets(int n) => _FindsWidgetMatcher(n, null);
106+
87107
/// Asserts that the [Finder] locates a single widget that has at
88108
/// least one [Offstage] widget ancestor.
89109
///

packages/flutter_test/test/matchers_test.dart

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,42 @@ void main() {
674674
handle.dispose();
675675
});
676676
});
677+
678+
group('findsAtLeastNWidgets', () {
679+
Widget boilerplate(Widget child) {
680+
return Directionality(
681+
textDirection: TextDirection.ltr,
682+
child: child,
683+
);
684+
}
685+
686+
testWidgets('succeeds when finds more then the specified count',
687+
(WidgetTester tester) async {
688+
await tester.pumpWidget(boilerplate(Column(
689+
children: const <Widget>[Text('1'), Text('2'), Text('3')],
690+
)));
691+
692+
expect(find.byType(Text), findsAtLeastNWidgets(2));
693+
});
694+
695+
testWidgets('succeeds when finds the exact specified count',
696+
(WidgetTester tester) async {
697+
await tester.pumpWidget(boilerplate(Column(
698+
children: const <Widget>[Text('1'), Text('2')],
699+
)));
700+
701+
expect(find.byType(Text), findsAtLeastNWidgets(2));
702+
});
703+
704+
testWidgets('fails when finds less then specified count',
705+
(WidgetTester tester) async {
706+
await tester.pumpWidget(boilerplate(Column(
707+
children: const <Widget>[Text('1'), Text('2')],
708+
)));
709+
710+
expect(find.byType(Text), isNot(findsAtLeastNWidgets(3)));
711+
});
712+
});
677713
}
678714

679715
enum _ComparatorBehavior {

0 commit comments

Comments
 (0)