Skip to content

Commit

Permalink
Merge pull request #692 from vieiralucas/4.x.x
Browse files Browse the repository at this point in the history
Verify types on within and related assertions
  • Loading branch information
meeber committed May 4, 2016
2 parents 6b11cc5 + de86590 commit 4908550
Show file tree
Hide file tree
Showing 4 changed files with 314 additions and 22 deletions.
105 changes: 85 additions & 20 deletions lib/chai/core/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -504,14 +504,15 @@ module.exports = function (chai, _) {
/**
* ### .above(value)
*
* Asserts that the target is greater than `value`.
* Asserts that the number target is greater than `value`.
*
* expect(10).to.be.above(5);
*
* Can also be used in conjunction with `length` to
* assert a minimum length. The benefit being a
* more informative error message than if the length
* was supplied directly.
* was supplied directly. In this case the target must
* have a length property.
*
* expect('foo').to.have.length.above(2);
* expect([ 1, 2, 3 ]).to.have.length.above(2);
Expand All @@ -521,14 +522,26 @@ module.exports = function (chai, _) {
* @alias greaterThan
* @param {Number} value
* @param {String} message _optional_
* @namespace BDD
* @api public
*/

function assertAbove (n, msg) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object');
if (flag(this, 'doLength')) {
var obj = flag(this, 'object')
, doLength = flag(this, 'doLength');

if (doLength) {
new Assertion(obj, msg).to.have.property('length');
} else {
new Assertion(obj, msg).is.a('number');
}

if (_.type(n) !== 'number') {
throw new Error('the argument to above must be a number');
}

if (doLength) {
var len = obj.length;
this.assert(
len > n
Expand All @@ -553,14 +566,15 @@ module.exports = function (chai, _) {
/**
* ### .least(value)
*
* Asserts that the target is greater than or equal to `value`.
* Asserts that the number target is greater than or equal to `value`.
*
* expect(10).to.be.at.least(10);
*
* Can also be used in conjunction with `length` to
* assert a minimum length. The benefit being a
* more informative error message than if the length
* was supplied directly.
* was supplied directly. In this case the target must
* have a length property.
*
* expect('foo').to.have.length.of.at.least(2);
* expect([ 1, 2, 3 ]).to.have.length.of.at.least(3);
Expand All @@ -569,14 +583,26 @@ module.exports = function (chai, _) {
* @alias gte
* @param {Number} value
* @param {String} message _optional_
* @namespace BDD
* @api public
*/

function assertLeast (n, msg) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object');
if (flag(this, 'doLength')) {
var obj = flag(this, 'object')
, doLength = flag(this, 'doLength');

if (doLength) {
new Assertion(obj, msg).to.have.property('length');
} else {
new Assertion(obj, msg).is.a('number');
}

if (_.type(n) !== 'number') {
throw new Error('the argument to least must be a number');
}

if (doLength) {
var len = obj.length;
this.assert(
len >= n
Expand All @@ -600,14 +626,15 @@ module.exports = function (chai, _) {
/**
* ### .below(value)
*
* Asserts that the target is less than `value`.
* Asserts that the number target is less than `value`.
*
* expect(5).to.be.below(10);
*
* Can also be used in conjunction with `length` to
* assert a maximum length. The benefit being a
* more informative error message than if the length
* was supplied directly.
* was supplied directly. In this case the target must
* have a length property.
*
* expect('foo').to.have.length.below(4);
* expect([ 1, 2, 3 ]).to.have.length.below(4);
Expand All @@ -617,14 +644,26 @@ module.exports = function (chai, _) {
* @alias lessThan
* @param {Number} value
* @param {String} message _optional_
* @namespace BDD
* @api public
*/

function assertBelow (n, msg) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object');
if (flag(this, 'doLength')) {
var obj = flag(this, 'object')
, doLength = flag(this, 'doLength');

if (doLength) {
new Assertion(obj, msg).to.have.property('length');
} else {
new Assertion(obj, msg).is.a('number');
}

if (_.type(n) !== 'number') {
throw new Error('the argument to below must be a number');
}

if (doLength) {
var len = obj.length;
this.assert(
len < n
Expand All @@ -649,14 +688,15 @@ module.exports = function (chai, _) {
/**
* ### .most(value)
*
* Asserts that the target is less than or equal to `value`.
* Asserts that the number target is less than or equal to `value`.
*
* expect(5).to.be.at.most(5);
*
* Can also be used in conjunction with `length` to
* assert a maximum length. The benefit being a
* more informative error message than if the length
* was supplied directly.
* was supplied directly. In this case the target must
* have a length property.
*
* expect('foo').to.have.length.of.at.most(4);
* expect([ 1, 2, 3 ]).to.have.length.of.at.most(3);
Expand All @@ -665,14 +705,26 @@ module.exports = function (chai, _) {
* @alias lte
* @param {Number} value
* @param {String} message _optional_
* @namespace BDD
* @api public
*/

function assertMost (n, msg) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object');
if (flag(this, 'doLength')) {
var obj = flag(this, 'object')
, doLength = flag(this, 'doLength');

if (doLength) {
new Assertion(obj, msg).to.have.property('length');
} else {
new Assertion(obj, msg).is.a('number');
}

if (_.type(n) !== 'number') {
throw new Error('the argument to most must be a number');
}

if (doLength) {
var len = obj.length;
this.assert(
len <= n
Expand All @@ -696,14 +748,15 @@ module.exports = function (chai, _) {
/**
* ### .within(start, finish)
*
* Asserts that the target is within a range.
* Asserts that the number target is within a range of numbers.
*
* expect(7).to.be.within(5,10);
*
* Can also be used in conjunction with `length` to
* assert a length range. The benefit being a
* more informative error message than if the length
* was supplied directly.
* was supplied directly. In this case the target must
* have a length property.
*
* expect('foo').to.have.length.within(2,4);
* expect([ 1, 2, 3 ]).to.have.length.within(2,4);
Expand All @@ -712,15 +765,27 @@ module.exports = function (chai, _) {
* @param {Number} start lowerbound inclusive
* @param {Number} finish upperbound inclusive
* @param {String} message _optional_
* @namespace BDD
* @api public
*/

Assertion.addMethod('within', function (start, finish, msg) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object')
, range = start + '..' + finish;
if (flag(this, 'doLength')) {
, range = start + '..' + finish
, doLength = flag(this, 'doLength');

if (doLength) {
new Assertion(obj, msg).to.have.property('length');
} else {
new Assertion(obj, msg).is.a('number');
}

if (_.type(start) !== 'number' || _.type(finish) !== 'number') {
throw new Error('the arguments to within must be numbers');
}

if (doLength) {
var len = obj.length;
this.assert(
len >= start && len <= finish
Expand Down
15 changes: 15 additions & 0 deletions test/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,14 @@ describe('assert', function () {
err(function() {
assert.isAbove(1, 1);
}, 'expected 1 to be above 1');

err(function() {
assert.isAbove(null, 1);
}, 'expected null to be a number');

err(function() {
assert.isAbove(1, null);
}, 'the argument to above must be a number');
});

it('below', function() {
Expand All @@ -802,6 +810,13 @@ describe('assert', function () {
assert.isBelow(1, 1);
}, 'expected 1 to be below 1');

err(function() {
assert.isBelow(null, 1);
}, 'expected null to be a number');

err(function() {
assert.isBelow(1, null);
}, 'the argument to below must be a number');
});

it('memberDeepEquals', function() {
Expand Down

0 comments on commit 4908550

Please sign in to comment.