Skip to content

Commit

Permalink
Merge pull request #779 from TheSavior/is-finite
Browse files Browse the repository at this point in the history
Adding `.finite`. Fixes #271
  • Loading branch information
keithamus committed Sep 2, 2016
2 parents a42586e + f26e415 commit d521ed8
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib/chai/core/assertions.js
Expand Up @@ -2145,4 +2145,27 @@ module.exports = function (chai, _) {
, 'expected #{this} to not be frozen'
);
});

/**
* ### .finite
*
* Asserts that the target is a finite number. Unlike `.a('number')`, this will fail for `NaN` and `Infinity`.
*
* expect(4).to.be.finite;
* expect(NaN).to.not.be.finite;
*
* @name finite
* @namespace BDD
* @api public
*/

Assertion.addProperty('finite', function(msg) {
var obj = flag(this, 'object');

this.assert(
typeof obj === "number" && isFinite(obj)
, 'expected #{this} to be a finite number'
, 'expected #{this} to not be a finite number'
);
});
};
21 changes: 21 additions & 0 deletions lib/chai/interface/assert.js
Expand Up @@ -732,6 +732,27 @@ module.exports = function (chai, util) {
new Assertion(val, msg).to.not.be.a('number');
};

/**
* ### .isFinite(value, [message])
*
* Asserts that `value` is a finite number. Unlike `.isNumber`, this will fail for `NaN` and `Infinity`
*
* var cups = 2;
* assert.isFinite(cups, 'how many cups');
*
* assert.isFinite(NaN); // throws
*
* @name isFinite
* @param {Number} value
* @param {String} message
* @namespace Assert
* @api public
*/

assert.isFinite = function (val, msg) {
new Assertion(val, msg).to.be.finite;
};

/**
* ### .isBoolean(value, [message])
*
Expand Down
25 changes: 25 additions & 0 deletions test/assert.js
Expand Up @@ -459,6 +459,31 @@ describe('assert', function () {
}, "expected 4 not to be a number");
});

it('isFinite', function() {
assert.isFinite(4);
assert.isFinite(-10);

err(function(){
assert.isFinite(NaN);
}, "expected NaN to be a finite number");

err(function(){
assert.isFinite(Infinity);
}, "expected Infinity to be a finite number");

err(function(){
assert.isFinite('foo');
}, "expected \'foo\' to be a finite number");

err(function(){
assert.isFinite([]);
}, "expected [] to be a finite number");

err(function(){
assert.isFinite({});
}, "expected {} to be a finite number");
})

it('isBoolean', function() {
assert.isBoolean(true);
assert.isBoolean(false);
Expand Down
25 changes: 25 additions & 0 deletions test/expect.js
Expand Up @@ -493,6 +493,31 @@ describe('expect', function () {
}, "expected 'foo' not to be NaN");
});

it('finite', function() {
expect(4).to.be.finite;
expect(-10).to.be.finite;

err(function(){
expect(NaN).to.be.finite;
}, "expected NaN to be a finite number");

err(function(){
expect(Infinity).to.be.finite;
}, "expected Infinity to be a finite number");

err(function(){
expect('foo').to.be.finite;
}, "expected \'foo\' to be a finite number");

err(function(){
expect([]).to.be.finite;
}, "expected [] to be a finite number");

err(function(){
expect({}).to.be.finite;
}, "expected {} to be a finite number");
});

it('property(name)', function(){
expect('test').to.have.property('length');
expect(4).to.not.have.property('length');
Expand Down
25 changes: 25 additions & 0 deletions test/should.js
Expand Up @@ -431,6 +431,31 @@ describe('should', function() {
}, "expected { foo: \'bar\' } to be empty");
});

it('finite(value)', function() {
(4).should.be.finite;
(-10).should.be.finite;

err(function(){
(NaN).should.be.finite;
}, "expected NaN to be a finite number");

err(function(){
(Infinity).should.be.finite;
}, "expected Infinity to be a finite number");

err(function(){
('foo').should.be.finite;
}, "expected \'foo\' to be a finite number");

err(function(){
([]).should.be.finite;
}, "expected [] to be a finite number");

err(function(){
({}).should.be.finite;
}, "expected {} to be a finite number");
});

it('property(name)', function(){
'test'.should.have.property('length');
(4).should.not.have.property('length');
Expand Down

0 comments on commit d521ed8

Please sign in to comment.