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

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 an optional and is typically
Bob Nystrom 2012/06/01 18:22:23 "an optional" -> "optional".
gram 2012/06/01 22:22:00 Done.
8 * not supplied, as a reason can be generated from the matcher.
9 *
10 * If the assertion fails, then the default behavior is to throw an
11 * [ExpectException], but this behavior can be changed by calling
12 * [configureExpectHandler] and providing an alternative handler that
13 * implements the [IFailureHandler] interface.
14 *
15 * [expect] allows an alternative call format, providing a Boolean
16 * predicate as the first argument and an optional reason as the
17 * second argument. This supports brevity at the expense of detailed
18 * error messages. For example, these are equivalent, but the first
19 * form will give a detailed error message, while the second form will
20 * just give a generic assertion failed message:
21 *
22 * expect(foo, isLessThanOrEqual(bar));
23 * expect(foo <= bar);
24 *
25 * expect() is a 3rd generation assertion mechanism, drawing
26 * inspiration from [Hamcrest] and Ladislav Thon's [dart-matchers]
27 * library.
28 *
29 * See [Hamcrest] http://en.wikipedia.org/wiki/Hamcrest
30 * [Hamcrest] http://http://code.google.com/p/hamcrest/
31 * [dart-matchers] https://github.com/Ladicek/dart-matchers
32 */
33 void expect(actual, [matcherOrReason = null, String reason = '']) {
34 if (matcherOrReason is IMatcher) {
35 if (!matcherOrReason.matches(actual)) {
36 // Make sure we have a failure handler and formatter configured
Bob Nystrom 2012/06/01 18:22:23 "." at the end of the comment. Google style is tha
gram 2012/06/01 22:22:00 Done.
37 configureExpectHandler(_assertFailureHandler);
38 configureExpectFormatter(_assertErrorFormatter);
39 _assertFailureHandler.failMatch(actual, matcherOrReason, reason);
40 }
41 } else {
42 if (!actual) {
43 reason = (matcherOrReason == null) ? 'Assertion failed' : matcherOrReason;
44 // Make sure we have a failure handler configured
Bob Nystrom 2012/06/01 18:22:23 "."
gram 2012/06/01 22:22:00 Done.
45 configureExpectHandler(_assertFailureHandler);
46 _assertFailureHandler.fail(reason);
47 }
48 }
49 }
50
51 /**
52 * Takes an argument and returns an equivalent matcher.
53 * If the argument is already a matcher this does nothing, else it
54 * generates an equals matcher for the argument.
55 */
56 IMatcher wrapMatcher(x) => ((x is IMatcher) ? x : equals(x));
57
58 // The handler for failed asserts
59 IFailureHandler _assertFailureHandler = null;
60
61 // The default failure handler that throws ExpectExceptions
62 class DefaultFailureHandler implements IFailureHandler {
63 void fail(String reason) {
64 throw new ExpectException(reason);
65 }
66 void failMatch(actual, IMatcher matcher, String reason) {
67 fail(_assertErrorFormatter(actual, matcher, reason));
68 }
69 }
70
71 /**
72 * Changes or resets to the default the failure handler for expect()
73 * [handler] is a reference to the new handler; if this is omitted
74 * or null then the failure handler is reset to the default, which
75 * throws [ExpectExceptions] on [expect] assertion failures.
76 */
77 void configureExpectHandler([IFailureHandler handler = null]) {
78 if (handler == null) {
79 handler = new DefaultFailureHandler();
80 }
81 _assertFailureHandler = handler;
82 }
83
84 // The error message formatter for failed asserts
85 ErrorFormatter _assertErrorFormatter = null;
86
87 // The default error formatter implementation
88 String _defaultErrorFormatter(actual, IMatcher matcher, String reason) {
89 var description = new StringDescription();
90 description.add(reason).add('\nExpected: ').addDescriptionOf(matcher).
91 add('\n but: ');
92 matcher.describeMismatch(actual, description);
93 description.add('\n');
94 return description.toString();
95 }
96
97 /**
98 * Changes or resets to default the failure message formatter for expect().
99 * [formatter] is a reference to the new formatter; if this is omitted or
100 * null then the failure formatter is reset to the default. The new
101 * formatter is returned; this allows custom expect handlers to easily
102 * get a reference to the default formatter.
103 */
104 ErrorFormatter configureExpectFormatter([ErrorFormatter formatter = null]) {
105 if (formatter == null) {
106 formatter = _defaultErrorFormatter;
107 }
108 return _assertErrorFormatter = formatter;
109 }
110
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698