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

Side by Side Diff: lib/unittest/expect.dart

Issue 10441104: New expectation functions plus convert old tests to use these. (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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 /**
6 * This is the main assertion function. It asserts that [actual]
7 * matches the [matcher]. [reason] is optional and is typically
8 * not supplied, as a reason can be generated from the matcher.
9 * If [reason] is included it is appended to the reason generated
10 * by the matcher.
11 *
12 * If the assertion fails, then the default behavior is to throw an
13 * [ExpectException], but this behavior can be changed by calling
14 * [configureExpectHandler] and providing an alternative handler that
15 * implements the [IFailureHandler] interface.
16 *
17 * [expect] allows an alternative call format, providing a Boolean
18 * predicate as the first argument and an optional reason as the
19 * second argument. This supports brevity at the expense of detailed
20 * error messages. For example, these are equivalent, but the first
21 * form will give a detailed error message, while the second form will
22 * just give a generic assertion failed message:
23 *
24 * expect(foo, isLessThanOrEqual(bar));
25 * expect(foo <= bar);
26 *
27 * expect() is a 3rd generation assertion mechanism, drawing
28 * inspiration from [Hamcrest] and Ladislav Thon's [dart-matchers]
29 * library.
30 *
31 * See [Hamcrest] http://en.wikipedia.org/wiki/Hamcrest
32 * [Hamcrest] http://http://code.google.com/p/hamcrest/
33 * [dart-matchers] https://github.com/Ladicek/dart-matchers
34 */
35 void expect(actual, [matcherOrReason = null, String reason = '']) {
36 if (matcherOrReason is Matcher) {
37 var doesMatch;
38 try {
39 doesMatch = matcherOrReason.matches(actual);
40 } catch (var e, var trace) {
41 doesMatch = false;
42 if (reason == '') {
43 reason = '${(e is String) ? e : e.toString()} at $trace';
44 }
45 }
46 if (!doesMatch) {
47 // Make sure we have a failure handler and formatter configured.
48 configureExpectHandler(_assertFailureHandler);
49 configureExpectFormatter(_assertErrorFormatter);
50 _assertFailureHandler.failMatch(actual, matcherOrReason, reason);
51 }
52 } else {
53 if (!actual) {
54 reason = (matcherOrReason == null) ? 'Assertion failed' : matcherOrReason;
55 // Make sure we have a failure handler configured.
56 configureExpectHandler(_assertFailureHandler);
57 _assertFailureHandler.fail(reason);
58 }
59 }
60 }
61
62 /**
63 * Takes an argument and returns an equivalent matcher.
64 * If the argument is already a matcher this does nothing, else it
65 * generates an equals matcher for the argument.
66 */
67 Matcher wrapMatcher(x) => ((x is Matcher) ? x : equals(x));
68
69 // The handler for failed asserts.
70 FailureHandler _assertFailureHandler = null;
71
72 // The default failure handler that throws ExpectExceptions.
73 class DefaultFailureHandler implements FailureHandler {
74 void fail(String reason) {
75 throw new ExpectException(reason);
76 }
77 void failMatch(actual, Matcher matcher, String reason) {
78 fail(_assertErrorFormatter(actual, matcher, reason));
79 }
80 }
81
82 /**
83 * Changes or resets to the default the failure handler for expect()
84 * [handler] is a reference to the new handler; if this is omitted
85 * or null then the failure handler is reset to the default, which
86 * throws [ExpectExceptions] on [expect] assertion failures.
87 */
88 void configureExpectHandler([FailureHandler handler = null]) {
89 if (handler == null) {
90 handler = new DefaultFailureHandler();
91 }
92 _assertFailureHandler = handler;
93 }
94
95 // The error message formatter for failed asserts.
96 ErrorFormatter _assertErrorFormatter = null;
97
98 // The default error formatter implementation.
99 String _defaultErrorFormatter(actual, Matcher matcher, String reason) {
100 var description = new StringDescription();
101 description.add('Expected: ').addDescriptionOf(matcher).
102 add('\n but: ');
103 matcher.describeMismatch(actual, description);
104 description.add('\n').add(reason).add('\n');
105 return description.toString();
106 }
107
108 /**
109 * Changes or resets to default the failure message formatter for expect().
110 * [formatter] is a reference to the new formatter; if this is omitted or
111 * null then the failure formatter is reset to the default. The new
112 * formatter is returned; this allows custom expect handlers to easily
113 * get a reference to the default formatter.
114 */
115 ErrorFormatter configureExpectFormatter([ErrorFormatter formatter = null]) {
116 if (formatter == null) {
117 formatter = _defaultErrorFormatter;
118 }
119 return _assertErrorFormatter = formatter;
120 }
121
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698