Change Clock.waitFor to support both unittest-based and matcher-based matchers.
diff --git a/lib/support/async.dart b/lib/support/async.dart
index 3f021af..39d5911 100644
--- a/lib/support/async.dart
+++ b/lib/support/async.dart
@@ -16,15 +16,15 @@
import 'dart:async' show Completer, Future;
-import 'package:matcher/matcher.dart';
+import 'package:matcher/matcher.dart' as m;
+import 'package:unittest/unittest.dart' as ut;
const defaultInterval = const Duration(milliseconds: 500);
const defaultTimeout = const Duration(seconds: 5);
const clock = const Clock();
-Future waitFor(condition(), {matcher: isNotNull,
- Duration timeout: defaultTimeout,
+Future waitFor(condition(), {matcher: null, Duration timeout: defaultTimeout,
Duration interval: defaultInterval}) => clock.waitFor(condition,
matcher: matcher, timeout: timeout, interval: interval);
@@ -46,39 +46,21 @@
/// is returned. Otherwise, if [condition] throws, then that exception is
/// rethrown. If [condition] doesn't throw then an [expect] exception is
/// thrown.
- Future waitFor(condition(), {matcher: isNotNull,
- Duration timeout: defaultTimeout,
+ Future waitFor(condition(), {matcher: null, Duration timeout: defaultTimeout,
Duration interval: defaultInterval}) async {
- expect(value, matcher) {
- if (matcher is! Matcher) {
- matcher = equals(matcher);
- }
-
- var matchState = {};
- if (matcher.matches(value, matchState)) {
- return;
- }
- var desc = new StringDescription()
- ..add('Expected: ')
- ..addDescriptionOf(matcher)
- ..add('\n')
- ..add(' Actual: ')
- ..addDescriptionOf(value)
- ..add('\n');
-
- var mismatchDescription = new StringDescription();
- matcher.describeMismatch(value, mismatchDescription, matchState, true);
- if (mismatchDescription.length > 0) {
- desc.add(' Which: ${mismatchDescription}\n');
- }
- throw new Exception(desc.toString());
+ if (matcher != null && matcher is! ut.Matcher && matcher is! m.Matcher) {
+ matcher = m.equals(matcher);
}
var endTime = now.add(timeout);
while (true) {
try {
var value = await condition();
- expect(value, matcher);
+ if (matcher is m.Matcher) {
+ _matcherExpect(value, matcher);
+ } else if (matcher is ut.Matcher) {
+ _unittestExpect(value, matcher);
+ }
return value;
} catch (e) {
if (now.isAfter(endTime)) {
@@ -91,6 +73,29 @@
}
}
+_unittestExpect(value, ut.Matcher matcher) => ut.expect(value, matcher);
+
+_matcherExpect(value, m.Matcher matcher) {
+ var matchState = {};
+ if (matcher.matches(value, matchState)) {
+ return;
+ }
+ var desc = new m.StringDescription()
+ ..add('Expected: ')
+ ..addDescriptionOf(matcher)
+ ..add('\n')
+ ..add(' Actual: ')
+ ..addDescriptionOf(value)
+ ..add('\n');
+
+ var mismatchDescription = new m.StringDescription();
+ matcher.describeMismatch(value, mismatchDescription, matchState, true);
+ if (mismatchDescription.length > 0) {
+ desc.add(' Which: ${mismatchDescription}\n');
+ }
+ throw new Exception(desc.toString());
+}
+
class Lock {
Completer _lock;
diff --git a/pubspec.yaml b/pubspec.yaml
index 9a06818..f5746cc 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,5 +1,5 @@
name: webdriver
-version: 0.10.0-pre.12
+version: 0.10.0-pre.13
author: Marc Fisher II <fisherii@google.com>
description: >
Provides WebDriver bindings for Dart. These use the WebDriver JSON interface,
@@ -10,7 +10,8 @@
dependencies:
crypto: '^0.9.0'
matcher: '^0.12.0+1'
- path: '^1.3.5'
+ path: '^1.3.6'
stack_trace: '^1.3.4'
+ unittest: '^0.12.3'
dev_dependencies:
- test: '^0.12.3+5'
+ test: '^0.12.3+7'
diff --git a/test/support/async_test.dart b/test/support/async_test.dart
index b33a858..b38e990 100644
--- a/test/support/async_test.dart
+++ b/test/support/async_test.dart
@@ -17,6 +17,7 @@
import 'dart:async' show Future;
import 'package:test/test.dart';
+import 'package:unittest/unittest.dart' as ut;
import 'package:webdriver/support/async.dart';
void main() {
@@ -92,25 +93,11 @@
var result = await clock.waitFor(() {
if (count == 2) return 'Google';
count++;
- return null;
+ throw '';
});
expect(result, equals('Google'));
});
- test('throws before successful', () async {
- var count = 0;
- var result = await clock.waitFor(() {
- expect(count, lessThanOrEqualTo(2));
- if (count == 2) {
- count++;
- return false;
- }
- count++;
- return null;
- });
- expect(result, isFalse);
- });
-
test('throws if condition throws and timeouts', () async {
var exception;
@@ -125,13 +112,33 @@
test('throws if condition never matches', () async {
var exception;
try {
- await clock.waitFor(() => null);
+ await clock.waitFor(() => null, matcher: isNotNull);
} catch (e) {
exception = e;
}
expect(exception, isNotNull);
});
+ test('throws if condition never matches unittest.Matcher', () async {
+ var exception;
+ try {
+ await clock.waitFor(() => null, matcher: ut.isNotNull);
+ } catch (e) {
+ exception = e;
+ }
+ expect(exception, isNotNull);
+ });
+
+ test('returns if condition matches unittest.Matcher', () async {
+ var count = 0;
+ var result = await clock.waitFor(() {
+ if (count == 2) return 'Google';
+ count++;
+ return null;
+ }, matcher: ut.isNotNull);
+ expect(result, isNotNull);
+ });
+
test('uses Future value', () async {
var result = await clock.waitFor(() => new Future.value('a value'),
matcher: 'a value');
@@ -159,7 +166,7 @@
} else {
return 'a value';
}
- });
+ }, matcher: isNotNull);
expect(result, 'a value');
});
@@ -178,7 +185,7 @@
var clock = new Clock();
var exception;
try {
- await clock.waitFor(() => null);
+ await clock.waitFor(() => null, matcher: isNotNull);
} catch (e) {
exception = e;
}
diff --git a/tool/travis.sh b/tool/travis.sh
index aa54aaa..3f8bbbd 100755
--- a/tool/travis.sh
+++ b/tool/travis.sh
@@ -19,7 +19,7 @@
# Verify that the libraries are error free.
pub global activate tuneup
-pub global run tuneup check
+pub global run tuneup check --ignore-infos
# Start chromedriver.
chromedriver --port=4444 --url-base=wd/hub &