123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- /*
- * Should
- * Copyright(c) 2010-2014 TJ Holowaychuk <tj@vision-media.ca>
- * MIT Licensed
- */
- module.exports = function(should, Assertion) {
- /**
- * Assert given object is NaN
- * @name NaN
- * @memberOf Assertion
- * @category assertion numbers
- * @example
- *
- * (10).should.not.be.NaN();
- * NaN.should.be.NaN();
- */
- Assertion.add('NaN', function() {
- this.params = { operator: 'to be NaN' };
- this.assert(this.obj !== this.obj);
- });
- /**
- * Assert given object is not finite (positive or negative)
- *
- * @name Infinity
- * @memberOf Assertion
- * @category assertion numbers
- * @example
- *
- * (10).should.not.be.Infinity();
- * NaN.should.not.be.Infinity();
- */
- Assertion.add('Infinity', function() {
- this.params = { operator: 'to be Infinity' };
- this.is.a.Number()
- .and.not.a.NaN()
- .and.assert(!isFinite(this.obj));
- });
- /**
- * Assert given number between `start` and `finish` or equal one of them.
- *
- * @name within
- * @memberOf Assertion
- * @category assertion numbers
- * @param {number} start Start number
- * @param {number} finish Finish number
- * @param {string} [description] Optional message
- * @example
- *
- * (10).should.be.within(0, 20);
- */
- Assertion.add('within', function(start, finish, description) {
- this.params = { operator: 'to be within ' + start + '..' + finish, message: description };
- this.assert(this.obj >= start && this.obj <= finish);
- });
- /**
- * Assert given number near some other `value` within `delta`
- *
- * @name approximately
- * @memberOf Assertion
- * @category assertion numbers
- * @param {number} value Center number
- * @param {number} delta Radius
- * @param {string} [description] Optional message
- * @example
- *
- * (9.99).should.be.approximately(10, 0.1);
- */
- Assertion.add('approximately', function(value, delta, description) {
- this.params = { operator: 'to be approximately ' + value + ' ±' + delta, message: description };
- this.assert(Math.abs(this.obj - value) <= delta);
- });
- /**
- * Assert given number above `n`.
- *
- * @name above
- * @alias Assertion#greaterThan
- * @memberOf Assertion
- * @category assertion numbers
- * @param {number} n Margin number
- * @param {string} [description] Optional message
- * @example
- *
- * (10).should.be.above(0);
- */
- Assertion.add('above', function(n, description) {
- this.params = { operator: 'to be above ' + n, message: description };
- this.assert(this.obj > n);
- });
- /**
- * Assert given number below `n`.
- *
- * @name below
- * @alias Assertion#lessThan
- * @memberOf Assertion
- * @category assertion numbers
- * @param {number} n Margin number
- * @param {string} [description] Optional message
- * @example
- *
- * (0).should.be.below(10);
- */
- Assertion.add('below', function(n, description) {
- this.params = { operator: 'to be below ' + n, message: description };
- this.assert(this.obj < n);
- });
- Assertion.alias('above', 'greaterThan');
- Assertion.alias('below', 'lessThan');
- /**
- * Assert given number above `n`.
- *
- * @name aboveOrEqual
- * @alias Assertion#greaterThanOrEqual
- * @memberOf Assertion
- * @category assertion numbers
- * @param {number} n Margin number
- * @param {string} [description] Optional message
- * @example
- *
- * (10).should.be.aboveOrEqual(0);
- * (10).should.be.aboveOrEqual(10);
- */
- Assertion.add('aboveOrEqual', function(n, description) {
- this.params = { operator: 'to be above or equal' + n, message: description };
- this.assert(this.obj >= n);
- });
- /**
- * Assert given number below `n`.
- *
- * @name belowOrEqual
- * @alias Assertion#lessThanOrEqual
- * @memberOf Assertion
- * @category assertion numbers
- * @param {number} n Margin number
- * @param {string} [description] Optional message
- * @example
- *
- * (0).should.be.belowOrEqual(10);
- * (0).should.be.belowOrEqual(0);
- */
- Assertion.add('belowOrEqual', function(n, description) {
- this.params = { operator: 'to be below or equal' + n, message: description };
- this.assert(this.obj <= n);
- });
- Assertion.alias('aboveOrEqual', 'greaterThanOrEqual');
- Assertion.alias('belowOrEqual', 'lessThanOrEqual');
- };
|