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