Skip to content

Commit

Permalink
Merge pull request #480 from bradcypert/NaN-support
Browse files Browse the repository at this point in the history
Added tests and expectations for NaN. Ex: `expect(4).not.to.be.NaN;`
  • Loading branch information
keithamus committed Jul 16, 2015
2 parents 95a172b + a5687a2 commit 5520c16
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lib/chai/core/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,25 @@ module.exports = function (chai, _) {
);
});

/**
* ### .NaN
* Asserts that the target is `NaN`.
*
* expect('foo').to.be.NaN;
* expect(4).not.to.be.NaN;
*
* @name NaN
* @api public
*/

Assertion.addProperty('NaN', function () {
this.assert(
isNaN(flag(this, 'object'))
, 'expected #{this} to be NaN'
, 'expected #{this} not to be NaN'
);
});

/**
* ### .exist
*
Expand Down
31 changes: 31 additions & 0 deletions lib/chai/interface/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,37 @@ module.exports = function (chai, util) {
new Assertion(val, msg).to.not.equal(null);
};

/**
* ### .isNaN
* Asserts that value is NaN
*
* assert.isNaN('foo', 'foo is NaN');
*
* @name isNaN
* @param {Mixed} value
* @param {String} message
* @api public
*/

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

/**
* ### .isNotNaN
* Asserts that value is not NaN
*
* assert.isNotNaN(4, '4 is not NaN');
*
* @name isNotNaN
* @param {Mixed} value
* @param {String} message
* @api public
*/
assert.isNotNaN = function (val, msg) {
new Assertion(val, msg).not.to.be.NaN;
};

/**
* ### .isUndefined(value, [message])
*
Expand Down
16 changes: 16 additions & 0 deletions test/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,22 @@ describe('assert', function () {
}, "expected null to not equal null");
});

it('isNaN', function() {
assert.isNaN('hello');

err(function (){
assert.isNaN(4);
}, "expected 4 to be NaN");
});

it('isNotNaN', function() {
assert.isNotNaN(4);

err(function (){
assert.isNotNaN('hello');
}, "expected 'hello' not to be NaN");
});

it('isUndefined', function() {
assert.isUndefined(undefined);

Expand Down
20 changes: 20 additions & 0 deletions test/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,26 @@ describe('expect', function () {
}, "expected { foo: \'bar\' } to be empty");
});

it('NaN', function() {
expect(NaN).to.be.NaN;
expect('foo').to.be.NaN;
expect({}).to.be.NaN;
expect(4).not.to.be.NaN;
expect([]).not.to.be.NaN;

err(function(){
expect(4).to.be.NaN;
}, "expected 4 to be NaN");

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

err(function(){
expect('foo').not.to.be.NaN;
}, "expected 'foo' not to be NaN");
});

it('property(name)', function(){
expect('test').to.have.property('length');
expect(4).to.not.have.property('length');
Expand Down
9 changes: 9 additions & 0 deletions test/should.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ describe('should', function() {
}, "expected '' to be null")
});

it('NaN', function(){
'foo'.should.be.NaN;
(4).should.not.be.NaN;

err(function(){
(4).should.be.NaN;
}, "expected 4 to be NaN")
});

it('undefined', function(){
(0).should.not.be.undefined;

Expand Down

0 comments on commit 5520c16

Please sign in to comment.