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

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

Issue 11301046: Restructure pkg/unittest and pkg/webdriver to follow the pub conventions. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 1 month 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 part of unittest;
6
7 /**
8 * This is the main assertion function. It asserts that [actual]
9 * matches the [matcher]. [reason] is optional and is typically not
10 * supplied, as a reason is generated from the matcher; if [reason]
11 * is included it is appended to the reason generated by the matcher.
12 *
13 * [matcher] can be a value in which case it will be wrapped in an
14 * [equals] matcher.
15 *
16 * If the assertion fails, then the default behavior is to throw an
17 * [ExpectException], but this behavior can be changed by calling
18 * [configureExpectFailureHandler] and providing an alternative handler that
19 * implements the [IFailureHandler] interface. It is also possible to
20 * pass a [failureHandler] to [expect] as a final parameter for fine-
21 * grained control.
22 *
23 * In some cases extra diagnostic info can be produced on failure (for
24 * example, stack traces on mismatched exceptions). To enable these,
25 * [verbose] should be specified as true;
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://code.google.com/p/hamcrest/
33 * [dart-matchers] https://github.com/Ladicek/dart-matchers
34 */
35 void expect(actual, matcher, {String reason, FailureHandler failureHandler,
36 bool verbose : false}) {
37 matcher = wrapMatcher(matcher);
38 bool doesMatch;
39 var matchState = new MatchState();
40 try {
41 doesMatch = matcher.matches(actual, matchState);
42 } catch (e, trace) {
43 doesMatch = false;
44 if (reason == null) {
45 reason = '${(e is String) ? e : e.toString()} at $trace';
46 }
47 }
48 if (!doesMatch) {
49 if (failureHandler == null) {
50 failureHandler = getOrCreateExpectFailureHandler();
51 }
52 failureHandler.failMatch(actual, matcher, reason, matchState, verbose);
53 }
54 }
55
56 /**
57 * Takes an argument and returns an equivalent matcher.
58 * If the argument is already a matcher this does nothing,
59 * else if the argument is a function, it generates a predicate
60 * function matcher, else it generates an equals matcher.
61 */
62 Matcher wrapMatcher(x) {
63 if (x is Matcher) {
64 return x;
65 } else if (x is Function) {
66 return predicate(x);
67 } else {
68 return equals(x);
69 }
70 }
71
72 // The handler for failed asserts.
73 FailureHandler _assertFailureHandler = null;
74
75 // The default failure handler that throws ExpectExceptions.
76 class DefaultFailureHandler implements FailureHandler {
77 DefaultFailureHandler() {
78 if (_assertErrorFormatter == null) {
79 _assertErrorFormatter = _defaultErrorFormatter;
80 }
81 }
82 void fail(String reason) {
83 throw new ExpectException(reason);
84 }
85 void failMatch(actual, Matcher matcher, String reason,
86 MatchState matchState, bool verbose) {
87 fail(_assertErrorFormatter(actual, matcher, reason, matchState, verbose));
88 }
89 }
90
91 /**
92 * Changes or resets to the default the failure handler for expect()
93 * [handler] is a reference to the new handler; if this is omitted
94 * or null then the failure handler is reset to the default, which
95 * throws [ExpectExceptions] on [expect] assertion failures.
96 */
97 void configureExpectFailureHandler([FailureHandler handler = null]) {
98 if (handler == null) {
99 handler = new DefaultFailureHandler();
100 }
101 _assertFailureHandler = handler;
102 }
103
104 FailureHandler getOrCreateExpectFailureHandler() {
105 if (_assertFailureHandler == null) {
106 configureExpectFailureHandler();
107 }
108 return _assertFailureHandler;
109 }
110
111 // The error message formatter for failed asserts.
112 ErrorFormatter _assertErrorFormatter = null;
113
114 // The default error formatter implementation.
115 String _defaultErrorFormatter(actual, Matcher matcher, String reason,
116 MatchState matchState, bool verbose) {
117 var description = new StringDescription();
118 description.add('Expected: ').addDescriptionOf(matcher).
119 add('\n but: ');
120 matcher.describeMismatch(actual, description, matchState, verbose);
121 description.add('.\n');
122 if (verbose && actual is Iterable) {
123 description.add('Actual: ').addDescriptionOf(actual).add('\n');
124 }
125 if (reason != null) {
126 description.add(reason).add('\n');
127 }
128 return description.toString();
129 }
130
131 /**
132 * Changes or resets to default the failure message formatter for expect().
133 * [formatter] is a reference to the new formatter; if this is omitted or
134 * null then the failure formatter is reset to the default. The new
135 * formatter is returned; this allows custom expect handlers to easily
136 * get a reference to the default formatter.
137 */
138 ErrorFormatter configureExpectFormatter([ErrorFormatter formatter = null]) {
139 if (formatter == null) {
140 formatter = _defaultErrorFormatter;
141 }
142 return _assertErrorFormatter = formatter;
143 }
144
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698