Chromium Code Reviews| 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 /** | |
| 6 * Returns a matcher which matches if the match argument is greater | |
| 7 * than the given [value]. | |
| 8 */ | |
| 9 IMatcher greaterThan(value) => | |
| 10 new _OrderingComparison(value, false, false, true, 'greater than'); | |
| 11 | |
| 12 /** | |
| 13 * Returns a matcher which matches if the match argument is greater | |
| 14 * than or equal to the given [value]. | |
| 15 */ | |
| 16 IMatcher greaterThanOrEqualTo(value) => | |
| 17 new _OrderingComparison(value, true, false, true, 'greater than or equal to'); | |
| 18 | |
| 19 /** | |
| 20 * Returns a matcher which matches if the match argument is less | |
| 21 * than the given [value]. | |
| 22 */ | |
| 23 IMatcher lessThan(value) => | |
| 24 new _OrderingComparison(value, false, true, false, 'less than'); | |
| 25 | |
| 26 /** | |
| 27 * Returns a matcher which matches if the match argument is less | |
| 28 * than or equal to the given [value]. | |
| 29 */ | |
| 30 IMatcher lessThanOrEqualTo(value) => | |
|
Bob Nystrom
2012/05/30 23:23:51
Is this really better than doing:
expect(foo > ba
gram
2012/06/01 17:33:15
It gives better error messages. Either can be used
| |
| 31 new _OrderingComparison(value, true, true, false, 'less than or equal to'); | |
| 32 | |
| 33 /** | |
| 34 * Returns a matcher which matches if the match argument is zero. | |
| 35 */ | |
| 36 IMatcher isZero() { | |
|
Bob Nystrom
2012/05/30 23:23:51
Make these constants.
gram
2012/06/01 17:33:15
Done.
| |
| 37 return new _OrderingComparison(0, true, false, false, 'equal to'); | |
| 38 } | |
| 39 | |
| 40 /** | |
| 41 * Returns a matcher which matches if the match argument is non-zero. | |
| 42 */ | |
| 43 IMatcher isNonZero() { | |
| 44 return new _OrderingComparison(0, false, true, true, 'not equal to'); | |
| 45 } | |
| 46 | |
| 47 /** | |
| 48 * Returns a matcher which matches if the match argument is positive | |
| 49 * (0 inclusive). | |
| 50 */ | |
| 51 IMatcher isPositive() { | |
| 52 return new _OrderingComparison(0, true, false, true, 'positive', false); | |
| 53 } | |
| 54 | |
| 55 /** | |
| 56 * Returns a matcher which matches if the match argument is negative | |
| 57 * (0 exclusive). | |
| 58 */ | |
| 59 IMatcher isNegative() { | |
| 60 return new _OrderingComparison(0, false, true, false, 'negative', false); | |
| 61 } | |
| 62 | |
| 63 bool _isNumeric(value) { | |
| 64 return value is int || value is double; | |
|
Bob Nystrom
2012/05/30 23:23:51
"is num"
But I would just get rid of this. You ca
gram
2012/06/01 17:33:15
This isn't a matcher - it's a utility function use
Bob Nystrom
2012/06/01 18:22:22
Oh, right. Sorry. In either case, you can still si
| |
| 65 } | |
| 66 | |
| 67 class _OrderingComparison extends _NumericMatcher { | |
| 68 | |
| 69 var _value; // Expected value. | |
|
Bob Nystrom
2012/05/30 23:23:51
Make these doc comments. Make the fields final.
gram
2012/06/01 17:33:15
Done.
| |
| 70 bool _equalValue; // Return this if values are equal. | |
| 71 bool _lessThanValue; // Return this if test value < expected. | |
| 72 bool _greaterThanValue; // Return this if test value > expected. | |
| 73 String _comparisonDescription; // Textual name of inequality. | |
| 74 bool _valueInDescription; // include value in description? | |
| 75 | |
| 76 _OrderingComparison( | |
| 77 this._value, | |
| 78 bool this._equalValue, | |
|
Bob Nystrom
2012/05/30 23:23:51
Remove these type annotations.
gram
2012/06/01 17:33:15
Done.
| |
| 79 bool this._lessThanValue, | |
| 80 bool this._greaterThanValue, | |
| 81 String this._comparisonDescription, | |
| 82 [bool this._valueInDescription = true]) { | |
| 83 if (!_isNumeric(_value)) { | |
| 84 throw new IllegalArgumentException( | |
| 85 'Expected value for ${_comparisonDescription} must be numeric'); | |
| 86 } | |
| 87 } | |
| 88 | |
| 89 bool matches(item) { | |
| 90 if (!_isNumeric(item)) { | |
|
Bob Nystrom
2012/05/30 23:23:51
Should we allow this on user-defined types that im
gram
2012/06/01 17:33:15
Done.
| |
| 91 return false; | |
| 92 } | |
| 93 if (item == _value) { | |
| 94 return _equalValue; | |
| 95 } else if (item < _value) { | |
| 96 return _lessThanValue; | |
| 97 } else { | |
| 98 return _greaterThanValue; | |
| 99 } | |
| 100 } | |
| 101 | |
| 102 IDescription describe(IDescription description) => | |
|
Bob Nystrom
2012/05/30 23:23:51
I would use a {} body here.
gram
2012/06/01 17:33:15
Done.
| |
| 103 description.append('a value '). | |
| 104 append(_comparisonDescription). | |
| 105 append(' '). | |
| 106 appendDescriptionOf(_value); | |
| 107 } | |
| 108 | |
| 109 /** | |
| 110 * Returns a matcher which matches if the match argument is within [delta] | |
| 111 * of some [value]; i.e. if the match argument is greater than | |
| 112 * than or equal [value]-[delta] and less than or equal to [value]+[delta]. | |
| 113 */ | |
| 114 IMatcher closeTo(value, delta) => new _IsCloseTo(value, delta); | |
| 115 | |
| 116 class _IsCloseTo extends _NumericMatcher { | |
| 117 | |
| 118 var _value, _delta; | |
| 119 | |
| 120 _IsCloseTo(this._value, this._delta) { | |
| 121 if (!_isNumeric(_value)) { | |
| 122 throw new IllegalArgumentException('IsCloseTo value must be numeric'); | |
| 123 } | |
| 124 if (!_isNumeric(_delta)) { | |
| 125 throw new IllegalArgumentException('IsCloseTo delta must be numeric'); | |
| 126 } | |
| 127 } | |
| 128 | |
| 129 bool matches(item) { | |
| 130 if (!_isNumeric(item)) { | |
| 131 return false; | |
| 132 } | |
| 133 var diff = item - _value; | |
| 134 if (diff < 0) diff = -diff; | |
|
Bob Nystrom
2012/05/30 23:23:51
var diff = Math.abs(item - _value);
gram
2012/06/01 17:33:15
There is no Math.abs
Bob Nystrom
2012/06/01 18:22:22
Heh, whoops! Every now and then, the corelib desig
| |
| 135 return (diff <= _delta); | |
| 136 } | |
| 137 | |
| 138 IDescription describeMismatch(item, IDescription mismatchDescription) { | |
| 139 if (_isNumeric(item)) { | |
| 140 var diff = item - _value; | |
| 141 if (diff < 0) diff = -diff; | |
| 142 return mismatchDescription. | |
| 143 appendDescriptionOf(item). | |
| 144 append(' differed by '). | |
| 145 appendDescriptionOf(diff); | |
| 146 } else { | |
| 147 return super.describeMismatch(item, mismatchDescription); | |
| 148 } | |
| 149 } | |
| 150 | |
| 151 IDescription describe(IDescription description) => | |
| 152 description.append('a numeric value within '). | |
| 153 appendDescriptionOf(_delta). | |
| 154 append(' of '). | |
| 155 appendDescriptionOf(_value); | |
| 156 } | |
| 157 | |
| 158 /** | |
| 159 * Returns a matcher which matches if the match argument is greater | |
| 160 * than or equal to a [low] and less than or equal to [high]. | |
|
Bob Nystrom
2012/05/30 23:23:51
"to a" -> "to"
gram
2012/06/01 17:33:15
Done.
| |
| 161 */ | |
| 162 IMatcher inInclusiveRange(low, high) => new _InRange(low, high, false, false); | |
| 163 | |
| 164 /** | |
| 165 * Returns a matcher which matches if the match argument is greater | |
| 166 * than [low] and less than [high]. | |
| 167 */ | |
| 168 IMatcher inExclusiveRange(low, high) => new _InRange(low, high, true, true); | |
|
Bob Nystrom
2012/05/30 23:23:51
Add a blank line after each member.
gram
2012/06/01 17:33:15
Done.
| |
| 169 /** | |
| 170 * Returns a matcher which matches if the match argument is greater | |
| 171 * than [low] and less than or equal to [high]. | |
| 172 */ | |
| 173 IMatcher inOpenClosedRange(low, high) => new _InRange(low, high, true, false); | |
| 174 /** | |
| 175 * Returns a matcher which matches if the match argument is greater | |
| 176 * than or equal to a [low] and less than [high]. | |
| 177 */ | |
| 178 IMatcher inClosedOpenRange(low, high) => new _InRange(low, high, false, true); | |
| 179 | |
| 180 | |
| 181 class _InRange extends _NumericMatcher { | |
| 182 num _low, _high; | |
| 183 bool _lowExclusive, _highExclusive; | |
| 184 | |
| 185 _InRange(num this._low, num this._high, | |
| 186 bool this._lowExclusive, bool this._highExclusive) { | |
| 187 if (!_isNumeric(_low) || !_isNumeric(_high)) { | |
|
Bob Nystrom
2012/05/30 23:23:51
Delete these checks. That's what checked mode is f
gram
2012/06/01 17:33:15
Done.
| |
| 188 throw new | |
| 189 IllegalArgumentException('IsRange range arguments must be numeric'); | |
| 190 } | |
| 191 if (!(_lowExclusive is bool) || !(_highExclusive is bool)) { | |
| 192 throw new IllegalArgumentException('IsRange flag arguments must be bool'); | |
| 193 } | |
| 194 } | |
| 195 | |
| 196 bool matches(value) { | |
| 197 if (!_isNumeric(value)) { | |
| 198 return false; | |
| 199 } | |
| 200 if (value < _low || value > _high) { | |
| 201 return false; | |
| 202 } | |
| 203 if (_lowExclusive && value == _low) { | |
| 204 return false; | |
| 205 } | |
| 206 if (_highExclusive && value == _high) { | |
| 207 return false; | |
| 208 } | |
| 209 return true; | |
| 210 } | |
| 211 | |
| 212 IDescription describe(IDescription description) => | |
| 213 description.append("be in range from " | |
| 214 "$_low (${_lowExclusive?'exclusive':'inclusive'}) to " | |
|
Bob Nystrom
2012/05/30 23:23:51
Spaces around ? and :
gram
2012/06/01 17:33:15
Done.
| |
| 215 "$_high (${_highExclusive?'exclusive':'inclusive'})"); | |
| 216 | |
| 217 IDescription describeMismatch(item, IDescription mismatchDescription) { | |
| 218 if (!_isNumeric(item)) { | |
| 219 return mismatchDescription. | |
| 220 appendDescriptionOf(item). | |
| 221 append(' not numeric'); | |
| 222 } else { | |
| 223 return super.describeMismatch(item, mismatchDescription); | |
| 224 } | |
| 225 } | |
| 226 } | |
| 227 | |
| 228 | |
| 229 // Numeric matchers match against a number. We add this intermediate | |
| 230 // class to give better mismatch error messages than the base Matcher class. | |
| 231 class _NumericMatcher extends Matcher { | |
| 232 IDescription describeMismatch(item, IDescription mismatchDescription) { | |
| 233 if (!_isNumeric(item)) { | |
| 234 return mismatchDescription. | |
| 235 appendDescriptionOf(item). | |
| 236 append(' not numeric'); | |
| 237 } else { | |
| 238 return super.describeMismatch(item, mismatchDescription); | |
| 239 } | |
| 240 } | |
| 241 } | |
| OLD | NEW |