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