| 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 | |
| 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 | |
| 29 | goog.require('goog.asserts'); |
| 30 | goog.require('goog.labs.testing.Matcher'); |
| 31 | |
| 32 | |
| 33 | |
| 34 | /** |
| 35 | * The GreaterThan matcher. |
| 36 | * |
| 37 | * @param {number} value The value to compare. |
| 38 | * |
| 39 | * @constructor |
| 40 | * @struct |
| 41 | * @implements {goog.labs.testing.Matcher} |
| 42 | * @final |
| 43 | */ |
| 44 | goog.labs.testing.GreaterThanMatcher = function(value) { |
| 45 | /** |
| 46 | * @type {number} |
| 47 | * @private |
| 48 | */ |
| 49 | this.value_ = value; |
| 50 | }; |
| 51 | |
| 52 | |
| 53 | /** |
| 54 | * Determines if input value is greater than the expected value. |
| 55 | * |
| 56 | * @override |
| 57 | */ |
| 58 | goog.labs.testing.GreaterThanMatcher.prototype.matches = function(actualValue) { |
| 59 | goog.asserts.assertNumber(actualValue); |
| 60 | return actualValue > this.value_; |
| 61 | }; |
| 62 | |
| 63 | |
| 64 | /** |
| 65 | * @override |
| 66 | */ |
| 67 | goog.labs.testing.GreaterThanMatcher.prototype.describe = |
| 68 | function(actualValue) { |
| 69 | goog.asserts.assertNumber(actualValue); |
| 70 | return actualValue + ' is not greater than ' + this.value_; |
| 71 | }; |
| 72 | |
| 73 | |
| 74 | |
| 75 | /** |
| 76 | * The lessThan matcher. |
| 77 | * |
| 78 | * @param {number} value The value to compare. |
| 79 | * |
| 80 | * @constructor |
| 81 | * @struct |
| 82 | * @implements {goog.labs.testing.Matcher} |
| 83 | * @final |
| 84 | */ |
| 85 | goog.labs.testing.LessThanMatcher = function(value) { |
| 86 | /** |
| 87 | * @type {number} |
| 88 | * @private |
| 89 | */ |
| 90 | this.value_ = value; |
| 91 | }; |
| 92 | |
| 93 | |
| 94 | /** |
| 95 | * Determines if the input value is less than the expected value. |
| 96 | * |
| 97 | * @override |
| 98 | */ |
| 99 | goog.labs.testing.LessThanMatcher.prototype.matches = function(actualValue) { |
| 100 | goog.asserts.assertNumber(actualValue); |
| 101 | return actualValue < this.value_; |
| 102 | }; |
| 103 | |
| 104 | |
| 105 | /** |
| 106 | * @override |
| 107 | */ |
| 108 | goog.labs.testing.LessThanMatcher.prototype.describe = |
| 109 | function(actualValue) { |
| 110 | goog.asserts.assertNumber(actualValue); |
| 111 | return actualValue + ' is not less than ' + this.value_; |
| 112 | }; |
| 113 | |
| 114 | |
| 115 | |
| 116 | /** |
| 117 | * The GreaterThanEqualTo matcher. |
| 118 | * |
| 119 | * @param {number} value The value to compare. |
| 120 | * |
| 121 | * @constructor |
| 122 | * @struct |
| 123 | * @implements {goog.labs.testing.Matcher} |
| 124 | * @final |
| 125 | */ |
| 126 | goog.labs.testing.GreaterThanEqualToMatcher = function(value) { |
| 127 | /** |
| 128 | * @type {number} |
| 129 | * @private |
| 130 | */ |
| 131 | this.value_ = value; |
| 132 | }; |
| 133 | |
| 134 | |
| 135 | /** |
| 136 | * Determines if the input value is greater than equal to the expected value. |
| 137 | * |
| 138 | * @override |
| 139 | */ |
| 140 | goog.labs.testing.GreaterThanEqualToMatcher.prototype.matches = |
| 141 | function(actualValue) { |
| 142 | goog.asserts.assertNumber(actualValue); |
| 143 | return actualValue >= this.value_; |
| 144 | }; |
| 145 | |
| 146 | |
| 147 | /** |
| 148 | * @override |
| 149 | */ |
| 150 | goog.labs.testing.GreaterThanEqualToMatcher.prototype.describe = |
| 151 | function(actualValue) { |
| 152 | goog.asserts.assertNumber(actualValue); |
| 153 | return actualValue + ' is not greater than equal to ' + this.value_; |
| 154 | }; |
| 155 | |
| 156 | |
| 157 | |
| 158 | /** |
| 159 | * The LessThanEqualTo matcher. |
| 160 | * |
| 161 | * @param {number} value The value to compare. |
| 162 | * |
| 163 | * @constructor |
| 164 | * @struct |
| 165 | * @implements {goog.labs.testing.Matcher} |
| 166 | * @final |
| 167 | */ |
| 168 | goog.labs.testing.LessThanEqualToMatcher = function(value) { |
| 169 | /** |
| 170 | * @type {number} |
| 171 | * @private |
| 172 | */ |
| 173 | this.value_ = value; |
| 174 | }; |
| 175 | |
| 176 | |
| 177 | /** |
| 178 | * Determines if the input value is less than or equal to the expected value. |
| 179 | * |
| 180 | * @override |
| 181 | */ |
| 182 | goog.labs.testing.LessThanEqualToMatcher.prototype.matches = |
| 183 | function(actualValue) { |
| 184 | goog.asserts.assertNumber(actualValue); |
| 185 | return actualValue <= this.value_; |
| 186 | }; |
| 187 | |
| 188 | |
| 189 | /** |
| 190 | * @override |
| 191 | */ |
| 192 | goog.labs.testing.LessThanEqualToMatcher.prototype.describe = |
| 193 | function(actualValue) { |
| 194 | goog.asserts.assertNumber(actualValue); |
| 195 | return actualValue + ' is not less than equal to ' + this.value_; |
| 196 | }; |
| 197 | |
| 198 | |
| 199 | |
| 200 | /** |
| 201 | * The EqualTo matcher. |
| 202 | * |
| 203 | * @param {number} value The value to compare. |
| 204 | * |
| 205 | * @constructor |
| 206 | * @struct |
| 207 | * @implements {goog.labs.testing.Matcher} |
| 208 | * @final |
| 209 | */ |
| 210 | goog.labs.testing.EqualToMatcher = function(value) { |
| 211 | /** |
| 212 | * @type {number} |
| 213 | * @private |
| 214 | */ |
| 215 | this.value_ = value; |
| 216 | }; |
| 217 | |
| 218 | |
| 219 | /** |
| 220 | * Determines if the input value is equal to the expected value. |
| 221 | * |
| 222 | * @override |
| 223 | */ |
| 224 | goog.labs.testing.EqualToMatcher.prototype.matches = function(actualValue) { |
| 225 | goog.asserts.assertNumber(actualValue); |
| 226 | return actualValue === this.value_; |
| 227 | }; |
| 228 | |
| 229 | |
| 230 | /** |
| 231 | * @override |
| 232 | */ |
| 233 | goog.labs.testing.EqualToMatcher.prototype.describe = |
| 234 | function(actualValue) { |
| 235 | goog.asserts.assertNumber(actualValue); |
| 236 | return actualValue + ' is not equal to ' + this.value_; |
| 237 | }; |
| 238 | |
| 239 | |
| 240 | |
| 241 | /** |
| 242 | * The CloseTo matcher. |
| 243 | * |
| 244 | * @param {number} value The value to compare. |
| 245 | * @param {number} range The range to check within. |
| 246 | * |
| 247 | * @constructor |
| 248 | * @struct |
| 249 | * @implements {goog.labs.testing.Matcher} |
| 250 | * @final |
| 251 | */ |
| 252 | goog.labs.testing.CloseToMatcher = function(value, range) { |
| 253 | /** |
| 254 | * @type {number} |
| 255 | * @private |
| 256 | */ |
| 257 | this.value_ = value; |
| 258 | /** |
| 259 | * @type {number} |
| 260 | * @private |
| 261 | */ |
| 262 | this.range_ = range; |
| 263 | }; |
| 264 | |
| 265 | |
| 266 | /** |
| 267 | * Determines if input value is within a certain range of the expected value. |
| 268 | * |
| 269 | * @override |
| 270 | */ |
| 271 | goog.labs.testing.CloseToMatcher.prototype.matches = function(actualValue) { |
| 272 | goog.asserts.assertNumber(actualValue); |
| 273 | return Math.abs(this.value_ - actualValue) < this.range_; |
| 274 | }; |
| 275 | |
| 276 | |
| 277 | /** |
| 278 | * @override |
| 279 | */ |
| 280 | goog.labs.testing.CloseToMatcher.prototype.describe = |
| 281 | function(actualValue) { |
| 282 | goog.asserts.assertNumber(actualValue); |
| 283 | return actualValue + ' is not close to(' + this.range_ + ') ' + this.value_; |
| 284 | }; |
| 285 | |
| 286 | |
| 287 | /** |
| 288 | * @param {number} value The expected value. |
| 289 | * |
| 290 | * @return {!goog.labs.testing.GreaterThanMatcher} A GreaterThanMatcher. |
| 291 | */ |
| 292 | function greaterThan(value) { |
| 293 | return new goog.labs.testing.GreaterThanMatcher(value); |
| 294 | } |
| 295 | |
| 296 | |
| 297 | /** |
| 298 | * @param {number} value The expected value. |
| 299 | * |
| 300 | * @return {!goog.labs.testing.GreaterThanEqualToMatcher} A |
| 301 | * GreaterThanEqualToMatcher. |
| 302 | */ |
| 303 | function greaterThanEqualTo(value) { |
| 304 | return new goog.labs.testing.GreaterThanEqualToMatcher(value); |
| 305 | } |
| 306 | |
| 307 | |
| 308 | /** |
| 309 | * @param {number} value The expected value. |
| 310 | * |
| 311 | * @return {!goog.labs.testing.LessThanMatcher} A LessThanMatcher. |
| 312 | */ |
| 313 | function lessThan(value) { |
| 314 | return new goog.labs.testing.LessThanMatcher(value); |
| 315 | } |
| 316 | |
| 317 | |
| 318 | /** |
| 319 | * @param {number} value The expected value. |
| 320 | * |
| 321 | * @return {!goog.labs.testing.LessThanEqualToMatcher} A LessThanEqualToMatcher. |
| 322 | */ |
| 323 | function lessThanEqualTo(value) { |
| 324 | return new goog.labs.testing.LessThanEqualToMatcher(value); |
| 325 | } |
| 326 | |
| 327 | |
| 328 | /** |
| 329 | * @param {number} value The expected value. |
| 330 | * |
| 331 | * @return {!goog.labs.testing.EqualToMatcher} An EqualToMatcher. |
| 332 | */ |
| 333 | function equalTo(value) { |
| 334 | return new goog.labs.testing.EqualToMatcher(value); |
| 335 | } |
| 336 | |
| 337 | |
| 338 | /** |
| 339 | * @param {number} value The expected value. |
| 340 | * @param {number} range The maximum allowed difference from the expected value. |
| 341 | * |
| 342 | * @return {!goog.labs.testing.CloseToMatcher} A CloseToMatcher. |
| 343 | */ |
| 344 | function closeTo(value, range) { |
| 345 | return new goog.labs.testing.CloseToMatcher(value, range); |
| 346 | } |