| 1 |
// Copyright 2012 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
/** |
| 16 |
* @fileoverview Provides the built-in number matchers like lessThan, |
| 17 |
* greaterThan, etc. |
| 18 |
*/ |
| 19 |
|
| 20 |
goog.provide('goog.labs.testing.AnyNumberMatcher'); |
| 21 |
goog.provide('goog.labs.testing.CloseToMatcher'); |
| 22 |
goog.provide('goog.labs.testing.EqualToMatcher'); |
| 23 |
goog.provide('goog.labs.testing.GreaterThanEqualToMatcher'); |
| 24 |
goog.provide('goog.labs.testing.GreaterThanMatcher'); |
| 25 |
goog.provide('goog.labs.testing.LessThanEqualToMatcher'); |
| 26 |
goog.provide('goog.labs.testing.LessThanMatcher'); |
| 27 |
|
| 28 |
goog.require('goog.asserts'); |
| 29 |
goog.require('goog.labs.testing.Matcher'); |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
/** |
| 34 |
* Matches any number value. |
| 35 |
* |
| 36 |
* @constructor @struct @implements {goog.labs.testing.Matcher} @final |
| 37 |
*/ |
| 38 |
goog.labs.testing.AnyNumberMatcher = function() {}; |
| 39 |
|
| 40 |
|
| 41 |
/** @override */ |
| 42 |
goog.labs.testing.AnyNumberMatcher.prototype.matches = function(actualValue) { |
| 43 |
return goog.isNumber(actualValue); |
| 44 |
}; |
| 45 |
|
| 46 |
|
| 47 |
/** @override */ |
| 48 |
goog.labs.testing.AnyNumberMatcher.prototype.describe = function(actualValue) { |
| 49 |
return '<' + actualValue + '> is not a number'; |
| 50 |
}; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
/** |
| 55 |
* The GreaterThan matcher. |
| 56 |
* |
| 57 |
* @param {number} value The value to compare. |
| 58 |
* |
| 59 |
* @constructor |
| 60 |
* @struct |
| 61 |
* @implements {goog.labs.testing.Matcher} |
| 62 |
* @final |
| 63 |
*/ |
| 64 |
goog.labs.testing.GreaterThanMatcher = function(value) { |
| 65 |
/** |
| 66 |
* @type {number} |
| 67 |
* @private |
| 68 |
*/ |
| 69 |
this.value_ = value; |
| 70 |
}; |
| 71 |
|
| 72 |
|
| 73 |
/** |
| 74 |
* Determines if input value is greater than the expected value. |
| 75 |
* |
| 76 |
* @override |
| 77 |
*/ |
| 78 |
goog.labs.testing.GreaterThanMatcher.prototype.matches = function(actualValue) { |
| 79 |
goog.asserts.assertNumber(actualValue); |
| 80 |
return actualValue > this.value_; |
| 81 |
}; |
| 82 |
|
| 83 |
|
| 84 |
/** |
| 85 |
* @override |
| 86 |
*/ |
| 87 |
goog.labs.testing.GreaterThanMatcher.prototype.describe = function( |
| 88 |
actualValue) { |
| 89 |
goog.asserts.assertNumber(actualValue); |
| 90 |
return actualValue + ' is not greater than ' + this.value_; |
| 91 |
}; |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
/** |
| 96 |
* The lessThan matcher. |
| 97 |
* |
| 98 |
* @param {number} value The value to compare. |
| 99 |
* |
| 100 |
* @constructor |
| 101 |
* @struct |
| 102 |
* @implements {goog.labs.testing.Matcher} |
| 103 |
* @final |
| 104 |
*/ |
| 105 |
goog.labs.testing.LessThanMatcher = function(value) { |
| 106 |
/** |
| 107 |
* @type {number} |
| 108 |
* @private |
| 109 |
*/ |
| 110 |
this.value_ = value; |
| 111 |
}; |
| 112 |
|
| 113 |
|
| 114 |
/** |
| 115 |
* Determines if the input value is less than the expected value. |
| 116 |
* |
| 117 |
* @override |
| 118 |
*/ |
| 119 |
goog.labs.testing.LessThanMatcher.prototype.matches = function(actualValue) { |
| 120 |
goog.asserts.assertNumber(actualValue); |
| 121 |
return actualValue < this.value_; |
| 122 |
}; |
| 123 |
|
| 124 |
|
| 125 |
/** |
| 126 |
* @override |
| 127 |
*/ |
| 128 |
goog.labs.testing.LessThanMatcher.prototype.describe = function(actualValue) { |
| 129 |
goog.asserts.assertNumber(actualValue); |
| 130 |
return actualValue + ' is not less than ' + this.value_; |
| 131 |
}; |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
/** |
| 136 |
* The GreaterThanEqualTo matcher. |
| 137 |
* |
| 138 |
* @param {number} value The value to compare. |
| 139 |
* |
| 140 |
* @constructor |
| 141 |
* @struct |
| 142 |
* @implements {goog.labs.testing.Matcher} |
| 143 |
* @final |
| 144 |
*/ |
| 145 |
goog.labs.testing.GreaterThanEqualToMatcher = function(value) { |
| 146 |
/** |
| 147 |
* @type {number} |
| 148 |
* @private |
| 149 |
*/ |
| 150 |
this.value_ = value; |
| 151 |
}; |
| 152 |
|
| 153 |
|
| 154 |
/** |
| 155 |
* Determines if the input value is greater than equal to the expected value. |
| 156 |
* |
| 157 |
* @override |
| 158 |
*/ |
| 159 |
goog.labs.testing.GreaterThanEqualToMatcher.prototype.matches = function( |
| 160 |
actualValue) { |
| 161 |
goog.asserts.assertNumber(actualValue); |
| 162 |
return actualValue >= this.value_; |
| 163 |
}; |
| 164 |
|
| 165 |
|
| 166 |
/** |
| 167 |
* @override |
| 168 |
*/ |
| 169 |
goog.labs.testing.GreaterThanEqualToMatcher.prototype.describe = function( |
| 170 |
actualValue) { |
| 171 |
goog.asserts.assertNumber(actualValue); |
| 172 |
return actualValue + ' is not greater than equal to ' + this.value_; |
| 173 |
}; |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
/** |
| 178 |
* The LessThanEqualTo matcher. |
| 179 |
* |
| 180 |
* @param {number} value The value to compare. |
| 181 |
* |
| 182 |
* @constructor |
| 183 |
* @struct |
| 184 |
* @implements {goog.labs.testing.Matcher} |
| 185 |
* @final |
| 186 |
*/ |
| 187 |
goog.labs.testing.LessThanEqualToMatcher = function(value) { |
| 188 |
/** |
| 189 |
* @type {number} |
| 190 |
* @private |
| 191 |
*/ |
| 192 |
this.value_ = value; |
| 193 |
}; |
| 194 |
|
| 195 |
|
| 196 |
/** |
| 197 |
* Determines if the input value is less than or equal to the expected value. |
| 198 |
* |
| 199 |
* @override |
| 200 |
*/ |
| 201 |
goog.labs.testing.LessThanEqualToMatcher.prototype.matches = function( |
| 202 |
actualValue) { |
| 203 |
goog.asserts.assertNumber(actualValue); |
| 204 |
return actualValue <= this.value_; |
| 205 |
}; |
| 206 |
|
| 207 |
|
| 208 |
/** |
| 209 |
* @override |
| 210 |
*/ |
| 211 |
goog.labs.testing.LessThanEqualToMatcher.prototype.describe = function( |
| 212 |
actualValue) { |
| 213 |
goog.asserts.assertNumber(actualValue); |
| 214 |
return actualValue + ' is not less than equal to ' + this.value_; |
| 215 |
}; |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
/** |
| 220 |
* The EqualTo matcher. |
| 221 |
* |
| 222 |
* @param {number} value The value to compare. |
| 223 |
* |
| 224 |
* @constructor |
| 225 |
* @struct |
| 226 |
* @implements {goog.labs.testing.Matcher} |
| 227 |
* @final |
| 228 |
*/ |
| 229 |
goog.labs.testing.EqualToMatcher = function(value) { |
| 230 |
/** |
| 231 |
* @type {number} |
| 232 |
* @private |
| 233 |
*/ |
| 234 |
this.value_ = value; |
| 235 |
}; |
| 236 |
|
| 237 |
|
| 238 |
/** |
| 239 |
* Determines if the input value is equal to the expected value. |
| 240 |
* |
| 241 |
* @override |
| 242 |
*/ |
| 243 |
goog.labs.testing.EqualToMatcher.prototype.matches = function(actualValue) { |
| 244 |
goog.asserts.assertNumber(actualValue); |
| 245 |
return actualValue === this.value_; |
| 246 |
}; |
| 247 |
|
| 248 |
|
| 249 |
/** |
| 250 |
* @override |
| 251 |
*/ |
| 252 |
goog.labs.testing.EqualToMatcher.prototype.describe = function(actualValue) { |
| 253 |
goog.asserts.assertNumber(actualValue); |
| 254 |
return actualValue + ' is not equal to ' + this.value_; |
| 255 |
}; |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
/** |
| 260 |
* The CloseTo matcher. |
| 261 |
* |
| 262 |
* @param {number} value The value to compare. |
| 263 |
* @param {number} range The range to check within. |
| 264 |
* |
| 265 |
* @constructor |
| 266 |
* @struct |
| 267 |
* @implements {goog.labs.testing.Matcher} |
| 268 |
* @final |
| 269 |
*/ |
| 270 |
goog.labs.testing.CloseToMatcher = function(value, range) { |
| 271 |
/** |
| 272 |
* @type {number} |
| 273 |
* @private |
| 274 |
*/ |
| 275 |
this.value_ = value; |
| 276 |
/** |
| 277 |
* @type {number} |
| 278 |
* @private |
| 279 |
*/ |
| 280 |
this.range_ = range; |
| 281 |
}; |
| 282 |
|
| 283 |
|
| 284 |
/** |
| 285 |
* Determines if input value is within a certain range of the expected value. |
| 286 |
* |
| 287 |
* @override |
| 288 |
*/ |
| 289 |
goog.labs.testing.CloseToMatcher.prototype.matches = function(actualValue) { |
| 290 |
goog.asserts.assertNumber(actualValue); |
| 291 |
return Math.abs(this.value_ - actualValue) < this.range_; |
| 292 |
}; |
| 293 |
|
| 294 |
|
| 295 |
/** |
| 296 |
* @override |
| 297 |
*/ |
| 298 |
goog.labs.testing.CloseToMatcher.prototype.describe = function(actualValue) { |
| 299 |
goog.asserts.assertNumber(actualValue); |
| 300 |
return actualValue + ' is not close to(' + this.range_ + ') ' + this.value_; |
| 301 |
}; |
| 302 |
|
| 303 |
|
| 304 |
/** @return {!goog.labs.testing.AnyNumberMatcher} */ |
| 305 |
function anyNumber() { |
| 306 |
return new goog.labs.testing.AnyNumberMatcher(); |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
/** |
| 311 |
* @param {number} value The expected value. |
| 312 |
* |
| 313 |
* @return {!goog.labs.testing.GreaterThanMatcher} A GreaterThanMatcher. |
| 314 |
*/ |
| 315 |
function greaterThan(value) { |
| 316 |
return new goog.labs.testing.GreaterThanMatcher(value); |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
/** |
| 321 |
* @param {number} value The expected value. |
| 322 |
* |
| 323 |
* @return {!goog.labs.testing.GreaterThanEqualToMatcher} A |
| 324 |
* GreaterThanEqualToMatcher. |
| 325 |
*/ |
| 326 |
function greaterThanEqualTo(value) { |
| 327 |
return new goog.labs.testing.GreaterThanEqualToMatcher(value); |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
/** |
| 332 |
* @param {number} value The expected value. |
| 333 |
* |
| 334 |
* @return {!goog.labs.testing.LessThanMatcher} A LessThanMatcher. |
| 335 |
*/ |
| 336 |
function lessThan(value) { |
| 337 |
return new goog.labs.testing.LessThanMatcher(value); |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
/** |
| 342 |
* @param {number} value The expected value. |
| 343 |
* |
| 344 |
* @return {!goog.labs.testing.LessThanEqualToMatcher} A LessThanEqualToMatcher. |
| 345 |
*/ |
| 346 |
function lessThanEqualTo(value) { |
| 347 |
return new goog.labs.testing.LessThanEqualToMatcher(value); |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
/** |
| 352 |
* @param {number} value The expected value. |
| 353 |
* |
| 354 |
* @return {!goog.labs.testing.EqualToMatcher} An EqualToMatcher. |
| 355 |
*/ |
| 356 |
function equalTo(value) { |
| 357 |
return new goog.labs.testing.EqualToMatcher(value); |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
/** |
| 362 |
* @param {number} value The expected value. |
| 363 |
* @param {number} range The maximum allowed difference from the expected value. |
| 364 |
* |
| 365 |
* @return {!goog.labs.testing.CloseToMatcher} A CloseToMatcher. |
| 366 |
*/ |
| 367 |
function closeTo(value, range) { |
| 368 |
return new goog.labs.testing.CloseToMatcher(value, range); |
| 369 |
} |