Skip to content

Commit

Permalink
Verify types on within and related assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
vieiralucas authored and meeber committed Sep 8, 2016
1 parent df1a892 commit c3e8285
Show file tree
Hide file tree
Showing 4 changed files with 292 additions and 10 deletions.
75 changes: 65 additions & 10 deletions lib/chai/core/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -645,9 +645,20 @@ module.exports = function (chai, _) {

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 Down Expand Up @@ -694,9 +705,20 @@ module.exports = function (chai, _) {

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 Down Expand Up @@ -743,9 +765,20 @@ module.exports = function (chai, _) {

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 Down Expand Up @@ -792,9 +825,20 @@ module.exports = function (chai, _) {

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 Down Expand Up @@ -841,9 +885,20 @@ module.exports = function (chai, _) {
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 @@ -1676,6 +1676,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('atLeast', function() {
Expand All @@ -1698,6 +1706,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('atMost', function() {
Expand Down
108 changes: 108 additions & 0 deletions test/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,34 @@ describe('expect', function () {
err(function () {
expect([ 1, 2, 3 ]).to.have.length.within(5,7, 'blah');
}, "blah: expected [ 1, 2, 3 ] to have a length within 5..7");

err(function () {
expect(null).to.be.within(0, 1, 'blah');
}, "blah: expected null to be a number");

err(function () {
expect(1).to.be.within(null, 1, 'blah');
}, "the arguments to within must be numbers");

err(function () {
expect(1).to.be.within(0, null, 'blah');
}, "the arguments to within must be numbers");

err(function () {
expect(null).to.not.be.within(0, 1, 'blah');
}, "blah: expected null to be a number");

err(function () {
expect(1).to.not.be.within(null, 1, 'blah');
}, "the arguments to within must be numbers");

err(function () {
expect(1).to.not.be.within(0, null, 'blah');
}, "the arguments to within must be numbers");

err(function () {
expect(1).to.have.length.within(5,7, 'blah');
}, "blah: expected 1 to have a property 'length'");
});

it('above(n)', function(){
Expand All @@ -242,6 +270,26 @@ describe('expect', function () {
err(function () {
expect([ 1, 2, 3 ]).to.have.length.above(4, 'blah');
}, "blah: expected [ 1, 2, 3 ] to have a length above 4 but got 3");

err(function () {
expect(null).to.be.above(0, 'blah');
}, "blah: expected null to be a number");

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

err(function () {
expect(null).to.not.be.above(0, 'blah');
}, "blah: expected null to be a number");

err(function () {
expect(1).to.not.be.above(null, 'blah');
}, "the argument to above must be a number");

err(function () {
expect(1).to.have.length.above(0, 'blah');
}, "blah: expected 1 to have a property 'length'");
});

it('least(n)', function(){
Expand Down Expand Up @@ -270,6 +318,26 @@ describe('expect', function () {
err(function () {
expect([ 1, 2, 3, 4 ]).to.not.have.length.of.at.least(4, 'blah');
}, "blah: expected [ 1, 2, 3, 4 ] to have a length below 4");

err(function () {
expect(null).to.be.at.least(0, 'blah');
}, "blah: expected null to be a number");

err(function () {
expect(1).to.be.at.least(null, 'blah');
}, "the argument to least must be a number");

err(function () {
expect(null).to.not.be.at.least(0, 'blah');
}, "blah: expected null to be a number");

err(function () {
expect(1).to.not.be.at.least(null, 'blah');
}, "the argument to least must be a number");

err(function () {
expect(1).to.have.length.at.least(0, 'blah');
}, "blah: expected 1 to have a property 'length'");
});

it('below(n)', function(){
Expand All @@ -295,6 +363,26 @@ describe('expect', function () {
err(function () {
expect([ 1, 2, 3 ]).to.have.length.below(2, 'blah');
}, "blah: expected [ 1, 2, 3 ] to have a length below 2 but got 3");

err(function () {
expect(null).to.be.below(0, 'blah');
}, "blah: expected null to be a number");

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

err(function () {
expect(null).to.not.be.below(0, 'blah');
}, "blah: expected null to be a number");

err(function () {
expect(1).to.not.be.below(null, 'blah');
}, "the argument to below must be a number");

err(function () {
expect(1).to.have.length.below(0, 'blah');
}, "blah: expected 1 to have a property 'length'");
});

it('most(n)', function(){
Expand Down Expand Up @@ -324,6 +412,26 @@ describe('expect', function () {
err(function () {
expect([ 1, 2 ]).to.not.have.length.of.at.most(2, 'blah');
}, "blah: expected [ 1, 2 ] to have a length above 2");

err(function () {
expect(null).to.be.at.most(0, 'blah');
}, "blah: expected null to be a number");

err(function () {
expect(1).to.be.at.most(null, 'blah');
}, "the argument to most must be a number");

err(function () {
expect(null).to.not.be.at.most(0, 'blah');
}, "blah: expected null to be a number");

err(function () {
expect(1).to.not.be.at.most(null, 'blah');
}, "the argument to most must be a number");

err(function () {
expect(1).to.have.length.of.at.most(0, 'blah');
}, "blah: expected 1 to have a property 'length'");
});

it('match(regexp)', function(){
Expand Down
Loading

0 comments on commit c3e8285

Please sign in to comment.