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