Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reason parameter to waitFor. #305

Merged
merged 3 commits into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 3.1.0-wip

* Add a `reason` argument to `Clock.waitFor`.

## 3.0.4

* Require Dart 3.1.
Expand Down
25 changes: 18 additions & 7 deletions lib/support/async.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import 'dart:async' show Completer, FutureOr;

import 'package:matcher/matcher.dart' as m;
import 'package:matcher/expect.dart' as m;
import 'package:stack_trace/stack_trace.dart' show Chain;

const defaultInterval = Duration(milliseconds: 500);
Expand All @@ -25,9 +25,10 @@ const clock = Clock();
Future<T> waitFor<T>(FutureOr<T> Function() condition,
{Object? matcher,
Duration timeout = defaultTimeout,
Duration interval = defaultInterval}) =>
Duration interval = defaultInterval,
String? reason}) =>
clock.waitFor<T>(condition,
matcher: matcher, timeout: timeout, interval: interval);
matcher: matcher, timeout: timeout, interval: interval, reason: reason);

class Clock {
const Clock();
Expand All @@ -47,17 +48,22 @@ class Clock {
/// is returned. Otherwise, if [condition] throws, then that exception is
/// rethrown. If [condition] doesn't throw then an [expect] exception is
/// thrown.
///
/// [reason] is optional and is typically not supplied, as a reason is
/// generated from [matcher]; if [reason] is included it is appended to the
/// reason generated by the matcher.
Future<T> waitFor<T>(FutureOr<T> Function() condition,
{Object? matcher,
Duration timeout = defaultTimeout,
Duration interval = defaultInterval}) async {
Duration interval = defaultInterval,
String? reason}) async {
final mMatcher = matcher == null ? null : m.wrapMatcher(matcher);
final endTime = now.add(timeout);
while (true) {
try {
final value = await condition();
if (mMatcher != null) {
_matcherExpect(value, mMatcher);
_matcherExpect(value, mMatcher, reason);
}
return value;
} catch (e) {
Expand All @@ -71,7 +77,7 @@ class Clock {
}
}

void _matcherExpect(Object? value, m.Matcher matcher) {
void _matcherExpect(Object? value, m.Matcher matcher, String? reason) {
final matchState = {};
if (matcher.matches(value, matchState)) {
return;
Expand All @@ -89,7 +95,12 @@ void _matcherExpect(Object? value, m.Matcher matcher) {
if (mismatchDescription.length > 0) {
desc.add(' Which: $mismatchDescription\n');
}
throw Exception(desc.toString());

if (reason != null) {
desc.add('$reason\n');
}

m.fail(desc.toString());
}

class Lock {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: webdriver
version: 3.0.4
version: 3.1.0-wip
description: >-
Provides WebDriver bindings for Dart. Supports WebDriver JSON interface and
W3C spec. Requires the use of WebDriver remote server.
Expand Down
16 changes: 15 additions & 1 deletion test/support/async_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ void main() {
expect(result, equals('Google'));
});

test('throws if condition throws and timeouts', () async {
test('throws if condition throws', () async {
Object? exception;

try {
Expand All @@ -127,6 +127,20 @@ void main() {
expect(exception, isNotNull);
});

test('throws with passed reason if condition never matches', () async {
Object? exception;
try {
await clock.waitFor(() => null,
matcher: isNotNull, reason: 'some reason');
} catch (e) {
exception = e;
}
expect(
exception,
isA<TestFailure>()
.having((e) => e.message, 'message', contains('some reason')));
});

test('uses Future value', () async {
final result = await clock.waitFor(() => Future.value('a value'),
matcher: 'a value');
Expand Down
Loading