# 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 * @implements {goog.labs.testing.Matcher}41 */42goog.labs.testing.GreaterThanMatcher = function(value) {43  /**44   * @type {number}45   * @private46   */47  this.value_ = value;48};495051/**52 * Determines if input value is greater than the expected value.53 *54 * @override55 */56goog.labs.testing.GreaterThanMatcher.prototype.matches = function(actualValue) {57  goog.asserts.assertNumber(actualValue);58  return actualValue > this.value_;59};606162/**63 * @override64 */65goog.labs.testing.GreaterThanMatcher.prototype.describe =66    function(actualValue) {67  goog.asserts.assertNumber(actualValue);68  return actualValue + ' is not greater than ' + this.value_;69};70717273/**74 * The lessThan matcher.75 *76 * @param {number} value The value to compare.77 *78 * @constructor79 * @implements {goog.labs.testing.Matcher}80 */81goog.labs.testing.LessThanMatcher = function(value) {82  /**83   * @type {number}84   * @private85   */86  this.value_ = value;87};888990/**91 * Determines if the input value is less than the expected value.92 *93 * @override94 */95goog.labs.testing.LessThanMatcher.prototype.matches = function(actualValue) {96  goog.asserts.assertNumber(actualValue);97  return actualValue < this.value_;98};99100101/**102 * @override103 */104goog.labs.testing.LessThanMatcher.prototype.describe =105    function(actualValue) {106  goog.asserts.assertNumber(actualValue);107  return actualValue + ' is not less than ' + this.value_;108};109110111112/**113 * The GreaterThanEqualTo matcher.114 *115 * @param {number} value The value to compare.116 *117 * @constructor118 * @implements {goog.labs.testing.Matcher}119 */120goog.labs.testing.GreaterThanEqualToMatcher = function(value) {121  /**122   * @type {number}123   * @private124   */125  this.value_ = value;126};127128129/**130 * Determines if the input value is greater than equal to the expected value.131 *132 * @override133 */134goog.labs.testing.GreaterThanEqualToMatcher.prototype.matches =135    function(actualValue) {136  goog.asserts.assertNumber(actualValue);137  return actualValue >= this.value_;138};139140141/**142 * @override143 */144goog.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};149150151152/**153 * The LessThanEqualTo matcher.154 *155 * @param {number} value The value to compare.156 *157 * @constructor158 * @implements {goog.labs.testing.Matcher}159 */160goog.labs.testing.LessThanEqualToMatcher = function(value) {161  /**162   * @type {number}163   * @private164   */165  this.value_ = value;166};167168169/**170 * Determines if the input value is less than or equal to the expected value.171 *172 * @override173 */174goog.labs.testing.LessThanEqualToMatcher.prototype.matches =175    function(actualValue) {176  goog.asserts.assertNumber(actualValue);177  return actualValue <= this.value_;178};179180181/**182 * @override183 */184goog.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};189190191192/**193 * The EqualTo matcher.194 *195 * @param {number} value The value to compare.196 *197 * @constructor198 * @implements {goog.labs.testing.Matcher}199 */200goog.labs.testing.EqualToMatcher = function(value) {201  /**202   * @type {number}203   * @private204   */205  this.value_ = value;206};207208209/**210 * Determines if the input value is equal to the expected value.211 *212 * @override213 */214goog.labs.testing.EqualToMatcher.prototype.matches = function(actualValue) {215  goog.asserts.assertNumber(actualValue);216  return actualValue === this.value_;217};218219220/**221 * @override222 */223goog.labs.testing.EqualToMatcher.prototype.describe =224    function(actualValue) {225  goog.asserts.assertNumber(actualValue);226  return actualValue + ' is not equal to ' + this.value_;227};228229230231/**232 * The CloseTo matcher.233 *234 * @param {number} value The value to compare.235 * @param {number} range The range to check within.236 *237 * @constructor238 * @implements {goog.labs.testing.Matcher}239 */240goog.labs.testing.CloseToMatcher = function(value, range) {241  /**242   * @type {number}243   * @private244   */245  this.value_ = value;246  /**247   * @type {number}248   * @private249   */250  this.range_ = range;251};252253254/**255 * Determines if input value is within a certain range of the expected value.256 *257 * @override258 */259goog.labs.testing.CloseToMatcher.prototype.matches = function(actualValue) {260  goog.asserts.assertNumber(actualValue);261  return Math.abs(this.value_ - actualValue) < this.range_;262};263264265/**266 * @override267 */268goog.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};273274275/**276 * @param {number} value The expected value.277 *278 * @return {!goog.labs.testing.GreaterThanMatcher} A GreaterThanMatcher.279 */280function greaterThan(value) {281  return new goog.labs.testing.GreaterThanMatcher(value);282}283284285/**286 * @param {number} value The expected value.287 *288 * @return {!goog.labs.testing.GreaterThanEqualToMatcher} A289 *     GreaterThanEqualToMatcher.290 */291function greaterThanEqualTo(value) {292  return new goog.labs.testing.GreaterThanEqualToMatcher(value);293}294295296/**297 * @param {number} value The expected value.298 *299 * @return {!goog.labs.testing.LessThanMatcher} A LessThanMatcher.300 */301function lessThan(value) {302  return new goog.labs.testing.LessThanMatcher(value);303}304305306/**307 * @param {number} value The expected value.308 *309 * @return {!goog.labs.testing.LessThanEqualToMatcher} A LessThanEqualToMatcher.310 */311function lessThanEqualTo(value) {312  return new goog.labs.testing.LessThanEqualToMatcher(value);313}314315316/**317 * @param {number} value The expected value.318 *319 * @return {!goog.labs.testing.EqualToMatcher} An EqualToMatcher.320 */321function equalTo(value) {322  return new goog.labs.testing.EqualToMatcher(value);323}324325326/**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 */332function closeTo(value, range) {333  return new goog.labs.testing.CloseToMatcher(value, range);334}