Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(861)

Unified Diff: lib/unittest/expect.dart

Issue 10544167: Some unit test fixes (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « lib/unittest/core_matchers.dart ('k') | lib/unittest/unittest.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/unittest/expect.dart
===================================================================
--- lib/unittest/expect.dart (revision 8732)
+++ lib/unittest/expect.dart (working copy)
@@ -15,7 +15,7 @@
* implements the [IFailureHandler] interface.
*
* [expect] allows an alternative call format, providing a Boolean
- * predicate as the first argument and an optional reason as the
+ * predicate as the first argument and an optional reason as a named
* second argument. This supports brevity at the expense of detailed
* error messages. For example, these are equivalent, but the first
* form will give a detailed error message, while the second form will
@@ -24,6 +24,10 @@
* expect(foo, isLessThanOrEqual(bar));
* expect(foo <= bar);
*
+ * A better way of doing the second form is:
+ *
+ * expect(foo <= bar, reason: "foo not less than or equal to bar");
+ *
* expect() is a 3rd generation assertion mechanism, drawing
* inspiration from [Hamcrest] and Ladislav Thon's [dart-matchers]
* library.
@@ -32,29 +36,34 @@
* [Hamcrest] http://http://code.google.com/p/hamcrest/
* [dart-matchers] https://github.com/Ladicek/dart-matchers
*/
-void expect(actual, [matcherOrReason = null, String reason = '']) {
- if (matcherOrReason is Matcher) {
+void expect(actual, [matcher = null, String reason = null]) {
+ if (matcher == null) {
+ // Treat this as an assert(predicate, [reason]).
+ if (!actual) {
+ if (reason == null) {
+ reason = 'Assertion failed';
+ }
+ // Make sure we have a failure handler configured.
+ configureExpectHandler(_assertFailureHandler);
+ _assertFailureHandler.fail(reason);
+ }
+ } else {
+ // Treat this as an expect(value, [matcher], [reason]).
+ matcher = wrapMatcher(matcher);
var doesMatch;
try {
- doesMatch = matcherOrReason.matches(actual);
+ doesMatch = matcher.matches(actual);
} catch (var e, var trace) {
doesMatch = false;
- if (reason == '') {
+ if (reason == null) {
reason = '${(e is String) ? e : e.toString()} at $trace';
}
}
if (!doesMatch) {
// Make sure we have a failure handler configured.
configureExpectHandler(_assertFailureHandler);
- _assertFailureHandler.failMatch(actual, matcherOrReason, reason);
+ _assertFailureHandler.failMatch(actual, matcher, reason);
}
- } else {
- if (!actual) {
- reason = (matcherOrReason == null) ? 'Assertion failed' : matcherOrReason;
- // Make sure we have a failure handler configured.
- configureExpectHandler(_assertFailureHandler);
- _assertFailureHandler.fail(reason);
- }
}
}
@@ -105,7 +114,9 @@
description.add('Expected: ').addDescriptionOf(matcher).
add('\n but: ');
matcher.describeMismatch(actual, description);
- description.add('\n').add(reason).add('\n');
+ if (reason != null) {
+ description.add('\n').add(reason).add('\n');
+ }
return description.toString();
}
« no previous file with comments | « lib/unittest/core_matchers.dart ('k') | lib/unittest/unittest.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698