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

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

Issue 10579008: Added test setup/teardown. (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
« no previous file with comments | « lib/unittest/description.dart ('k') | lib/unittest/map_matchers.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 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 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 /** 5 /**
6 * This is the main assertion function. It asserts that [actual] 6 * This is the main assertion function. It asserts that [actual]
7 * matches the [matcher]. [reason] is optional and is typically 7 * matches the [matcher]. [matcher] is optional and defaults to isTrue,
8 * not supplied, as a reason can be generated from the matcher. 8 * so expect can be used with a single predicate argument. [reason]
9 * is optional and is typically not supplied if a reasonable matcher is
10 * explicitly provided, as a reason can be generated from the matcher.
9 * If [reason] is included it is appended to the reason generated 11 * If [reason] is included it is appended to the reason generated
10 * by the matcher. 12 * by the matcher.
11 * 13 *
14 * [matcher] can be a value in which case it will be wrapped in an
15 * [equals] matcher.
16 *
12 * If the assertion fails, then the default behavior is to throw an 17 * If the assertion fails, then the default behavior is to throw an
13 * [ExpectException], but this behavior can be changed by calling 18 * [ExpectException], but this behavior can be changed by calling
14 * [configureExpectHandler] and providing an alternative handler that 19 * [configureExpectHandler] and providing an alternative handler that
15 * implements the [IFailureHandler] interface. 20 * implements the [IFailureHandler] interface.
16 * 21 *
17 * [expect] allows an alternative call format, providing a Boolean
18 * predicate as the first argument and an optional reason as a named
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 * A better way of doing the second form is:
28 *
29 * expect(foo <= bar, reason: "foo not less than or equal to bar");
30 *
31 * expect() is a 3rd generation assertion mechanism, drawing 22 * expect() is a 3rd generation assertion mechanism, drawing
32 * inspiration from [Hamcrest] and Ladislav Thon's [dart-matchers] 23 * inspiration from [Hamcrest] and Ladislav Thon's [dart-matchers]
33 * library. 24 * library.
34 * 25 *
35 * See [Hamcrest] http://en.wikipedia.org/wiki/Hamcrest 26 * See [Hamcrest] http://en.wikipedia.org/wiki/Hamcrest
36 * [Hamcrest] http://http://code.google.com/p/hamcrest/ 27 * [Hamcrest] http://http://code.google.com/p/hamcrest/
37 * [dart-matchers] https://github.com/Ladicek/dart-matchers 28 * [dart-matchers] https://github.com/Ladicek/dart-matchers
38 */ 29 */
39 void expect(actual, [matcher = null, String reason = null]) { 30 void expect(actual, [matcher = isTrue, String reason = null]) {
40 if (matcher == null) { 31 matcher = wrapMatcher(matcher);
41 // Treat this as an assert(predicate, [reason]). 32 var doesMatch;
42 if (!actual) { 33 try {
43 if (reason == null) { 34 doesMatch = matcher.matches(actual);
44 reason = 'Assertion failed'; 35 } catch (var e, var trace) {
45 } 36 doesMatch = false;
46 // Make sure we have a failure handler configured. 37 if (reason == null) {
47 configureExpectHandler(_assertFailureHandler); 38 reason = '${(e is String) ? e : e.toString()} at $trace';
48 _assertFailureHandler.fail(reason);
49 } 39 }
50 } else { 40 }
51 // Treat this as an expect(value, [matcher], [reason]). 41 if (!doesMatch) {
52 matcher = wrapMatcher(matcher); 42 // Make sure we have a failure handler configured.
53 var doesMatch; 43 configureExpectHandler(_assertFailureHandler);
54 try { 44 _assertFailureHandler.failMatch(actual, matcher, reason);
55 doesMatch = matcher.matches(actual);
56 } catch (var e, var trace) {
57 doesMatch = false;
58 if (reason == null) {
59 reason = '${(e is String) ? e : e.toString()} at $trace';
60 }
61 }
62 if (!doesMatch) {
63 // Make sure we have a failure handler configured.
64 configureExpectHandler(_assertFailureHandler);
65 _assertFailureHandler.failMatch(actual, matcher, reason);
66 }
67 } 45 }
68 } 46 }
69 47
70 /** 48 /**
71 * Takes an argument and returns an equivalent matcher. 49 * Takes an argument and returns an equivalent matcher.
72 * If the argument is already a matcher this does nothing, else it 50 * If the argument is already a matcher this does nothing,
73 * generates an equals matcher for the argument. 51 * else if the argument is a function, it generates a predicate
52 * function matcher, else it generates an equals matcher.
74 */ 53 */
75 Matcher wrapMatcher(x) => ((x is Matcher) ? x : equals(x)); 54 Matcher wrapMatcher(x) {
55 if (x is Matcher) {
56 return x;
57 } else if (x is Function) {
58 return predicate(x);
59 } else {
60 return equals(x);
61 }
62 }
76 63
77 // The handler for failed asserts. 64 // The handler for failed asserts.
78 FailureHandler _assertFailureHandler = null; 65 FailureHandler _assertFailureHandler = null;
79 66
80 // The default failure handler that throws ExpectExceptions. 67 // The default failure handler that throws ExpectExceptions.
81 class DefaultFailureHandler implements FailureHandler { 68 class DefaultFailureHandler implements FailureHandler {
82 DefaultFailureHandler() { 69 DefaultFailureHandler() {
83 if (_assertErrorFormatter == null) { 70 if (_assertErrorFormatter == null) {
84 _assertErrorFormatter = _defaultErrorFormatter; 71 _assertErrorFormatter = _defaultErrorFormatter;
85 } 72 }
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
127 * formatter is returned; this allows custom expect handlers to easily 114 * formatter is returned; this allows custom expect handlers to easily
128 * get a reference to the default formatter. 115 * get a reference to the default formatter.
129 */ 116 */
130 ErrorFormatter configureExpectFormatter([ErrorFormatter formatter = null]) { 117 ErrorFormatter configureExpectFormatter([ErrorFormatter formatter = null]) {
131 if (formatter == null) { 118 if (formatter == null) {
132 formatter = _defaultErrorFormatter; 119 formatter = _defaultErrorFormatter;
133 } 120 }
134 return _assertErrorFormatter = formatter; 121 return _assertErrorFormatter = formatter;
135 } 122 }
136 123
OLDNEW
« no previous file with comments | « lib/unittest/description.dart ('k') | lib/unittest/map_matchers.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698