Chromium Code Reviews| OLD | NEW |
|---|---|
| 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]. [matcher] is optional and defaults to isTrue, | 7 * matches the [matcher]. [matcher] is optional and defaults to isTrue, |
| 8 * so expect can be used with a single predicate argument. [reason] | 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 | 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. | 10 * explicitly provided, as a reason can be generated from the matcher. |
| 11 * If [reason] is included it is appended to the reason generated | 11 * If [reason] is included it is appended to the reason generated |
| 12 * by the matcher. | 12 * by the matcher. |
| 13 * | 13 * |
| 14 * [matcher] can be a value in which case it will be wrapped in an | 14 * [matcher] can be a value in which case it will be wrapped in an |
| 15 * [equals] matcher. | 15 * [equals] matcher. |
| 16 * | 16 * |
| 17 * 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 |
| 18 * [ExpectException], but this behavior can be changed by calling | 18 * [ExpectException], but this behavior can be changed by calling |
| 19 * [configureExpectHandler] and providing an alternative handler that | 19 * [configureExpectHandler] and providing an alternative handler that |
| 20 * implements the [IFailureHandler] interface. | 20 * implements the [IFailureHandler] interface. It is also possible to |
| 21 * pass a [failureHandler] to [expect] as a final parameter for fine- | |
| 22 * grained control. | |
| 21 * | 23 * |
| 22 * expect() is a 3rd generation assertion mechanism, drawing | 24 * expect() is a 3rd generation assertion mechanism, drawing |
| 23 * inspiration from [Hamcrest] and Ladislav Thon's [dart-matchers] | 25 * inspiration from [Hamcrest] and Ladislav Thon's [dart-matchers] |
| 24 * library. | 26 * library. |
| 25 * | 27 * |
| 26 * See [Hamcrest] http://en.wikipedia.org/wiki/Hamcrest | 28 * See [Hamcrest] http://en.wikipedia.org/wiki/Hamcrest |
| 27 * [Hamcrest] http://http://code.google.com/p/hamcrest/ | 29 * [Hamcrest] http://http://code.google.com/p/hamcrest/ |
| 28 * [dart-matchers] https://github.com/Ladicek/dart-matchers | 30 * [dart-matchers] https://github.com/Ladicek/dart-matchers |
| 29 */ | 31 */ |
| 30 void expect(actual, [matcher = isTrue, String reason = null]) { | 32 void expect(actual, [matcher = isTrue, String reason = null, |
| 33 failureHandler = null]) { | |
| 31 matcher = wrapMatcher(matcher); | 34 matcher = wrapMatcher(matcher); |
| 32 var doesMatch; | 35 var doesMatch; |
| 33 try { | 36 try { |
| 34 doesMatch = matcher.matches(actual); | 37 doesMatch = matcher.matches(actual); |
| 35 } catch (var e, var trace) { | 38 } catch (var e, var trace) { |
| 36 doesMatch = false; | 39 doesMatch = false; |
| 37 if (reason == null) { | 40 if (reason == null) { |
| 38 reason = '${(e is String) ? e : e.toString()} at $trace'; | 41 reason = '${(e is String) ? e : e.toString()} at $trace'; |
| 39 } | 42 } |
| 40 } | 43 } |
| 41 if (!doesMatch) { | 44 if (!doesMatch) { |
| 42 // Make sure we have a failure handler configured. | 45 if (failureHandler == null) { |
| 43 configureExpectHandler(_assertFailureHandler); | 46 // Make sure we have a failure handler configured. |
| 44 _assertFailureHandler.failMatch(actual, matcher, reason); | 47 if (_assertFailureHandler == null) { |
| 48 configureExpectHandler(); | |
| 49 } | |
| 50 failureHandler = _assertFailureHandler; | |
| 51 } | |
| 52 failureHandler.failMatch(actual, matcher, reason); | |
| 45 } | 53 } |
| 46 } | 54 } |
| 47 | 55 |
| 48 /** | 56 /** |
| 49 * Takes an argument and returns an equivalent matcher. | 57 * Takes an argument and returns an equivalent matcher. |
| 50 * If the argument is already a matcher this does nothing, | 58 * If the argument is already a matcher this does nothing, |
| 51 * else if the argument is a function, it generates a predicate | 59 * else if the argument is a function, it generates a predicate |
| 52 * function matcher, else it generates an equals matcher. | 60 * function matcher, else it generates an equals matcher. |
| 53 */ | 61 */ |
| 54 Matcher wrapMatcher(x) { | 62 Matcher wrapMatcher(x) { |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 85 * or null then the failure handler is reset to the default, which | 93 * or null then the failure handler is reset to the default, which |
| 86 * throws [ExpectExceptions] on [expect] assertion failures. | 94 * throws [ExpectExceptions] on [expect] assertion failures. |
| 87 */ | 95 */ |
| 88 void configureExpectHandler([FailureHandler handler = null]) { | 96 void configureExpectHandler([FailureHandler handler = null]) { |
| 89 if (handler == null) { | 97 if (handler == null) { |
| 90 handler = new DefaultFailureHandler(); | 98 handler = new DefaultFailureHandler(); |
| 91 } | 99 } |
| 92 _assertFailureHandler = handler; | 100 _assertFailureHandler = handler; |
| 93 } | 101 } |
| 94 | 102 |
| 103 FailureHandler getFailureHandler() { | |
|
Siggi Cherem (dart-lang)
2012/06/26 21:35:44
please use a getter, or change to getOrCreateFailu
gram
2012/06/26 23:39:59
Done.
| |
| 104 if (_assertFailureHandler == null) { | |
| 105 configureExpectHandler(); | |
| 106 } | |
| 107 return _assertFailureHandler; | |
| 108 } | |
| 109 | |
| 95 // The error message formatter for failed asserts. | 110 // The error message formatter for failed asserts. |
| 96 ErrorFormatter _assertErrorFormatter = null; | 111 ErrorFormatter _assertErrorFormatter = null; |
| 97 | 112 |
| 98 // The default error formatter implementation. | 113 // The default error formatter implementation. |
| 99 String _defaultErrorFormatter(actual, Matcher matcher, String reason) { | 114 String _defaultErrorFormatter(actual, Matcher matcher, String reason) { |
| 100 var description = new StringDescription(); | 115 var description = new StringDescription(); |
| 101 description.add('Expected: ').addDescriptionOf(matcher). | 116 description.add('Expected: ').addDescriptionOf(matcher). |
| 102 add('\n but: '); | 117 add('\n but: '); |
| 103 matcher.describeMismatch(actual, description); | 118 matcher.describeMismatch(actual, description); |
| 104 if (reason != null) { | 119 if (reason != null) { |
| 105 description.add('\n').add(reason).add('\n'); | 120 description.add('\n').add(reason).add('\n'); |
| 106 } | 121 } |
| 107 return description.toString(); | 122 return description.toString(); |
| 108 } | 123 } |
| 109 | 124 |
| 110 /** | 125 /** |
| 111 * Changes or resets to default the failure message formatter for expect(). | 126 * Changes or resets to default the failure message formatter for expect(). |
| 112 * [formatter] is a reference to the new formatter; if this is omitted or | 127 * [formatter] is a reference to the new formatter; if this is omitted or |
| 113 * null then the failure formatter is reset to the default. The new | 128 * null then the failure formatter is reset to the default. The new |
| 114 * formatter is returned; this allows custom expect handlers to easily | 129 * formatter is returned; this allows custom expect handlers to easily |
| 115 * get a reference to the default formatter. | 130 * get a reference to the default formatter. |
| 116 */ | 131 */ |
| 117 ErrorFormatter configureExpectFormatter([ErrorFormatter formatter = null]) { | 132 ErrorFormatter configureExpectFormatter([ErrorFormatter formatter = null]) { |
| 118 if (formatter == null) { | 133 if (formatter == null) { |
| 119 formatter = _defaultErrorFormatter; | 134 formatter = _defaultErrorFormatter; |
| 120 } | 135 } |
| 121 return _assertErrorFormatter = formatter; | 136 return _assertErrorFormatter = formatter; |
| 122 } | 137 } |
| 123 | 138 |
| OLD | NEW |