# lib/goog/labs/testing/numbermatcher.js

    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 at6//7//      http://www.apache.org/licenses/LICENSE-2.08//9// Unless required by applicable law or agreed to in writing, software10// 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 and13// limitations under the License.1415/**16 * @fileoverview Provides the built-in number matchers like lessThan,17 * greaterThan, etc.18 */192021goog.provide('goog.labs.testing.CloseToMatcher');22goog.provide('goog.labs.testing.EqualToMatcher');23goog.provide('goog.labs.testing.GreaterThanEqualToMatcher');24goog.provide('goog.labs.testing.GreaterThanMatcher');25goog.provide('goog.labs.testing.LessThanEqualToMatcher');26goog.provide('goog.labs.testing.LessThanMatcher');272829goog.require('goog.asserts');30goog.require('goog.labs.testing.Matcher');31323334/**35 * The GreaterThan matcher.36 *37 * @param {number} value The value to compare.38 *39 * @constructor40 * @struct41 * @implements {goog.labs.testing.Matcher}42 * @final43 */44goog.labs.testing.GreaterThanMatcher = function(value) {45  /**46   * @type {number}47   * @private48   */49  this.value_ = value;50};515253/**54 * Determines if input value is greater than the expected value.55 *56 * @override57 */58goog.labs.testing.GreaterThanMatcher.prototype.matches = function(actualValue) {59  goog.asserts.assertNumber(actualValue);60  return actualValue > this.value_;61};626364/**65 * @override66 */67goog.labs.testing.GreaterThanMatcher.prototype.describe =68    function(actualValue) {69  goog.asserts.assertNumber(actualValue);70  return actualValue + ' is not greater than ' + this.value_;71};72737475/**76 * The lessThan matcher.77 *78 * @param {number} value The value to compare.79 *80 * @constructor81 * @struct82 * @implements {goog.labs.testing.Matcher}83 * @final84 */85goog.labs.testing.LessThanMatcher = function(value) {86  /**87   * @type {number}88   * @private89   */90  this.value_ = value;91};929394/**95 * Determines if the input value is less than the expected value.96 *97 * @override98 */99goog.labs.testing.LessThanMatcher.prototype.matches = function(actualValue) {100  goog.asserts.assertNumber(actualValue);101  return actualValue < this.value_;102};103104105/**106 * @override107 */108goog.labs.testing.LessThanMatcher.prototype.describe =109    function(actualValue) {110  goog.asserts.assertNumber(actualValue);111  return actualValue + ' is not less than ' + this.value_;112};113114115116/**117 * The GreaterThanEqualTo matcher.118 *119 * @param {number} value The value to compare.120 *121 * @constructor122 * @struct123 * @implements {goog.labs.testing.Matcher}124 * @final125 */126goog.labs.testing.GreaterThanEqualToMatcher = function(value) {127  /**128   * @type {number}129   * @private130   */131  this.value_ = value;132};133134135/**136 * Determines if the input value is greater than equal to the expected value.137 *138 * @override139 */140goog.labs.testing.GreaterThanEqualToMatcher.prototype.matches =141    function(actualValue) {142  goog.asserts.assertNumber(actualValue);143  return actualValue >= this.value_;144};145146147/**148 * @override149 */150goog.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};155156157158/**159 * The LessThanEqualTo matcher.160 *161 * @param {number} value The value to compare.162 *163 * @constructor164 * @struct165 * @implements {goog.labs.testing.Matcher}166 * @final167 */168goog.labs.testing.LessThanEqualToMatcher = function(value) {169  /**170   * @type {number}171   * @private172   */173  this.value_ = value;174};175176177/**178 * Determines if the input value is less than or equal to the expected value.179 *180 * @override181 */182goog.labs.testing.LessThanEqualToMatcher.prototype.matches =183    function(actualValue) {184  goog.asserts.assertNumber(actualValue);185  return actualValue <= this.value_;186};187188189/**190 * @override191 */192goog.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};197198199200/**201 * The EqualTo matcher.202 *203 * @param {number} value The value to compare.204 *205 * @constructor206 * @struct207 * @implements {goog.labs.testing.Matcher}208 * @final209 */210goog.labs.testing.EqualToMatcher = function(value) {211  /**212   * @type {number}213   * @private214   */215  this.value_ = value;216};217218219/**220 * Determines if the input value is equal to the expected value.221 *222 * @override223 */224goog.labs.testing.EqualToMatcher.prototype.matches = function(actualValue) {225  goog.asserts.assertNumber(actualValue);226  return actualValue === this.value_;227};228229230/**231 * @override232 */233goog.labs.testing.EqualToMatcher.prototype.describe =234    function(actualValue) {235  goog.asserts.assertNumber(actualValue);236  return actualValue + ' is not equal to ' + this.value_;237};238239240241/**242 * The CloseTo matcher.243 *244 * @param {number} value The value to compare.245 * @param {number} range The range to check within.246 *247 * @constructor248 * @struct249 * @implements {goog.labs.testing.Matcher}250 * @final251 */252goog.labs.testing.CloseToMatcher = function(value, range) {253  /**254   * @type {number}255   * @private256   */257  this.value_ = value;258  /**259   * @type {number}260   * @private261   */262  this.range_ = range;263};264265266/**267 * Determines if input value is within a certain range of the expected value.268 *269 * @override270 */271goog.labs.testing.CloseToMatcher.prototype.matches = function(actualValue) {272  goog.asserts.assertNumber(actualValue);273  return Math.abs(this.value_ - actualValue) < this.range_;274};275276277/**278 * @override279 */280goog.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};285286287/**288 * @param {number} value The expected value.289 *290 * @return {!goog.labs.testing.GreaterThanMatcher} A GreaterThanMatcher.291 */292function greaterThan(value) {293  return new goog.labs.testing.GreaterThanMatcher(value);294}295296297/**298 * @param {number} value The expected value.299 *300 * @return {!goog.labs.testing.GreaterThanEqualToMatcher} A301 *     GreaterThanEqualToMatcher.302 */303function greaterThanEqualTo(value) {304  return new goog.labs.testing.GreaterThanEqualToMatcher(value);305}306307308/**309 * @param {number} value The expected value.310 *311 * @return {!goog.labs.testing.LessThanMatcher} A LessThanMatcher.312 */313function lessThan(value) {314  return new goog.labs.testing.LessThanMatcher(value);315}316317318/**319 * @param {number} value The expected value.320 *321 * @return {!goog.labs.testing.LessThanEqualToMatcher} A LessThanEqualToMatcher.322 */323function lessThanEqualTo(value) {324  return new goog.labs.testing.LessThanEqualToMatcher(value);325}326327328/**329 * @param {number} value The expected value.330 *331 * @return {!goog.labs.testing.EqualToMatcher} An EqualToMatcher.332 */333function equalTo(value) {334  return new goog.labs.testing.EqualToMatcher(value);335}336337338/**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 */344function closeTo(value, range) {345  return new goog.labs.testing.CloseToMatcher(value, range);346}