123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369 |
- // Copyright 2012 The Closure Library Authors. All Rights Reserved.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS-IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- /**
- * @fileoverview Provides the built-in number matchers like lessThan,
- * greaterThan, etc.
- */
- goog.provide('goog.labs.testing.AnyNumberMatcher');
- goog.provide('goog.labs.testing.CloseToMatcher');
- goog.provide('goog.labs.testing.EqualToMatcher');
- goog.provide('goog.labs.testing.GreaterThanEqualToMatcher');
- goog.provide('goog.labs.testing.GreaterThanMatcher');
- goog.provide('goog.labs.testing.LessThanEqualToMatcher');
- goog.provide('goog.labs.testing.LessThanMatcher');
- goog.require('goog.asserts');
- goog.require('goog.labs.testing.Matcher');
- /**
- * Matches any number value.
- *
- * @constructor @struct @implements {goog.labs.testing.Matcher} @final
- */
- goog.labs.testing.AnyNumberMatcher = function() {};
- /** @override */
- goog.labs.testing.AnyNumberMatcher.prototype.matches = function(actualValue) {
- return goog.isNumber(actualValue);
- };
- /** @override */
- goog.labs.testing.AnyNumberMatcher.prototype.describe = function(actualValue) {
- return '<' + actualValue + '> is not a number';
- };
- /**
- * The GreaterThan matcher.
- *
- * @param {number} value The value to compare.
- *
- * @constructor
- * @struct
- * @implements {goog.labs.testing.Matcher}
- * @final
- */
- goog.labs.testing.GreaterThanMatcher = function(value) {
- /**
- * @type {number}
- * @private
- */
- this.value_ = value;
- };
- /**
- * Determines if input value is greater than the expected value.
- *
- * @override
- */
- goog.labs.testing.GreaterThanMatcher.prototype.matches = function(actualValue) {
- goog.asserts.assertNumber(actualValue);
- return actualValue > this.value_;
- };
- /**
- * @override
- */
- goog.labs.testing.GreaterThanMatcher.prototype.describe = function(
- actualValue) {
- goog.asserts.assertNumber(actualValue);
- return actualValue + ' is not greater than ' + this.value_;
- };
- /**
- * The lessThan matcher.
- *
- * @param {number} value The value to compare.
- *
- * @constructor
- * @struct
- * @implements {goog.labs.testing.Matcher}
- * @final
- */
- goog.labs.testing.LessThanMatcher = function(value) {
- /**
- * @type {number}
- * @private
- */
- this.value_ = value;
- };
- /**
- * Determines if the input value is less than the expected value.
- *
- * @override
- */
- goog.labs.testing.LessThanMatcher.prototype.matches = function(actualValue) {
- goog.asserts.assertNumber(actualValue);
- return actualValue < this.value_;
- };
- /**
- * @override
- */
- goog.labs.testing.LessThanMatcher.prototype.describe = function(actualValue) {
- goog.asserts.assertNumber(actualValue);
- return actualValue + ' is not less than ' + this.value_;
- };
- /**
- * The GreaterThanEqualTo matcher.
- *
- * @param {number} value The value to compare.
- *
- * @constructor
- * @struct
- * @implements {goog.labs.testing.Matcher}
- * @final
- */
- goog.labs.testing.GreaterThanEqualToMatcher = function(value) {
- /**
- * @type {number}
- * @private
- */
- this.value_ = value;
- };
- /**
- * Determines if the input value is greater than equal to the expected value.
- *
- * @override
- */
- goog.labs.testing.GreaterThanEqualToMatcher.prototype.matches = function(
- actualValue) {
- goog.asserts.assertNumber(actualValue);
- return actualValue >= this.value_;
- };
- /**
- * @override
- */
- goog.labs.testing.GreaterThanEqualToMatcher.prototype.describe = function(
- actualValue) {
- goog.asserts.assertNumber(actualValue);
- return actualValue + ' is not greater than equal to ' + this.value_;
- };
- /**
- * The LessThanEqualTo matcher.
- *
- * @param {number} value The value to compare.
- *
- * @constructor
- * @struct
- * @implements {goog.labs.testing.Matcher}
- * @final
- */
- goog.labs.testing.LessThanEqualToMatcher = function(value) {
- /**
- * @type {number}
- * @private
- */
- this.value_ = value;
- };
- /**
- * Determines if the input value is less than or equal to the expected value.
- *
- * @override
- */
- goog.labs.testing.LessThanEqualToMatcher.prototype.matches = function(
- actualValue) {
- goog.asserts.assertNumber(actualValue);
- return actualValue <= this.value_;
- };
- /**
- * @override
- */
- goog.labs.testing.LessThanEqualToMatcher.prototype.describe = function(
- actualValue) {
- goog.asserts.assertNumber(actualValue);
- return actualValue + ' is not less than equal to ' + this.value_;
- };
- /**
- * The EqualTo matcher.
- *
- * @param {number} value The value to compare.
- *
- * @constructor
- * @struct
- * @implements {goog.labs.testing.Matcher}
- * @final
- */
- goog.labs.testing.EqualToMatcher = function(value) {
- /**
- * @type {number}
- * @private
- */
- this.value_ = value;
- };
- /**
- * Determines if the input value is equal to the expected value.
- *
- * @override
- */
- goog.labs.testing.EqualToMatcher.prototype.matches = function(actualValue) {
- goog.asserts.assertNumber(actualValue);
- return actualValue === this.value_;
- };
- /**
- * @override
- */
- goog.labs.testing.EqualToMatcher.prototype.describe = function(actualValue) {
- goog.asserts.assertNumber(actualValue);
- return actualValue + ' is not equal to ' + this.value_;
- };
- /**
- * The CloseTo matcher.
- *
- * @param {number} value The value to compare.
- * @param {number} range The range to check within.
- *
- * @constructor
- * @struct
- * @implements {goog.labs.testing.Matcher}
- * @final
- */
- goog.labs.testing.CloseToMatcher = function(value, range) {
- /**
- * @type {number}
- * @private
- */
- this.value_ = value;
- /**
- * @type {number}
- * @private
- */
- this.range_ = range;
- };
- /**
- * Determines if input value is within a certain range of the expected value.
- *
- * @override
- */
- goog.labs.testing.CloseToMatcher.prototype.matches = function(actualValue) {
- goog.asserts.assertNumber(actualValue);
- return Math.abs(this.value_ - actualValue) < this.range_;
- };
- /**
- * @override
- */
- goog.labs.testing.CloseToMatcher.prototype.describe = function(actualValue) {
- goog.asserts.assertNumber(actualValue);
- return actualValue + ' is not close to(' + this.range_ + ') ' + this.value_;
- };
- /** @return {!goog.labs.testing.AnyNumberMatcher} */
- function anyNumber() {
- return new goog.labs.testing.AnyNumberMatcher();
- }
- /**
- * @param {number} value The expected value.
- *
- * @return {!goog.labs.testing.GreaterThanMatcher} A GreaterThanMatcher.
- */
- function greaterThan(value) {
- return new goog.labs.testing.GreaterThanMatcher(value);
- }
- /**
- * @param {number} value The expected value.
- *
- * @return {!goog.labs.testing.GreaterThanEqualToMatcher} A
- * GreaterThanEqualToMatcher.
- */
- function greaterThanEqualTo(value) {
- return new goog.labs.testing.GreaterThanEqualToMatcher(value);
- }
- /**
- * @param {number} value The expected value.
- *
- * @return {!goog.labs.testing.LessThanMatcher} A LessThanMatcher.
- */
- function lessThan(value) {
- return new goog.labs.testing.LessThanMatcher(value);
- }
- /**
- * @param {number} value The expected value.
- *
- * @return {!goog.labs.testing.LessThanEqualToMatcher} A LessThanEqualToMatcher.
- */
- function lessThanEqualTo(value) {
- return new goog.labs.testing.LessThanEqualToMatcher(value);
- }
- /**
- * @param {number} value The expected value.
- *
- * @return {!goog.labs.testing.EqualToMatcher} An EqualToMatcher.
- */
- function equalTo(value) {
- return new goog.labs.testing.EqualToMatcher(value);
- }
- /**
- * @param {number} value The expected value.
- * @param {number} range The maximum allowed difference from the expected value.
- *
- * @return {!goog.labs.testing.CloseToMatcher} A CloseToMatcher.
- */
- function closeTo(value, range) {
- return new goog.labs.testing.CloseToMatcher(value, range);
- }
|