| 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 library matcher.numeric_matchers; | |
| 6 | |
| 7 import 'interfaces.dart'; | 5 import 'interfaces.dart'; |
| 8 | 6 |
| 9 /// Returns a matcher which matches if the match argument is greater | |
| 10 /// than the given [value]. | |
| 11 Matcher greaterThan(value) => | |
| 12 new _OrderingComparison(value, false, false, true, 'a value greater than'); | |
| 13 | |
| 14 /// Returns a matcher which matches if the match argument is greater | |
| 15 /// than or equal to the given [value]. | |
| 16 Matcher greaterThanOrEqualTo(value) => new _OrderingComparison( | |
| 17 value, true, false, true, 'a value greater than or equal to'); | |
| 18 | |
| 19 /// Returns a matcher which matches if the match argument is less | |
| 20 /// than the given [value]. | |
| 21 Matcher lessThan(value) => | |
| 22 new _OrderingComparison(value, false, true, false, 'a value less than'); | |
| 23 | |
| 24 /// Returns a matcher which matches if the match argument is less | |
| 25 /// than or equal to the given [value]. | |
| 26 Matcher lessThanOrEqualTo(value) => new _OrderingComparison( | |
| 27 value, true, true, false, 'a value less than or equal to'); | |
| 28 | |
| 29 /// A matcher which matches if the match argument is zero. | |
| 30 const Matcher isZero = | |
| 31 const _OrderingComparison(0, true, false, false, 'a value equal to'); | |
| 32 | |
| 33 /// A matcher which matches if the match argument is non-zero. | |
| 34 const Matcher isNonZero = | |
| 35 const _OrderingComparison(0, false, true, true, 'a value not equal to'); | |
| 36 | |
| 37 /// A matcher which matches if the match argument is positive. | |
| 38 const Matcher isPositive = | |
| 39 const _OrderingComparison(0, false, false, true, 'a positive value', false); | |
| 40 | |
| 41 /// A matcher which matches if the match argument is zero or negative. | |
| 42 const Matcher isNonPositive = const _OrderingComparison( | |
| 43 0, true, true, false, 'a non-positive value', false); | |
| 44 | |
| 45 /// A matcher which matches if the match argument is negative. | |
| 46 const Matcher isNegative = | |
| 47 const _OrderingComparison(0, false, true, false, 'a negative value', false); | |
| 48 | |
| 49 /// A matcher which matches if the match argument is zero or positive. | |
| 50 const Matcher isNonNegative = const _OrderingComparison( | |
| 51 0, true, false, true, 'a non-negative value', false); | |
| 52 | |
| 53 bool _isNumeric(value) { | |
| 54 return value is num; | |
| 55 } | |
| 56 | |
| 57 // TODO(kevmoo) Note that matchers that use _OrderingComparison only use | |
| 58 // `==` and `<` operators to evaluate the match. Or change the matcher. | |
| 59 class _OrderingComparison extends Matcher { | |
| 60 /// Expected value. | |
| 61 final _value; | |
| 62 /// What to return if actual == expected | |
| 63 final bool _equalValue; | |
| 64 /// What to return if actual < expected | |
| 65 final bool _lessThanValue; | |
| 66 /// What to return if actual > expected | |
| 67 final bool _greaterThanValue; | |
| 68 /// Textual name of the inequality | |
| 69 final String _comparisonDescription; | |
| 70 /// Whether to include the expected value in the description | |
| 71 final bool _valueInDescription; | |
| 72 | |
| 73 const _OrderingComparison(this._value, this._equalValue, this._lessThanValue, | |
| 74 this._greaterThanValue, this._comparisonDescription, | |
| 75 [bool valueInDescription = true]) | |
| 76 : this._valueInDescription = valueInDescription; | |
| 77 | |
| 78 bool matches(item, Map matchState) { | |
| 79 if (item == _value) { | |
| 80 return _equalValue; | |
| 81 } else if (item < _value) { | |
| 82 return _lessThanValue; | |
| 83 } else { | |
| 84 return _greaterThanValue; | |
| 85 } | |
| 86 } | |
| 87 | |
| 88 Description describe(Description description) { | |
| 89 if (_valueInDescription) { | |
| 90 return description | |
| 91 .add(_comparisonDescription) | |
| 92 .add(' ') | |
| 93 .addDescriptionOf(_value); | |
| 94 } else { | |
| 95 return description.add(_comparisonDescription); | |
| 96 } | |
| 97 } | |
| 98 | |
| 99 Description describeMismatch( | |
| 100 item, Description mismatchDescription, Map matchState, bool verbose) { | |
| 101 mismatchDescription.add('is not '); | |
| 102 return describe(mismatchDescription); | |
| 103 } | |
| 104 } | |
| 105 | |
| 106 /// Returns a matcher which matches if the match argument is within [delta] | 7 /// Returns a matcher which matches if the match argument is within [delta] |
| 107 /// of some [value]. | 8 /// of some [value]. |
| 108 /// | 9 /// |
| 109 /// In other words, this matches if the match argument is greater than | 10 /// In other words, this matches if the match argument is greater than |
| 110 /// than or equal [value]-[delta] and less than or equal to [value]+[delta]. | 11 /// than or equal [value]-[delta] and less than or equal to [value]+[delta]. |
| 111 Matcher closeTo(num value, num delta) => new _IsCloseTo(value, delta); | 12 Matcher closeTo(num value, num delta) => new _IsCloseTo(value, delta); |
| 112 | 13 |
| 113 class _IsCloseTo extends Matcher { | 14 class _IsCloseTo extends Matcher { |
| 114 final num _value, _delta; | 15 final num _value, _delta; |
| 115 | 16 |
| 116 const _IsCloseTo(this._value, this._delta); | 17 const _IsCloseTo(this._value, this._delta); |
| 117 | 18 |
| 118 bool matches(item, Map matchState) { | 19 bool matches(item, Map matchState) { |
| 119 if (!_isNumeric(item)) { | 20 if (item is! num) return false; |
| 120 return false; | 21 |
| 121 } | |
| 122 var diff = item - _value; | 22 var diff = item - _value; |
| 123 if (diff < 0) diff = -diff; | 23 if (diff < 0) diff = -diff; |
| 124 return (diff <= _delta); | 24 return (diff <= _delta); |
| 125 } | 25 } |
| 126 | 26 |
| 127 Description describe(Description description) => description | 27 Description describe(Description description) => description |
| 128 .add('a numeric value within ') | 28 .add('a numeric value within ') |
| 129 .addDescriptionOf(_delta) | 29 .addDescriptionOf(_delta) |
| 130 .add(' of ') | 30 .add(' of ') |
| 131 .addDescriptionOf(_value); | 31 .addDescriptionOf(_value); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 } | 78 } |
| 179 if (value == _low) { | 79 if (value == _low) { |
| 180 return _lowMatchValue; | 80 return _lowMatchValue; |
| 181 } | 81 } |
| 182 if (value == _high) { | 82 if (value == _high) { |
| 183 return _highMatchValue; | 83 return _highMatchValue; |
| 184 } | 84 } |
| 185 return true; | 85 return true; |
| 186 } | 86 } |
| 187 | 87 |
| 188 Description describe(Description description) => description.add( | 88 Description describe(Description description) => |
| 189 "be in range from " | 89 description.add("be in range from " |
| 190 "$_low (${_lowMatchValue ? 'inclusive' : 'exclusive'}) to " | 90 "$_low (${_lowMatchValue ? 'inclusive' : 'exclusive'}) to " |
| 191 "$_high (${_highMatchValue ? 'inclusive' : 'exclusive'})"); | 91 "$_high (${_highMatchValue ? 'inclusive' : 'exclusive'})"); |
| 192 | 92 |
| 193 Description describeMismatch( | 93 Description describeMismatch( |
| 194 item, Description mismatchDescription, Map matchState, bool verbose) { | 94 item, Description mismatchDescription, Map matchState, bool verbose) { |
| 195 if (item is! num) { | 95 if (item is! num) { |
| 196 return mismatchDescription.addDescriptionOf(item).add(' not numeric'); | 96 return mismatchDescription.addDescriptionOf(item).add(' not numeric'); |
| 197 } else { | 97 } else { |
| 198 return super.describeMismatch( | 98 return super |
| 199 item, mismatchDescription, matchState, verbose); | 99 .describeMismatch(item, mismatchDescription, matchState, verbose); |
| 200 } | 100 } |
| 201 } | 101 } |
| 202 } | 102 } |
| OLD | NEW |