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