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]. [reason] is optional and is typically | 7 * matches the [matcher]. [reason] is optional and is typically |
| 8 * not supplied, as a reason can be generated from the matcher. | 8 * not supplied, as a reason can be generated from the matcher. |
| 9 * If [reason] is included it is appended to the reason generated | 9 * If [reason] is included it is appended to the reason generated |
| 10 * by the matcher. | 10 * by the matcher. |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 25 * expect(foo <= bar); | 25 * expect(foo <= bar); |
| 26 * | 26 * |
| 27 * expect() is a 3rd generation assertion mechanism, drawing | 27 * expect() is a 3rd generation assertion mechanism, drawing |
| 28 * inspiration from [Hamcrest] and Ladislav Thon's [dart-matchers] | 28 * inspiration from [Hamcrest] and Ladislav Thon's [dart-matchers] |
| 29 * library. | 29 * library. |
| 30 * | 30 * |
| 31 * See [Hamcrest] http://en.wikipedia.org/wiki/Hamcrest | 31 * See [Hamcrest] http://en.wikipedia.org/wiki/Hamcrest |
| 32 * [Hamcrest] http://http://code.google.com/p/hamcrest/ | 32 * [Hamcrest] http://http://code.google.com/p/hamcrest/ |
| 33 * [dart-matchers] https://github.com/Ladicek/dart-matchers | 33 * [dart-matchers] https://github.com/Ladicek/dart-matchers |
| 34 */ | 34 */ |
| 35 void expect(actual, [matcherOrReason = null, String reason = '']) { | 35 void expect(actual, [matcherOrReason = null, String reason = '']) { |
|
eub
2012/06/15 21:02:49
BTW, I imagine you had prior review discussion of
gram
2012/06/15 21:25:55
Yes - the aim is to have just one expect(), but to
| |
| 36 if (matcherOrReason is Matcher) { | 36 if (actual is bool && |
| 37 (matcherOrReason == null || matcherOrReason is String)) { | |
| 38 // Treat this as an assert(predicate, reason). | |
| 39 if (!actual) { | |
| 40 reason = (matcherOrReason == null) ? 'Assertion failed' : matcherOrReason; | |
| 41 // Make sure we have a failure handler configured. | |
| 42 configureExpectHandler(_assertFailureHandler); | |
| 43 _assertFailureHandler.fail(reason); | |
| 44 } | |
| 45 } else { | |
| 46 // Treat this as an expect(value, matcher, [reason]). | |
| 47 if (matcherOrReason is! Matcher) { | |
| 48 matcherOrReason = wrapMatcher(matcherOrReason); | |
| 49 } | |
| 37 var doesMatch; | 50 var doesMatch; |
| 38 try { | 51 try { |
| 39 doesMatch = matcherOrReason.matches(actual); | 52 doesMatch = matcherOrReason.matches(actual); |
| 40 } catch (var e, var trace) { | 53 } catch (var e, var trace) { |
| 41 doesMatch = false; | 54 doesMatch = false; |
| 42 if (reason == '') { | 55 if (reason == '') { |
| 43 reason = '${(e is String) ? e : e.toString()} at $trace'; | 56 reason = '${(e is String) ? e : e.toString()} at $trace'; |
| 44 } | 57 } |
| 45 } | 58 } |
| 46 if (!doesMatch) { | 59 if (!doesMatch) { |
| 47 // Make sure we have a failure handler configured. | 60 // Make sure we have a failure handler configured. |
| 48 configureExpectHandler(_assertFailureHandler); | 61 configureExpectHandler(_assertFailureHandler); |
| 49 _assertFailureHandler.failMatch(actual, matcherOrReason, reason); | 62 _assertFailureHandler.failMatch(actual, matcherOrReason, reason); |
| 50 } | 63 } |
| 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 } | 64 } |
| 59 } | 65 } |
| 60 | 66 |
| 61 /** | 67 /** |
| 62 * Takes an argument and returns an equivalent matcher. | 68 * Takes an argument and returns an equivalent matcher. |
| 63 * If the argument is already a matcher this does nothing, else it | 69 * If the argument is already a matcher this does nothing, else it |
| 64 * generates an equals matcher for the argument. | 70 * generates an equals matcher for the argument. |
| 65 */ | 71 */ |
| 66 Matcher wrapMatcher(x) => ((x is Matcher) ? x : equals(x)); | 72 Matcher wrapMatcher(x) => ((x is Matcher) ? x : equals(x)); |
| 67 | 73 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 116 * formatter is returned; this allows custom expect handlers to easily | 122 * formatter is returned; this allows custom expect handlers to easily |
| 117 * get a reference to the default formatter. | 123 * get a reference to the default formatter. |
| 118 */ | 124 */ |
| 119 ErrorFormatter configureExpectFormatter([ErrorFormatter formatter = null]) { | 125 ErrorFormatter configureExpectFormatter([ErrorFormatter formatter = null]) { |
| 120 if (formatter == null) { | 126 if (formatter == null) { |
| 121 formatter = _defaultErrorFormatter; | 127 formatter = _defaultErrorFormatter; |
| 122 } | 128 } |
| 123 return _assertErrorFormatter = formatter; | 129 return _assertErrorFormatter = formatter; |
| 124 } | 130 } |
| 125 | 131 |
| OLD | NEW |