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