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