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

Side by Side Diff: lib/unittest/numeric_matchers.dart

Issue 10832058: Improved the way we generate mismatch descriptions. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 4 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 | Annotate | Revision Log
« no previous file with comments | « lib/unittest/mock.dart ('k') | lib/unittest/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
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 /** 5 /**
6 * Returns a matcher which matches if the match argument is greater 6 * Returns a matcher which matches if the match argument is greater
7 * than the given [value]. 7 * than the given [value].
8 */ 8 */
9 Matcher greaterThan(value) => 9 Matcher greaterThan(value) =>
10 new _OrderingComparison(value, false, false, true, 'a value greater than'); 10 new _OrderingComparison(value, false, false, true, 'a value greater than');
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 final String _comparisonDescription; 88 final String _comparisonDescription;
89 /** Whether to include the expected value in the description */ 89 /** Whether to include the expected value in the description */
90 final bool _valueInDescription; 90 final bool _valueInDescription;
91 91
92 const _OrderingComparison( 92 const _OrderingComparison(
93 this._value, 93 this._value,
94 this._equalValue, 94 this._equalValue,
95 this._lessThanValue, 95 this._lessThanValue,
96 this._greaterThanValue, 96 this._greaterThanValue,
97 this._comparisonDescription, 97 this._comparisonDescription,
98 [valueInDescription = true]) : 98 [valueInDescription = true]) :
99 this._valueInDescription = valueInDescription; 99 this._valueInDescription = valueInDescription;
100 100
101 bool matches(item) { 101 bool matches(item, MatchState matchState) {
102 if (item == _value) { 102 if (item == _value) {
103 return _equalValue; 103 return _equalValue;
104 } else if (item < _value) { 104 } else if (item < _value) {
105 return _lessThanValue; 105 return _lessThanValue;
106 } else { 106 } else {
107 return _greaterThanValue; 107 return _greaterThanValue;
108 } 108 }
109 } 109 }
110 110
111 Description describe(Description description) { 111 Description describe(Description description) {
(...skipping 11 matching lines...) Expand all
123 * of some [value]; i.e. if the match argument is greater than 123 * of some [value]; i.e. if the match argument is greater than
124 * than or equal [value]-[delta] and less than or equal to [value]+[delta]. 124 * than or equal [value]-[delta] and less than or equal to [value]+[delta].
125 */ 125 */
126 Matcher closeTo(value, delta) => new _IsCloseTo(value, delta); 126 Matcher closeTo(value, delta) => new _IsCloseTo(value, delta);
127 127
128 class _IsCloseTo extends BaseMatcher { 128 class _IsCloseTo extends BaseMatcher {
129 final num _value, _delta; 129 final num _value, _delta;
130 130
131 const _IsCloseTo(this._value, this._delta); 131 const _IsCloseTo(this._value, this._delta);
132 132
133 bool matches(item) { 133 bool matches(item, MatchState matchState) {
134 if (!_isNumeric(item)) { 134 if (!_isNumeric(item)) {
135 return false; 135 return false;
136 } 136 }
137 var diff = item - _value; 137 var diff = item - _value;
138 if (diff < 0) diff = -diff; 138 if (diff < 0) diff = -diff;
139 return (diff <= _delta); 139 return (diff <= _delta);
140 } 140 }
141 141
142 Description describe(Description description) => 142 Description describe(Description description) =>
143 description.add('a numeric value within '). 143 description.add('a numeric value within ').
144 addDescriptionOf(_delta). 144 addDescriptionOf(_delta).
145 add(' of '). 145 add(' of ').
146 addDescriptionOf(_value); 146 addDescriptionOf(_value);
147 147
148 Description describeMismatch(item, Description mismatchDescription) { 148 Description describeMismatch(item, Description mismatchDescription,
149 MatchState matchState, bool verbose) {
149 if (item is !num) { 150 if (item is !num) {
150 return mismatchDescription. 151 return mismatchDescription.
151 addDescriptionOf(item). 152 addDescriptionOf(item).
152 add(' not numeric'); 153 add(' not numeric');
153 } else { 154 } else {
154 var diff = item - _value; 155 var diff = item - _value;
155 if (diff < 0) diff = -diff; 156 if (diff < 0) diff = -diff;
156 return mismatchDescription. 157 return mismatchDescription.
157 addDescriptionOf(item). 158 addDescriptionOf(item).
158 add(' differed by '). 159 add(' differed by ').
(...skipping 26 matching lines...) Expand all
185 */ 186 */
186 Matcher inClosedOpenRange(low, high) => new _InRange(low, high, true, false); 187 Matcher inClosedOpenRange(low, high) => new _InRange(low, high, true, false);
187 188
188 class _InRange extends BaseMatcher { 189 class _InRange extends BaseMatcher {
189 final num _low, _high; 190 final num _low, _high;
190 final bool _lowMatchValue, _highMatchValue; 191 final bool _lowMatchValue, _highMatchValue;
191 192
192 const _InRange(this._low, this._high, 193 const _InRange(this._low, this._high,
193 this._lowMatchValue, this._highMatchValue); 194 this._lowMatchValue, this._highMatchValue);
194 195
195 bool matches(value) { 196 bool matches(value, MatchState matchState) {
196 if (value is !num) { 197 if (value is !num) {
197 return false; 198 return false;
198 } 199 }
199 if (value < _low || value > _high) { 200 if (value < _low || value > _high) {
200 return false; 201 return false;
201 } 202 }
202 if (value == _low) { 203 if (value == _low) {
203 return _lowMatchValue; 204 return _lowMatchValue;
204 } 205 }
205 if (value == _high) { 206 if (value == _high) {
206 return _highMatchValue; 207 return _highMatchValue;
207 } 208 }
208 return true; 209 return true;
209 } 210 }
210 211
211 Description describe(Description description) => 212 Description describe(Description description) =>
212 description.add("be in range from " 213 description.add("be in range from "
213 "$_low (${_lowMatchValue ? 'inclusive' : 'exclusive'}) to " 214 "$_low (${_lowMatchValue ? 'inclusive' : 'exclusive'}) to "
214 "$_high (${_highMatchValue ? 'inclusive' : 'exclusive'})"); 215 "$_high (${_highMatchValue ? 'inclusive' : 'exclusive'})");
215 216
216 Description describeMismatch(item, Description mismatchDescription) { 217 Description describeMismatch(item, Description mismatchDescription,
218 MatchState matchState, bool verbose) {
217 if (item is !num) { 219 if (item is !num) {
218 return mismatchDescription. 220 return mismatchDescription.
219 addDescriptionOf(item). 221 addDescriptionOf(item).
220 add(' not numeric'); 222 add(' not numeric');
221 } else { 223 } else {
222 return super.describeMismatch(item, mismatchDescription); 224 return super.describeMismatch(item, mismatchDescription,
225 matchState, verbose);
223 } 226 }
224 } 227 }
225 } 228 }
226 229
OLDNEW
« no previous file with comments | « lib/unittest/mock.dart ('k') | lib/unittest/operator_matchers.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698