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