/* * should.js - assertion library * Copyright(c) 2010-2013 TJ Holowaychuk * Copyright(c) 2013-2016 Denis Bardadym * 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'); };