| 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 part of matcher; | 5 library matcher.numeric_matchers; |
| 6 | 6 |
| 7 /** | 7 import 'interfaces.dart'; |
| 8 * Returns a matcher which matches if the match argument is greater | 8 |
| 9 * than the given [value]. | 9 /// Returns a matcher which matches if the match argument is greater |
| 10 */ | 10 /// than the given [value]. |
| 11 Matcher greaterThan(value) => | 11 Matcher greaterThan(value) => |
| 12 new _OrderingComparison(value, false, false, true, 'a value greater than'); | 12 new _OrderingComparison(value, false, false, true, 'a value greater than'); |
| 13 | 13 |
| 14 /** | 14 /// Returns a matcher which matches if the match argument is greater |
| 15 * Returns a matcher which matches if the match argument is greater | 15 /// than or equal to the given [value]. |
| 16 * than or equal to the given [value]. | |
| 17 */ | |
| 18 Matcher greaterThanOrEqualTo(value) => | 16 Matcher greaterThanOrEqualTo(value) => |
| 19 new _OrderingComparison(value, true, false, true, | 17 new _OrderingComparison(value, true, false, true, |
| 20 'a value greater than or equal to'); | 18 'a value greater than or equal to'); |
| 21 | 19 |
| 22 /** | 20 /// Returns a matcher which matches if the match argument is less |
| 23 * Returns a matcher which matches if the match argument is less | 21 /// than the given [value]. |
| 24 * than the given [value]. | |
| 25 */ | |
| 26 Matcher lessThan(value) => | 22 Matcher lessThan(value) => |
| 27 new _OrderingComparison(value, false, true, false, 'a value less than'); | 23 new _OrderingComparison(value, false, true, false, 'a value less than'); |
| 28 | 24 |
| 29 /** | 25 /// Returns a matcher which matches if the match argument is less |
| 30 * Returns a matcher which matches if the match argument is less | 26 /// than or equal to the given [value]. |
| 31 * than or equal to the given [value]. | |
| 32 */ | |
| 33 Matcher lessThanOrEqualTo(value) => | 27 Matcher lessThanOrEqualTo(value) => |
| 34 new _OrderingComparison(value, true, true, false, | 28 new _OrderingComparison(value, true, true, false, |
| 35 'a value less than or equal to'); | 29 'a value less than or equal to'); |
| 36 | 30 |
| 37 /** | 31 /// A matcher which matches if the match argument is zero. |
| 38 * A matcher which matches if the match argument is zero. | |
| 39 */ | |
| 40 const Matcher isZero = | 32 const Matcher isZero = |
| 41 const _OrderingComparison(0, true, false, false, 'a value equal to'); | 33 const _OrderingComparison(0, true, false, false, 'a value equal to'); |
| 42 | 34 |
| 43 | 35 |
| 44 /** | 36 /// A matcher which matches if the match argument is non-zero. |
| 45 * A matcher which matches if the match argument is non-zero. | |
| 46 */ | |
| 47 const Matcher isNonZero = | 37 const Matcher isNonZero = |
| 48 const _OrderingComparison(0, false, true, true, 'a value not equal to'); | 38 const _OrderingComparison(0, false, true, true, 'a value not equal to'); |
| 49 | 39 |
| 50 /** | 40 /// A matcher which matches if the match argument is positive. |
| 51 * A matcher which matches if the match argument is positive. | |
| 52 */ | |
| 53 const Matcher isPositive = | 41 const Matcher isPositive = |
| 54 const _OrderingComparison(0, false, false, true, 'a positive value', false); | 42 const _OrderingComparison(0, false, false, true, 'a positive value', false); |
| 55 | 43 |
| 56 /** | 44 /// A matcher which matches if the match argument is zero or negative. |
| 57 * A matcher which matches if the match argument is zero or negative. | |
| 58 */ | |
| 59 const Matcher isNonPositive = | 45 const Matcher isNonPositive = |
| 60 const _OrderingComparison(0, true, true, false, | 46 const _OrderingComparison(0, true, true, false, |
| 61 'a non-positive value', false); | 47 'a non-positive value', false); |
| 62 | 48 |
| 63 /** | 49 /// A matcher which matches if the match argument is negative. |
| 64 * A matcher which matches if the match argument is negative. | |
| 65 */ | |
| 66 const Matcher isNegative = | 50 const Matcher isNegative = |
| 67 const _OrderingComparison(0, false, true, false, 'a negative value', false); | 51 const _OrderingComparison(0, false, true, false, 'a negative value', false); |
| 68 | 52 |
| 69 /** | 53 /// A matcher which matches if the match argument is zero or positive. |
| 70 * A matcher which matches if the match argument is zero or positive. | |
| 71 */ | |
| 72 const Matcher isNonNegative = | 54 const Matcher isNonNegative = |
| 73 const _OrderingComparison(0, true, false, true, | 55 const _OrderingComparison(0, true, false, true, |
| 74 'a non-negative value', false); | 56 'a non-negative value', false); |
| 75 | 57 |
| 76 bool _isNumeric(value) { | 58 bool _isNumeric(value) { |
| 77 return value is num; | 59 return value is num; |
| 78 } | 60 } |
| 79 | 61 |
| 62 // TODO(kevmoo) Note that matchers that use _OrderingComparison only use |
| 63 // `==` and `<` operators to evaluate the match. Or change the matcher. |
| 80 class _OrderingComparison extends Matcher { | 64 class _OrderingComparison extends Matcher { |
| 81 /** Expected value. */ | 65 /// Expected value. |
| 82 final _value; | 66 final _value; |
| 83 /** What to return if actual == expected */ | 67 /// What to return if actual == expected |
| 84 final bool _equalValue; | 68 final bool _equalValue; |
| 85 /** What to return if actual < expected */ | 69 /// What to return if actual < expected |
| 86 final bool _lessThanValue; | 70 final bool _lessThanValue; |
| 87 /** What to return if actual > expected */ | 71 /// What to return if actual > expected |
| 88 final bool _greaterThanValue; | 72 final bool _greaterThanValue; |
| 89 /** Textual name of the inequality */ | 73 /// Textual name of the inequality |
| 90 final String _comparisonDescription; | 74 final String _comparisonDescription; |
| 91 /** Whether to include the expected value in the description */ | 75 /// Whether to include the expected value in the description |
| 92 final bool _valueInDescription; | 76 final bool _valueInDescription; |
| 93 | 77 |
| 94 const _OrderingComparison( | 78 const _OrderingComparison( |
| 95 this._value, | 79 this._value, |
| 96 this._equalValue, | 80 this._equalValue, |
| 97 this._lessThanValue, | 81 this._lessThanValue, |
| 98 this._greaterThanValue, | 82 this._greaterThanValue, |
| 99 this._comparisonDescription, | 83 this._comparisonDescription, |
| 100 [valueInDescription = true]) : | 84 [bool valueInDescription = true]) : |
| 101 this._valueInDescription = valueInDescription; | 85 this._valueInDescription = valueInDescription; |
| 102 | 86 |
| 103 bool matches(item, Map matchState) { | 87 bool matches(item, Map matchState) { |
| 104 if (item == _value) { | 88 if (item == _value) { |
| 105 return _equalValue; | 89 return _equalValue; |
| 106 } else if (item < _value) { | 90 } else if (item < _value) { |
| 107 return _lessThanValue; | 91 return _lessThanValue; |
| 108 } else { | 92 } else { |
| 109 return _greaterThanValue; | 93 return _greaterThanValue; |
| 110 } | 94 } |
| 111 } | 95 } |
| 112 | 96 |
| 113 Description describe(Description description) { | 97 Description describe(Description description) { |
| 114 if (_valueInDescription) { | 98 if (_valueInDescription) { |
| 115 return description.add(_comparisonDescription).add(' '). | 99 return description.add(_comparisonDescription).add(' '). |
| 116 addDescriptionOf(_value); | 100 addDescriptionOf(_value); |
| 117 } else { | 101 } else { |
| 118 return description.add(_comparisonDescription); | 102 return description.add(_comparisonDescription); |
| 119 } | 103 } |
| 120 } | 104 } |
| 121 | 105 |
| 122 Description describeMismatch(item, Description mismatchDescription, | 106 Description describeMismatch(item, Description mismatchDescription, |
| 123 Map matchState, bool verbose) { | 107 Map matchState, bool verbose) { |
| 124 mismatchDescription.add('is not '); | 108 mismatchDescription.add('is not '); |
| 125 return describe(mismatchDescription); | 109 return describe(mismatchDescription); |
| 126 } | 110 } |
| 127 } | 111 } |
| 128 | 112 |
| 129 /** | 113 /// Returns a matcher which matches if the match argument is within [delta] |
| 130 * Returns a matcher which matches if the match argument is within [delta] | 114 /// of some [value]; i.e. if the match argument is greater than |
| 131 * of some [value]; i.e. if the match argument is greater than | 115 /// than or equal [value]-[delta] and less than or equal to [value]+[delta]. |
| 132 * than or equal [value]-[delta] and less than or equal to [value]+[delta]. | 116 Matcher closeTo(num value, num delta) => new _IsCloseTo(value, delta); |
| 133 */ | |
| 134 Matcher closeTo(value, delta) => new _IsCloseTo(value, delta); | |
| 135 | 117 |
| 136 class _IsCloseTo extends Matcher { | 118 class _IsCloseTo extends Matcher { |
| 137 final num _value, _delta; | 119 final num _value, _delta; |
| 138 | 120 |
| 139 const _IsCloseTo(this._value, this._delta); | 121 const _IsCloseTo(this._value, this._delta); |
| 140 | 122 |
| 141 bool matches(item, Map matchState) { | 123 bool matches(item, Map matchState) { |
| 142 if (!_isNumeric(item)) { | 124 if (!_isNumeric(item)) { |
| 143 return false; | 125 return false; |
| 144 } | 126 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 160 } else { | 142 } else { |
| 161 var diff = item - _value; | 143 var diff = item - _value; |
| 162 if (diff < 0) diff = -diff; | 144 if (diff < 0) diff = -diff; |
| 163 return mismatchDescription. | 145 return mismatchDescription. |
| 164 add(' differs by '). | 146 add(' differs by '). |
| 165 addDescriptionOf(diff); | 147 addDescriptionOf(diff); |
| 166 } | 148 } |
| 167 } | 149 } |
| 168 } | 150 } |
| 169 | 151 |
| 170 /** | 152 /// Returns a matcher which matches if the match argument is greater |
| 171 * Returns a matcher which matches if the match argument is greater | 153 /// than or equal to [low] and less than or equal to [high]. |
| 172 * than or equal to [low] and less than or equal to [high]. | 154 Matcher inInclusiveRange(num low, num high) => |
| 173 */ | 155 new _InRange(low, high, true, true); |
| 174 Matcher inInclusiveRange(low, high) => new _InRange(low, high, true, true); | |
| 175 | 156 |
| 176 /** | 157 /// Returns a matcher which matches if the match argument is greater |
| 177 * Returns a matcher which matches if the match argument is greater | 158 /// than [low] and less than [high]. |
| 178 * than [low] and less than [high]. | 159 Matcher inExclusiveRange(num low, num high) => |
| 179 */ | 160 new _InRange(low, high, false, false); |
| 180 Matcher inExclusiveRange(low, high) => new _InRange(low, high, false, false); | |
| 181 | 161 |
| 182 /** | 162 /// Returns a matcher which matches if the match argument is greater |
| 183 * Returns a matcher which matches if the match argument is greater | 163 /// than [low] and less than or equal to [high]. |
| 184 * than [low] and less than or equal to [high]. | 164 Matcher inOpenClosedRange(num low, num high) => |
| 185 */ | 165 new _InRange(low, high, false, true); |
| 186 Matcher inOpenClosedRange(low, high) => new _InRange(low, high, false, true); | |
| 187 | 166 |
| 188 /** | 167 /// Returns a matcher which matches if the match argument is greater |
| 189 * Returns a matcher which matches if the match argument is greater | 168 /// than or equal to a [low] and less than [high]. |
| 190 * than or equal to a [low] and less than [high]. | 169 Matcher inClosedOpenRange(num low, num high) => |
| 191 */ | 170 new _InRange(low, high, true, false); |
| 192 Matcher inClosedOpenRange(low, high) => new _InRange(low, high, true, false); | |
| 193 | 171 |
| 194 class _InRange extends Matcher { | 172 class _InRange extends Matcher { |
| 195 final num _low, _high; | 173 final num _low, _high; |
| 196 final bool _lowMatchValue, _highMatchValue; | 174 final bool _lowMatchValue, _highMatchValue; |
| 197 | 175 |
| 198 const _InRange(this._low, this._high, | 176 const _InRange(this._low, this._high, |
| 199 this._lowMatchValue, this._highMatchValue); | 177 this._lowMatchValue, this._highMatchValue); |
| 200 | 178 |
| 201 bool matches(value, Map matchState) { | 179 bool matches(value, Map matchState) { |
| 202 if (value is! num) { | 180 if (value is! num) { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 224 if (item is !num) { | 202 if (item is !num) { |
| 225 return mismatchDescription. | 203 return mismatchDescription. |
| 226 addDescriptionOf(item). | 204 addDescriptionOf(item). |
| 227 add(' not numeric'); | 205 add(' not numeric'); |
| 228 } else { | 206 } else { |
| 229 return super.describeMismatch(item, mismatchDescription, | 207 return super.describeMismatch(item, mismatchDescription, |
| 230 matchState, verbose); | 208 matchState, verbose); |
| 231 } | 209 } |
| 232 } | 210 } |
| 233 } | 211 } |
| 234 | |
| OLD | NEW |