| 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 const 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 const 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 const 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 const 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 const 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 const 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 [valueInDescription = true]) : | |
| 99 this._valueInDescription = valueInDescription; | |
| 100 | |
| 101 bool matches(item, MatchState matchState) { | |
| 102 if (item == _value) { | |
| 103 return _equalValue; | |
| 104 } else if (item < _value) { | |
| 105 return _lessThanValue; | |
| 106 } else { | |
| 107 return _greaterThanValue; | |
| 108 } | |
| 109 } | |
| 110 | |
| 111 Description describe(Description description) { | |
| 112 if (_valueInDescription) { | |
| 113 return description.add(_comparisonDescription).add(' '). | |
| 114 addDescriptionOf(_value); | |
| 115 } else { | |
| 116 return description.add(_comparisonDescription); | |
| 117 } | |
| 118 } | |
| 119 } | |
| 120 | |
| 121 /** | |
| 122 * Returns a matcher which matches if the match argument is within [delta] | |
| 123 * of some [value]; i.e. if the match argument is greater than | |
| 124 * than or equal [value]-[delta] and less than or equal to [value]+[delta]. | |
| 125 */ | |
| 126 Matcher closeTo(value, delta) => new _IsCloseTo(value, delta); | |
| 127 | |
| 128 class _IsCloseTo extends BaseMatcher { | |
| 129 final num _value, _delta; | |
| 130 | |
| 131 const _IsCloseTo(this._value, this._delta); | |
| 132 | |
| 133 bool matches(item, MatchState matchState) { | |
| 134 if (!_isNumeric(item)) { | |
| 135 return false; | |
| 136 } | |
| 137 var diff = item - _value; | |
| 138 if (diff < 0) diff = -diff; | |
| 139 return (diff <= _delta); | |
| 140 } | |
| 141 | |
| 142 Description describe(Description description) => | |
| 143 description.add('a numeric value within '). | |
| 144 addDescriptionOf(_delta). | |
| 145 add(' of '). | |
| 146 addDescriptionOf(_value); | |
| 147 | |
| 148 Description describeMismatch(item, Description mismatchDescription, | |
| 149 MatchState matchState, bool verbose) { | |
| 150 if (item is !num) { | |
| 151 return mismatchDescription. | |
| 152 addDescriptionOf(item). | |
| 153 add(' not numeric'); | |
| 154 } else { | |
| 155 var diff = item - _value; | |
| 156 if (diff < 0) diff = -diff; | |
| 157 return mismatchDescription. | |
| 158 addDescriptionOf(item). | |
| 159 add(' differed by '). | |
| 160 addDescriptionOf(diff); | |
| 161 } | |
| 162 } | |
| 163 } | |
| 164 | |
| 165 /** | |
| 166 * Returns a matcher which matches if the match argument is greater | |
| 167 * than or equal to [low] and less than or equal to [high]. | |
| 168 */ | |
| 169 Matcher inInclusiveRange(low, high) => new _InRange(low, high, true, true); | |
| 170 | |
| 171 /** | |
| 172 * Returns a matcher which matches if the match argument is greater | |
| 173 * than [low] and less than [high]. | |
| 174 */ | |
| 175 Matcher inExclusiveRange(low, high) => new _InRange(low, high, false, false); | |
| 176 | |
| 177 /** | |
| 178 * Returns a matcher which matches if the match argument is greater | |
| 179 * than [low] and less than or equal to [high]. | |
| 180 */ | |
| 181 Matcher inOpenClosedRange(low, high) => new _InRange(low, high, false, true); | |
| 182 | |
| 183 /** | |
| 184 * Returns a matcher which matches if the match argument is greater | |
| 185 * than or equal to a [low] and less than [high]. | |
| 186 */ | |
| 187 Matcher inClosedOpenRange(low, high) => new _InRange(low, high, true, false); | |
| 188 | |
| 189 class _InRange extends BaseMatcher { | |
| 190 final num _low, _high; | |
| 191 final bool _lowMatchValue, _highMatchValue; | |
| 192 | |
| 193 const _InRange(this._low, this._high, | |
| 194 this._lowMatchValue, this._highMatchValue); | |
| 195 | |
| 196 bool matches(value, MatchState matchState) { | |
| 197 if (value is !num) { | |
| 198 return false; | |
| 199 } | |
| 200 if (value < _low || value > _high) { | |
| 201 return false; | |
| 202 } | |
| 203 if (value == _low) { | |
| 204 return _lowMatchValue; | |
| 205 } | |
| 206 if (value == _high) { | |
| 207 return _highMatchValue; | |
| 208 } | |
| 209 return true; | |
| 210 } | |
| 211 | |
| 212 Description describe(Description description) => | |
| 213 description.add("be in range from " | |
| 214 "$_low (${_lowMatchValue ? 'inclusive' : 'exclusive'}) to " | |
| 215 "$_high (${_highMatchValue ? 'inclusive' : 'exclusive'})"); | |
| 216 | |
| 217 Description describeMismatch(item, Description mismatchDescription, | |
| 218 MatchState matchState, bool verbose) { | |
| 219 if (item is !num) { | |
| 220 return mismatchDescription. | |
| 221 addDescriptionOf(item). | |
| 222 add(' not numeric'); | |
| 223 } else { | |
| 224 return super.describeMismatch(item, mismatchDescription, | |
| 225 matchState, verbose); | |
| 226 } | |
| 227 } | |
| 228 } | |
| 229 | |
| OLD | NEW |