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

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

Issue 1135653004: pkg/(unit)test: include a copy of old matcher (Closed) Base URL: https://github.com/dart-lang/test.git@stable
Patch Set: another nit Created 5 years, 7 months 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
« no previous file with comments | « lib/src/matcher/map_matchers.dart ('k') | lib/src/matcher/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 unittest.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) => 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]
107 /// of some [value].
108 ///
109 /// 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].
111 Matcher closeTo(num value, num delta) => new _IsCloseTo(value, delta);
112
113 class _IsCloseTo extends Matcher {
114 final num _value, _delta;
115
116 const _IsCloseTo(this._value, this._delta);
117
118 bool matches(item, Map matchState) {
119 if (!_isNumeric(item)) {
120 return false;
121 }
122 var diff = item - _value;
123 if (diff < 0) diff = -diff;
124 return (diff <= _delta);
125 }
126
127 Description describe(Description description) => description
128 .add('a numeric value within ')
129 .addDescriptionOf(_delta)
130 .add(' of ')
131 .addDescriptionOf(_value);
132
133 Description describeMismatch(
134 item, Description mismatchDescription, Map matchState, bool verbose) {
135 if (item is! num) {
136 return mismatchDescription.add(' not numeric');
137 } else {
138 var diff = item - _value;
139 if (diff < 0) diff = -diff;
140 return mismatchDescription.add(' differs by ').addDescriptionOf(diff);
141 }
142 }
143 }
144
145 /// Returns a matcher which matches if the match argument is greater
146 /// than or equal to [low] and less than or equal to [high].
147 Matcher inInclusiveRange(num low, num high) =>
148 new _InRange(low, high, true, true);
149
150 /// Returns a matcher which matches if the match argument is greater
151 /// than [low] and less than [high].
152 Matcher inExclusiveRange(num low, num high) =>
153 new _InRange(low, high, false, false);
154
155 /// Returns a matcher which matches if the match argument is greater
156 /// than [low] and less than or equal to [high].
157 Matcher inOpenClosedRange(num low, num high) =>
158 new _InRange(low, high, false, true);
159
160 /// Returns a matcher which matches if the match argument is greater
161 /// than or equal to a [low] and less than [high].
162 Matcher inClosedOpenRange(num low, num high) =>
163 new _InRange(low, high, true, false);
164
165 class _InRange extends Matcher {
166 final num _low, _high;
167 final bool _lowMatchValue, _highMatchValue;
168
169 const _InRange(
170 this._low, this._high, this._lowMatchValue, this._highMatchValue);
171
172 bool matches(value, Map matchState) {
173 if (value is! num) {
174 return false;
175 }
176 if (value < _low || value > _high) {
177 return false;
178 }
179 if (value == _low) {
180 return _lowMatchValue;
181 }
182 if (value == _high) {
183 return _highMatchValue;
184 }
185 return true;
186 }
187
188 Description describe(Description description) => description.add(
189 "be in range from "
190 "$_low (${_lowMatchValue ? 'inclusive' : 'exclusive'}) to "
191 "$_high (${_highMatchValue ? 'inclusive' : 'exclusive'})");
192
193 Description describeMismatch(
194 item, Description mismatchDescription, Map matchState, bool verbose) {
195 if (item is! num) {
196 return mismatchDescription.addDescriptionOf(item).add(' not numeric');
197 } else {
198 return super.describeMismatch(
199 item, mismatchDescription, matchState, verbose);
200 }
201 }
202 }
OLDNEW
« no previous file with comments | « lib/src/matcher/map_matchers.dart ('k') | lib/src/matcher/operator_matchers.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698