1414
1515import 'dart:async' show Completer, FutureOr;
1616
17- import 'package:matcher/matcher .dart' as m;
17+ import 'package:matcher/expect .dart' as m;
1818import 'package:stack_trace/stack_trace.dart' show Chain;
1919
2020const defaultInterval = Duration (milliseconds: 500 );
@@ -25,9 +25,10 @@ const clock = Clock();
2525Future <T > waitFor <T >(FutureOr <T > Function () condition,
2626 {Object ? matcher,
2727 Duration timeout = defaultTimeout,
28- Duration interval = defaultInterval}) =>
28+ Duration interval = defaultInterval,
29+ String ? reason}) =>
2930 clock.waitFor <T >(condition,
30- matcher: matcher, timeout: timeout, interval: interval);
31+ matcher: matcher, timeout: timeout, interval: interval, reason : reason );
3132
3233class Clock {
3334 const Clock ();
@@ -47,17 +48,22 @@ class Clock {
4748 /// is returned. Otherwise, if [condition] throws, then that exception is
4849 /// rethrown. If [condition] doesn't throw then an [expect] exception is
4950 /// thrown.
51+ ///
52+ /// [reason] is optional and is typically not supplied, as a reason is
53+ /// generated from [matcher] ; if [reason] is included it is appended to the
54+ /// reason generated by the matcher.
5055 Future <T > waitFor <T >(FutureOr <T > Function () condition,
5156 {Object ? matcher,
5257 Duration timeout = defaultTimeout,
53- Duration interval = defaultInterval}) async {
58+ Duration interval = defaultInterval,
59+ String ? reason}) async {
5460 final mMatcher = matcher == null ? null : m.wrapMatcher (matcher);
5561 final endTime = now.add (timeout);
5662 while (true ) {
5763 try {
5864 final value = await condition ();
5965 if (mMatcher != null ) {
60- _matcherExpect (value, mMatcher);
66+ _matcherExpect (value, mMatcher, reason );
6167 }
6268 return value;
6369 } catch (e) {
@@ -71,7 +77,7 @@ class Clock {
7177 }
7278}
7379
74- void _matcherExpect (Object ? value, m.Matcher matcher) {
80+ void _matcherExpect (Object ? value, m.Matcher matcher, String ? reason ) {
7581 final matchState = {};
7682 if (matcher.matches (value, matchState)) {
7783 return ;
@@ -89,7 +95,12 @@ void _matcherExpect(Object? value, m.Matcher matcher) {
8995 if (mismatchDescription.length > 0 ) {
9096 desc.add (' Which: $mismatchDescription \n ' );
9197 }
92- throw Exception (desc.toString ());
98+
99+ if (reason != null ) {
100+ desc.add ('$reason \n ' );
101+ }
102+
103+ m.fail (desc.toString ());
93104}
94105
95106class Lock {
0 commit comments