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

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

Issue 16408019: Improved error messages from unittest. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 6 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 | « pkg/unittest/lib/src/map_matchers.dart ('k') | pkg/unittest/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
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 part of matcher; 5 part of matcher;
6 6
7 /** 7 /**
8 * Returns a matcher which matches if the match argument is greater 8 * Returns a matcher which matches if the match argument is greater
9 * than the given [value]. 9 * than the given [value].
10 */ 10 */
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 93
94 const _OrderingComparison( 94 const _OrderingComparison(
95 this._value, 95 this._value,
96 this._equalValue, 96 this._equalValue,
97 this._lessThanValue, 97 this._lessThanValue,
98 this._greaterThanValue, 98 this._greaterThanValue,
99 this._comparisonDescription, 99 this._comparisonDescription,
100 [valueInDescription = true]) : 100 [valueInDescription = true]) :
101 this._valueInDescription = valueInDescription; 101 this._valueInDescription = valueInDescription;
102 102
103 bool matches(item, MatchState matchState) { 103 bool matches(item, Map matchState) {
104 if (item == _value) { 104 if (item == _value) {
105 return _equalValue; 105 return _equalValue;
106 } else if (item < _value) { 106 } else if (item < _value) {
107 return _lessThanValue; 107 return _lessThanValue;
108 } else { 108 } else {
109 return _greaterThanValue; 109 return _greaterThanValue;
110 } 110 }
111 } 111 }
112 112
113 Description describe(Description description) { 113 Description describe(Description description) {
114 if (_valueInDescription) { 114 if (_valueInDescription) {
115 return description.add(_comparisonDescription).add(' '). 115 return description.add(_comparisonDescription).add(' ').
116 addDescriptionOf(_value); 116 addDescriptionOf(_value);
117 } else { 117 } else {
118 return description.add(_comparisonDescription); 118 return description.add(_comparisonDescription);
119 } 119 }
120 } 120 }
121
122 Description describeMismatch(item, Description mismatchDescription,
123 Map matchState, bool verbose) {
124 mismatchDescription.add('is not ');
125 return describe(mismatchDescription);
126 }
121 } 127 }
122 128
123 /** 129 /**
124 * Returns a matcher which matches if the match argument is within [delta] 130 * Returns a matcher which matches if the match argument is within [delta]
125 * of some [value]; i.e. if the match argument is greater than 131 * of some [value]; i.e. if the match argument is greater than
126 * than or equal [value]-[delta] and less than or equal to [value]+[delta]. 132 * than or equal [value]-[delta] and less than or equal to [value]+[delta].
127 */ 133 */
128 Matcher closeTo(value, delta) => new _IsCloseTo(value, delta); 134 Matcher closeTo(value, delta) => new _IsCloseTo(value, delta);
129 135
130 class _IsCloseTo extends BaseMatcher { 136 class _IsCloseTo extends BaseMatcher {
131 final num _value, _delta; 137 final num _value, _delta;
132 138
133 const _IsCloseTo(this._value, this._delta); 139 const _IsCloseTo(this._value, this._delta);
134 140
135 bool matches(item, MatchState matchState) { 141 bool matches(item, Map matchState) {
136 if (!_isNumeric(item)) { 142 if (!_isNumeric(item)) {
137 return false; 143 return false;
138 } 144 }
139 var diff = item - _value; 145 var diff = item - _value;
140 if (diff < 0) diff = -diff; 146 if (diff < 0) diff = -diff;
141 return (diff <= _delta); 147 return (diff <= _delta);
142 } 148 }
143 149
144 Description describe(Description description) => 150 Description describe(Description description) =>
145 description.add('a numeric value within '). 151 description.add('a numeric value within ').
146 addDescriptionOf(_delta). 152 addDescriptionOf(_delta).
147 add(' of '). 153 add(' of ').
148 addDescriptionOf(_value); 154 addDescriptionOf(_value);
149 155
150 Description describeMismatch(item, Description mismatchDescription, 156 Description describeMismatch(item, Description mismatchDescription,
151 MatchState matchState, bool verbose) { 157 Map matchState, bool verbose) {
152 if (item is !num) { 158 if (item is !num) {
153 return mismatchDescription.add(' not numeric'); 159 return mismatchDescription.add(' not numeric');
154 } else { 160 } else {
155 var diff = item - _value; 161 var diff = item - _value;
156 if (diff < 0) diff = -diff; 162 if (diff < 0) diff = -diff;
157 return mismatchDescription. 163 return mismatchDescription.
158 add(' differed by '). 164 add(' differs by ').
159 addDescriptionOf(diff); 165 addDescriptionOf(diff);
160 } 166 }
161 } 167 }
162 } 168 }
163 169
164 /** 170 /**
165 * Returns a matcher which matches if the match argument is greater 171 * Returns a matcher which matches if the match argument is greater
166 * than or equal to [low] and less than or equal to [high]. 172 * than or equal to [low] and less than or equal to [high].
167 */ 173 */
168 Matcher inInclusiveRange(low, high) => new _InRange(low, high, true, true); 174 Matcher inInclusiveRange(low, high) => new _InRange(low, high, true, true);
(...skipping 16 matching lines...) Expand all
185 */ 191 */
186 Matcher inClosedOpenRange(low, high) => new _InRange(low, high, true, false); 192 Matcher inClosedOpenRange(low, high) => new _InRange(low, high, true, false);
187 193
188 class _InRange extends BaseMatcher { 194 class _InRange extends BaseMatcher {
189 final num _low, _high; 195 final num _low, _high;
190 final bool _lowMatchValue, _highMatchValue; 196 final bool _lowMatchValue, _highMatchValue;
191 197
192 const _InRange(this._low, this._high, 198 const _InRange(this._low, this._high,
193 this._lowMatchValue, this._highMatchValue); 199 this._lowMatchValue, this._highMatchValue);
194 200
195 bool matches(value, MatchState matchState) { 201 bool matches(value, Map matchState) {
196 if (value is !num) { 202 if (value is !num) {
197 return false; 203 return false;
198 } 204 }
199 if (value < _low || value > _high) { 205 if (value < _low || value > _high) {
200 return false; 206 return false;
201 } 207 }
202 if (value == _low) { 208 if (value == _low) {
203 return _lowMatchValue; 209 return _lowMatchValue;
204 } 210 }
205 if (value == _high) { 211 if (value == _high) {
206 return _highMatchValue; 212 return _highMatchValue;
207 } 213 }
208 return true; 214 return true;
209 } 215 }
210 216
211 Description describe(Description description) => 217 Description describe(Description description) =>
212 description.add("be in range from " 218 description.add("be in range from "
213 "$_low (${_lowMatchValue ? 'inclusive' : 'exclusive'}) to " 219 "$_low (${_lowMatchValue ? 'inclusive' : 'exclusive'}) to "
214 "$_high (${_highMatchValue ? 'inclusive' : 'exclusive'})"); 220 "$_high (${_highMatchValue ? 'inclusive' : 'exclusive'})");
215 221
216 Description describeMismatch(item, Description mismatchDescription, 222 Description describeMismatch(item, Description mismatchDescription,
217 MatchState matchState, bool verbose) { 223 Map matchState, bool verbose) {
218 if (item is !num) { 224 if (item is !num) {
219 return mismatchDescription. 225 return mismatchDescription.
220 addDescriptionOf(item). 226 addDescriptionOf(item).
221 add(' not numeric'); 227 add(' not numeric');
222 } else { 228 } else {
223 return super.describeMismatch(item, mismatchDescription, 229 return super.describeMismatch(item, mismatchDescription,
224 matchState, verbose); 230 matchState, verbose);
225 } 231 }
226 } 232 }
227 } 233 }
228 234
OLDNEW
« no previous file with comments | « pkg/unittest/lib/src/map_matchers.dart ('k') | pkg/unittest/lib/src/operator_matchers.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698