Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(242)

Side by Side Diff: pkg/matcher/lib/src/numeric_matchers.dart

Issue 814953002: Remove unittest and matcher from the repo. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « pkg/matcher/lib/src/map_matchers.dart ('k') | pkg/matcher/lib/src/operator_matchers.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 library matcher.numeric_matchers;
6
7 import 'interfaces.dart';
8
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) =>
17 new _OrderingComparison(value, true, false, true,
18 'a value greater than or equal to');
19
20 /// Returns a matcher which matches if the match argument is less
21 /// than the given [value].
22 Matcher lessThan(value) =>
23 new _OrderingComparison(value, false, true, false, 'a value less than');
24
25 /// Returns a matcher which matches if the match argument is less
26 /// than or equal to the given [value].
27 Matcher lessThanOrEqualTo(value) =>
28 new _OrderingComparison(value, true, true, false,
29 'a value less than or equal to');
30
31 /// A matcher which matches if the match argument is zero.
32 const Matcher isZero =
33 const _OrderingComparison(0, true, false, false, 'a value equal to');
34
35
36 /// A matcher which matches if the match argument is non-zero.
37 const Matcher isNonZero =
38 const _OrderingComparison(0, false, true, true, 'a value not equal to');
39
40 /// A matcher which matches if the match argument is positive.
41 const Matcher isPositive =
42 const _OrderingComparison(0, false, false, true, 'a positive value', false);
43
44 /// A matcher which matches if the match argument is zero or negative.
45 const Matcher isNonPositive =
46 const _OrderingComparison(0, true, true, false,
47 'a non-positive value', false);
48
49 /// A matcher which matches if the match argument is negative.
50 const Matcher isNegative =
51 const _OrderingComparison(0, false, true, false, 'a negative value', false);
52
53 /// A matcher which matches if the match argument is zero or positive.
54 const Matcher isNonNegative =
55 const _OrderingComparison(0, true, false, true,
56 'a non-negative value', false);
57
58 bool _isNumeric(value) {
59 return value is num;
60 }
61
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 {
65 /// Expected value.
66 final _value;
67 /// What to return if actual == expected
68 final bool _equalValue;
69 /// What to return if actual < expected
70 final bool _lessThanValue;
71 /// What to return if actual > expected
72 final bool _greaterThanValue;
73 /// Textual name of the inequality
74 final String _comparisonDescription;
75 /// Whether to include the expected value in the description
76 final bool _valueInDescription;
77
78 const _OrderingComparison(
79 this._value,
80 this._equalValue,
81 this._lessThanValue,
82 this._greaterThanValue,
83 this._comparisonDescription,
84 [bool valueInDescription = true]) :
85 this._valueInDescription = valueInDescription;
86
87 bool matches(item, Map matchState) {
88 if (item == _value) {
89 return _equalValue;
90 } else if (item < _value) {
91 return _lessThanValue;
92 } else {
93 return _greaterThanValue;
94 }
95 }
96
97 Description describe(Description description) {
98 if (_valueInDescription) {
99 return description.add(_comparisonDescription).add(' ').
100 addDescriptionOf(_value);
101 } else {
102 return description.add(_comparisonDescription);
103 }
104 }
105
106 Description describeMismatch(item, Description mismatchDescription,
107 Map matchState, bool verbose) {
108 mismatchDescription.add('is not ');
109 return describe(mismatchDescription);
110 }
111 }
112
113 /// Returns a matcher which matches if the match argument is within [delta]
114 /// of some [value].
115 ///
116 /// 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].
118 Matcher closeTo(num value, num delta) => new _IsCloseTo(value, delta);
119
120 class _IsCloseTo extends Matcher {
121 final num _value, _delta;
122
123 const _IsCloseTo(this._value, this._delta);
124
125 bool matches(item, Map matchState) {
126 if (!_isNumeric(item)) {
127 return false;
128 }
129 var diff = item - _value;
130 if (diff < 0) diff = -diff;
131 return (diff <= _delta);
132 }
133
134 Description describe(Description description) =>
135 description.add('a numeric value within ').
136 addDescriptionOf(_delta).
137 add(' of ').
138 addDescriptionOf(_value);
139
140 Description describeMismatch(item, Description mismatchDescription,
141 Map matchState, bool verbose) {
142 if (item is !num) {
143 return mismatchDescription.add(' not numeric');
144 } else {
145 var diff = item - _value;
146 if (diff < 0) diff = -diff;
147 return mismatchDescription.
148 add(' differs by ').
149 addDescriptionOf(diff);
150 }
151 }
152 }
153
154 /// Returns a matcher which matches if the match argument is greater
155 /// than or equal to [low] and less than or equal to [high].
156 Matcher inInclusiveRange(num low, num high) =>
157 new _InRange(low, high, true, true);
158
159 /// Returns a matcher which matches if the match argument is greater
160 /// than [low] and less than [high].
161 Matcher inExclusiveRange(num low, num high) =>
162 new _InRange(low, high, false, false);
163
164 /// Returns a matcher which matches if the match argument is greater
165 /// than [low] and less than or equal to [high].
166 Matcher inOpenClosedRange(num low, num high) =>
167 new _InRange(low, high, false, true);
168
169 /// Returns a matcher which matches if the match argument is greater
170 /// than or equal to a [low] and less than [high].
171 Matcher inClosedOpenRange(num low, num high) =>
172 new _InRange(low, high, true, false);
173
174 class _InRange extends Matcher {
175 final num _low, _high;
176 final bool _lowMatchValue, _highMatchValue;
177
178 const _InRange(this._low, this._high,
179 this._lowMatchValue, this._highMatchValue);
180
181 bool matches(value, Map matchState) {
182 if (value is! num) {
183 return false;
184 }
185 if (value < _low || value > _high) {
186 return false;
187 }
188 if (value == _low) {
189 return _lowMatchValue;
190 }
191 if (value == _high) {
192 return _highMatchValue;
193 }
194 return true;
195 }
196
197 Description describe(Description description) =>
198 description.add("be in range from "
199 "$_low (${_lowMatchValue ? 'inclusive' : 'exclusive'}) to "
200 "$_high (${_highMatchValue ? 'inclusive' : 'exclusive'})");
201
202 Description describeMismatch(item, Description mismatchDescription,
203 Map matchState, bool verbose) {
204 if (item is !num) {
205 return mismatchDescription.
206 addDescriptionOf(item).
207 add(' not numeric');
208 } else {
209 return super.describeMismatch(item, mismatchDescription,
210 matchState, verbose);
211 }
212 }
213 }
OLDNEW
« no previous file with comments | « pkg/matcher/lib/src/map_matchers.dart ('k') | pkg/matcher/lib/src/operator_matchers.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698