Path: blob/trunk/third_party/closure/goog/labs/testing/numbermatcher.js
2868 views
// Copyright 2012 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview Provides the built-in number matchers like lessThan,16* greaterThan, etc.17*/1819goog.provide('goog.labs.testing.AnyNumberMatcher');20goog.provide('goog.labs.testing.CloseToMatcher');21goog.provide('goog.labs.testing.EqualToMatcher');22goog.provide('goog.labs.testing.GreaterThanEqualToMatcher');23goog.provide('goog.labs.testing.GreaterThanMatcher');24goog.provide('goog.labs.testing.LessThanEqualToMatcher');25goog.provide('goog.labs.testing.LessThanMatcher');2627goog.require('goog.asserts');28goog.require('goog.labs.testing.Matcher');29303132/**33* Matches any number value.34*35* @constructor @struct @implements {goog.labs.testing.Matcher} @final36*/37goog.labs.testing.AnyNumberMatcher = function() {};383940/** @override */41goog.labs.testing.AnyNumberMatcher.prototype.matches = function(actualValue) {42return goog.isNumber(actualValue);43};444546/** @override */47goog.labs.testing.AnyNumberMatcher.prototype.describe = function(actualValue) {48return '<' + actualValue + '> is not a number';49};50515253/**54* The GreaterThan matcher.55*56* @param {number} value The value to compare.57*58* @constructor59* @struct60* @implements {goog.labs.testing.Matcher}61* @final62*/63goog.labs.testing.GreaterThanMatcher = function(value) {64/**65* @type {number}66* @private67*/68this.value_ = value;69};707172/**73* Determines if input value is greater than the expected value.74*75* @override76*/77goog.labs.testing.GreaterThanMatcher.prototype.matches = function(actualValue) {78goog.asserts.assertNumber(actualValue);79return actualValue > this.value_;80};818283/**84* @override85*/86goog.labs.testing.GreaterThanMatcher.prototype.describe = function(87actualValue) {88goog.asserts.assertNumber(actualValue);89return actualValue + ' is not greater than ' + this.value_;90};91929394/**95* The lessThan matcher.96*97* @param {number} value The value to compare.98*99* @constructor100* @struct101* @implements {goog.labs.testing.Matcher}102* @final103*/104goog.labs.testing.LessThanMatcher = function(value) {105/**106* @type {number}107* @private108*/109this.value_ = value;110};111112113/**114* Determines if the input value is less than the expected value.115*116* @override117*/118goog.labs.testing.LessThanMatcher.prototype.matches = function(actualValue) {119goog.asserts.assertNumber(actualValue);120return actualValue < this.value_;121};122123124/**125* @override126*/127goog.labs.testing.LessThanMatcher.prototype.describe = function(actualValue) {128goog.asserts.assertNumber(actualValue);129return actualValue + ' is not less than ' + this.value_;130};131132133134/**135* The GreaterThanEqualTo matcher.136*137* @param {number} value The value to compare.138*139* @constructor140* @struct141* @implements {goog.labs.testing.Matcher}142* @final143*/144goog.labs.testing.GreaterThanEqualToMatcher = function(value) {145/**146* @type {number}147* @private148*/149this.value_ = value;150};151152153/**154* Determines if the input value is greater than equal to the expected value.155*156* @override157*/158goog.labs.testing.GreaterThanEqualToMatcher.prototype.matches = function(159actualValue) {160goog.asserts.assertNumber(actualValue);161return actualValue >= this.value_;162};163164165/**166* @override167*/168goog.labs.testing.GreaterThanEqualToMatcher.prototype.describe = function(169actualValue) {170goog.asserts.assertNumber(actualValue);171return actualValue + ' is not greater than equal to ' + this.value_;172};173174175176/**177* The LessThanEqualTo matcher.178*179* @param {number} value The value to compare.180*181* @constructor182* @struct183* @implements {goog.labs.testing.Matcher}184* @final185*/186goog.labs.testing.LessThanEqualToMatcher = function(value) {187/**188* @type {number}189* @private190*/191this.value_ = value;192};193194195/**196* Determines if the input value is less than or equal to the expected value.197*198* @override199*/200goog.labs.testing.LessThanEqualToMatcher.prototype.matches = function(201actualValue) {202goog.asserts.assertNumber(actualValue);203return actualValue <= this.value_;204};205206207/**208* @override209*/210goog.labs.testing.LessThanEqualToMatcher.prototype.describe = function(211actualValue) {212goog.asserts.assertNumber(actualValue);213return actualValue + ' is not less than equal to ' + this.value_;214};215216217218/**219* The EqualTo matcher.220*221* @param {number} value The value to compare.222*223* @constructor224* @struct225* @implements {goog.labs.testing.Matcher}226* @final227*/228goog.labs.testing.EqualToMatcher = function(value) {229/**230* @type {number}231* @private232*/233this.value_ = value;234};235236237/**238* Determines if the input value is equal to the expected value.239*240* @override241*/242goog.labs.testing.EqualToMatcher.prototype.matches = function(actualValue) {243goog.asserts.assertNumber(actualValue);244return actualValue === this.value_;245};246247248/**249* @override250*/251goog.labs.testing.EqualToMatcher.prototype.describe = function(actualValue) {252goog.asserts.assertNumber(actualValue);253return actualValue + ' is not equal to ' + this.value_;254};255256257258/**259* The CloseTo matcher.260*261* @param {number} value The value to compare.262* @param {number} range The range to check within.263*264* @constructor265* @struct266* @implements {goog.labs.testing.Matcher}267* @final268*/269goog.labs.testing.CloseToMatcher = function(value, range) {270/**271* @type {number}272* @private273*/274this.value_ = value;275/**276* @type {number}277* @private278*/279this.range_ = range;280};281282283/**284* Determines if input value is within a certain range of the expected value.285*286* @override287*/288goog.labs.testing.CloseToMatcher.prototype.matches = function(actualValue) {289goog.asserts.assertNumber(actualValue);290return Math.abs(this.value_ - actualValue) < this.range_;291};292293294/**295* @override296*/297goog.labs.testing.CloseToMatcher.prototype.describe = function(actualValue) {298goog.asserts.assertNumber(actualValue);299return actualValue + ' is not close to(' + this.range_ + ') ' + this.value_;300};301302303/** @return {!goog.labs.testing.AnyNumberMatcher} */304function anyNumber() {305return new goog.labs.testing.AnyNumberMatcher();306}307308309/**310* @param {number} value The expected value.311*312* @return {!goog.labs.testing.GreaterThanMatcher} A GreaterThanMatcher.313*/314function greaterThan(value) {315return new goog.labs.testing.GreaterThanMatcher(value);316}317318319/**320* @param {number} value The expected value.321*322* @return {!goog.labs.testing.GreaterThanEqualToMatcher} A323* GreaterThanEqualToMatcher.324*/325function greaterThanEqualTo(value) {326return new goog.labs.testing.GreaterThanEqualToMatcher(value);327}328329330/**331* @param {number} value The expected value.332*333* @return {!goog.labs.testing.LessThanMatcher} A LessThanMatcher.334*/335function lessThan(value) {336return new goog.labs.testing.LessThanMatcher(value);337}338339340/**341* @param {number} value The expected value.342*343* @return {!goog.labs.testing.LessThanEqualToMatcher} A LessThanEqualToMatcher.344*/345function lessThanEqualTo(value) {346return new goog.labs.testing.LessThanEqualToMatcher(value);347}348349350/**351* @param {number} value The expected value.352*353* @return {!goog.labs.testing.EqualToMatcher} An EqualToMatcher.354*/355function equalTo(value) {356return new goog.labs.testing.EqualToMatcher(value);357}358359360/**361* @param {number} value The expected value.362* @param {number} range The maximum allowed difference from the expected value.363*364* @return {!goog.labs.testing.CloseToMatcher} A CloseToMatcher.365*/366function closeTo(value, range) {367return new goog.labs.testing.CloseToMatcher(value, range);368}369370371