OLD | NEW |
(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 configured. |
| 48 configureExpectHandler(_assertFailureHandler); |
| 49 _assertFailureHandler.failMatch(actual, matcherOrReason, reason); |
| 50 } |
| 51 } else { |
| 52 if (!actual) { |
| 53 reason = (matcherOrReason == null) ? 'Assertion failed' : matcherOrReason; |
| 54 // Make sure we have a failure handler configured. |
| 55 configureExpectHandler(_assertFailureHandler); |
| 56 _assertFailureHandler.fail(reason); |
| 57 } |
| 58 } |
| 59 } |
| 60 |
| 61 /** |
| 62 * Takes an argument and returns an equivalent matcher. |
| 63 * If the argument is already a matcher this does nothing, else it |
| 64 * generates an equals matcher for the argument. |
| 65 */ |
| 66 Matcher wrapMatcher(x) => ((x is Matcher) ? x : equals(x)); |
| 67 |
| 68 // The handler for failed asserts. |
| 69 FailureHandler _assertFailureHandler = null; |
| 70 |
| 71 // The default failure handler that throws ExpectExceptions. |
| 72 class DefaultFailureHandler implements FailureHandler { |
| 73 DefaultFailureHandler() { |
| 74 if (_assertErrorFormatter == null) { |
| 75 _assertErrorFormatter = _defaultErrorFormatter; |
| 76 } |
| 77 } |
| 78 void fail(String reason) { |
| 79 throw new ExpectException(reason); |
| 80 } |
| 81 void failMatch(actual, Matcher matcher, String reason) { |
| 82 fail(_assertErrorFormatter(actual, matcher, reason)); |
| 83 } |
| 84 } |
| 85 |
| 86 /** |
| 87 * Changes or resets to the default the failure handler for expect() |
| 88 * [handler] is a reference to the new handler; if this is omitted |
| 89 * or null then the failure handler is reset to the default, which |
| 90 * throws [ExpectExceptions] on [expect] assertion failures. |
| 91 */ |
| 92 void configureExpectHandler([FailureHandler handler = null]) { |
| 93 if (handler == null) { |
| 94 handler = new DefaultFailureHandler(); |
| 95 } |
| 96 _assertFailureHandler = handler; |
| 97 } |
| 98 |
| 99 // The error message formatter for failed asserts. |
| 100 ErrorFormatter _assertErrorFormatter = null; |
| 101 |
| 102 // The default error formatter implementation. |
| 103 String _defaultErrorFormatter(actual, Matcher matcher, String reason) { |
| 104 var description = new StringDescription(); |
| 105 description.add('Expected: ').addDescriptionOf(matcher). |
| 106 add('\n but: '); |
| 107 matcher.describeMismatch(actual, description); |
| 108 description.add('\n').add(reason).add('\n'); |
| 109 return description.toString(); |
| 110 } |
| 111 |
| 112 /** |
| 113 * Changes or resets to default the failure message formatter for expect(). |
| 114 * [formatter] is a reference to the new formatter; if this is omitted or |
| 115 * null then the failure formatter is reset to the default. The new |
| 116 * formatter is returned; this allows custom expect handlers to easily |
| 117 * get a reference to the default formatter. |
| 118 */ |
| 119 ErrorFormatter configureExpectFormatter([ErrorFormatter formatter = null]) { |
| 120 if (formatter == null) { |
| 121 formatter = _defaultErrorFormatter; |
| 122 } |
| 123 return _assertErrorFormatter = formatter; |
| 124 } |
| 125 |
OLD | NEW |