Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verify types on increase | decrease #796

Merged
merged 3 commits into from
Sep 12, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 11 additions & 5 deletions lib/chai/core/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ module.exports = function (chai, _) {
* expect([1,2,3]).to.include(2);
* expect('foobar').to.contain('foo');
* expect({ foo: 'bar', hello: 'universe' }).to.include({ foo: 'bar' });
*
*
* By default, strict equality (===) is used. When asserting the inclusion of
* a value in an array, the array is searched for an element that's strictly
* equal to the given value. When asserting a subset of properties in an
Expand Down Expand Up @@ -319,7 +319,7 @@ module.exports = function (chai, _) {

return;
}

// Assert inclusion in an array or substring in a string.
this.assert(
typeof obj === 'string' || !isDeep ? ~obj.indexOf(val)
Expand Down Expand Up @@ -1764,7 +1764,7 @@ module.exports = function (chai, _) {
if (!cmp) {
var matchIdx = superset.indexOf(elem);
if (matchIdx === -1) return false;

// Remove match from superset so not counted twice if duplicate in subset.
if (!contains) superset.splice(matchIdx, 1);
return true;
Expand Down Expand Up @@ -1961,7 +1961,7 @@ module.exports = function (chai, _) {
/**
* ### .increase(function)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the docs for this could use another name for the argument being passed other than function.

*
* Asserts that a function increases an object property
* Asserts that a function increases a numeric object property
*
* var obj = { val: 10 };
* var fn = function() { obj.val = 15 };
Expand Down Expand Up @@ -1991,6 +1991,9 @@ module.exports = function (chai, _) {
initial = target[prop];
}

// Make sure that the target is a number
new Assertion(initial).is.a('number');

fn();

var final = prop === undefined || prop === null ? target() : target[prop];
Expand All @@ -2015,7 +2018,7 @@ module.exports = function (chai, _) {
/**
* ### .decrease(function)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same applies for this line, what about renaming function to target on these cases?

*
* Asserts that a function decreases an object property
* Asserts that a function decreases a numeric object property
*
* var obj = { val: 10 };
* var fn = function() { obj.val = 5 };
Expand Down Expand Up @@ -2045,6 +2048,9 @@ module.exports = function (chai, _) {
initial = target[prop];
}

// Make sure that the target is a number
new Assertion(initial).is.a('number');

fn();

var final = prop === undefined || prop === null ? target() : target[prop];
Expand Down
18 changes: 9 additions & 9 deletions lib/chai/interface/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -2182,7 +2182,7 @@ module.exports = function (chai, util) {
/**
* ### .increases(function, object, property, [message])
*
* Asserts that a function increases an object property
* Asserts that a function increases a numeric object property
*
* var obj = { val: 10 };
* var fn = function() { obj.val = 13 };
Expand All @@ -2209,7 +2209,7 @@ module.exports = function (chai, util) {
/**
* ### .increasesBy(function, object, property, delta, [message])
*
* Asserts that a function increases an object property or a function's return value by an amount (delta)
* Asserts that a function increases a numeric object property or a function's return value by an amount (delta)
*
* var obj = { val: 10 };
* var fn = function() { obj.val += 10 };
Expand Down Expand Up @@ -2241,7 +2241,7 @@ module.exports = function (chai, util) {
/**
* ### .doesNotIncrease(function, object, property, [message])
*
* Asserts that a function does not increase object property
* Asserts that a function does not increase a numeric object property
*
* var obj = { val: 10 };
* var fn = function() { obj.val = 8 };
Expand All @@ -2268,7 +2268,7 @@ module.exports = function (chai, util) {
/**
* ### .increasesButNotBy(function, object, property, [message])
*
* Asserts that a function does not increase object property or function's return value by an amount (delta)
* Asserts that a function does not increase a numeric object property or function's return value by an amount (delta)
*
* var obj = { val: 10 };
* var fn = function() { obj.val = 15 };
Expand Down Expand Up @@ -2300,7 +2300,7 @@ module.exports = function (chai, util) {
/**
* ### .decreases(function, object, property, [message])
*
* Asserts that a function decreases an object property
* Asserts that a function decreases a numeric object property
*
* var obj = { val: 10 };
* var fn = function() { obj.val = 5 };
Expand All @@ -2327,7 +2327,7 @@ module.exports = function (chai, util) {
/**
* ### .decreasesBy(function, object, property, delta, [message])
*
* Asserts that a function decreases an object property or a function's return value by an amount (delta)
* Asserts that a function decreases a numeric object property or a function's return value by an amount (delta)
*
* var obj = { val: 10 };
* var fn = function() { obj.val -= 5 };
Expand Down Expand Up @@ -2359,7 +2359,7 @@ module.exports = function (chai, util) {
/**
* ### .doesNotDecrease(function, object, property, [message])
*
* Asserts that a function does not decreases an object property
* Asserts that a function does not decreases a numeric object property
*
* var obj = { val: 10 };
* var fn = function() { obj.val = 15 };
Expand All @@ -2386,7 +2386,7 @@ module.exports = function (chai, util) {
/**
* ### .doesNotDecreaseBy(function, object, property, delta, [message])
*
* Asserts that a function does not decreases an object property or a function's return value by an amount (delta)
* Asserts that a function does not decreases a numeric object property or a function's return value by an amount (delta)
*
* var obj = { val: 10 };
* var fn = function() { obj.val = 5 };
Expand Down Expand Up @@ -2418,7 +2418,7 @@ module.exports = function (chai, util) {
/**
* ### .decreasesButNotBy(function, object, property, delta, [message])
*
* Asserts that a function does not decreases an object property or a function's return value by an amount (delta)
* Asserts that a function does not decreases a numeric object property or a function's return value by an amount (delta)
*
* var obj = { val: 10 };
* var fn = function() { obj.val = 5 };
Expand Down
41 changes: 31 additions & 10 deletions test/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ describe('assert', function () {
it('exists', function() {
var meeber = 'awesome';
var iDoNotExist;

assert.exists(meeber);
assert.exists(0);
assert.exists(false);
Expand All @@ -379,7 +379,7 @@ describe('assert', function () {
it('notExists', function() {
var meeber = 'awesome';
var iDoNotExist;

assert.notExists(iDoNotExist);

err(function (){
Expand Down Expand Up @@ -728,7 +728,7 @@ describe('assert', function () {
var aKey = {thisIs: 'anExampleObject'}
, anotherKey = {doingThisBecauseOf: 'referential equality'}
, testMap = new Map();

testMap.set(aKey, 'aValue');
testMap.set(anotherKey, 'anotherValue');

Expand All @@ -751,7 +751,7 @@ describe('assert', function () {
, weirdMapKey2 = {toString: NaN}
, weirdMapKey3 = []
, weirdMap = new Map();

weirdMap.set(weirdMapKey1, 'val1');
weirdMap.set(weirdMapKey2, 'val2');

Expand All @@ -763,7 +763,7 @@ describe('assert', function () {
, symMapKey2 = Symbol()
, symMapKey3 = Symbol()
, symMap = new Map();

symMap.set(symMapKey1, 'val1');
symMap.set(symMapKey2, 'val2');

Expand Down Expand Up @@ -825,7 +825,7 @@ describe('assert', function () {
var aKey = {thisIs: 'anExampleObject'}
, anotherKey = {doingThisBecauseOf: 'referential equality'}
, testSet = new Set();

testSet.add(aKey);
testSet.add(anotherKey);

Expand All @@ -848,7 +848,7 @@ describe('assert', function () {
, weirdSetKey2 = {toString: NaN}
, weirdSetKey3 = []
, weirdSet = new Set();

weirdSet.add(weirdSetKey1);
weirdSet.add(weirdSetKey2);

Expand All @@ -860,7 +860,7 @@ describe('assert', function () {
, symSetKey2 = Symbol()
, symSetKey3 = Symbol()
, symSet = new Set();

symSet.add(symSetKey1);
symSet.add(symSetKey2);

Expand All @@ -873,7 +873,7 @@ describe('assert', function () {
}

var errSet = new Set();

errSet.add({1: 20});
errSet.add('number');

Expand Down Expand Up @@ -1693,6 +1693,13 @@ describe('assert', function () {
err(function() {
assert.isAtLeast(1, 3);
}, 'expected 1 to be at least 3');

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

it('below', function() {
Expand Down Expand Up @@ -1722,6 +1729,13 @@ describe('assert', function () {
err(function() {
assert.isAtMost(3, 1);
}, 'expected 3 to be at most 1');

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

it('change', function() {
Expand Down Expand Up @@ -1749,7 +1763,7 @@ describe('assert', function () {
});

it('increase, decrease', function() {
var obj = { value: 10 },
var obj = { value: 10, noop: null },
arr = ['one', 'two'],
pFn = function() { arr.push('three') },
popFn = function() { arr.pop() },
Expand Down Expand Up @@ -1777,6 +1791,13 @@ describe('assert', function () {
assert.doesNotIncrease(popFn, lenFn);
assert.increasesBy(pFn, lenFn, 1);
assert.increasesButNotBy(pFn, lenFn, 2);

err(function() {
assert.increases(incFn, obj, 'noop');
}, 'expected null to be a number');
err(function() {
assert.decreases(incFn, obj, 'noop');
}, 'expected null to be a number');
});

it('isExtensible / extensible', function() {
Expand Down
25 changes: 16 additions & 9 deletions test/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ describe('expect', function () {
err(function(){
expect().to.not.be.empty;
}, "expected undefined to exist");

});

it('NaN', function() {
Expand Down Expand Up @@ -797,7 +797,7 @@ describe('expect', function () {
expect(obj).to.not.have.deep.property('a', {b: 1}, 'blah');
}, "blah: expected { a: { b: 1 } } to not have a deep property 'a' of { b: 1 }");
});

it('nested.property(name, val)', function(){
expect({ foo: { bar: 'baz' } })
.to.have.nested.property('foo.bar', 'baz');
Expand Down Expand Up @@ -1182,7 +1182,7 @@ describe('expect', function () {
var aKey = {thisIs: 'anExampleObject'}
, anotherKey = {doingThisBecauseOf: 'referential equality'}
, testMap = new Map();

testMap.set(aKey, 'aValue');
testMap.set(anotherKey, 'anotherValue');

Expand All @@ -1209,7 +1209,7 @@ describe('expect', function () {
, weirdMapKey2 = {toString: NaN}
, weirdMapKey3 = []
, weirdMap = new Map();

weirdMap.set(weirdMapKey1, 'val1');
weirdMap.set(weirdMapKey2, 'val2');

Expand All @@ -1221,7 +1221,7 @@ describe('expect', function () {
, symMapKey2 = Symbol()
, symMapKey3 = Symbol()
, symMap = new Map();

symMap.set(symMapKey1, 'val1');
symMap.set(symMapKey2, 'val2');

Expand Down Expand Up @@ -1262,7 +1262,7 @@ describe('expect', function () {
var aKey = {thisIs: 'anExampleObject'}
, anotherKey = {doingThisBecauseOf: 'referential equality'}
, testSet = new Set();

testSet.add(aKey);
testSet.add(anotherKey);

Expand All @@ -1289,7 +1289,7 @@ describe('expect', function () {
, weirdSetKey2 = {toString: NaN}
, weirdSetKey3 = []
, weirdSet = new Set();

weirdSet.add(weirdSetKey1);
weirdSet.add(weirdSetKey2);

Expand All @@ -1301,7 +1301,7 @@ describe('expect', function () {
, symSetKey2 = Symbol()
, symSetKey3 = Symbol()
, symSet = new Set();

symSet.add(symSetKey1);
symSet.add(symSetKey2);

Expand Down Expand Up @@ -1898,7 +1898,7 @@ describe('expect', function () {
});

it('increase, decrease', function() {
var obj = { value: 10 },
var obj = { value: 10, noop: null },
arr = ['one', 'two'],
pFn = function() { arr.push('three') },
popFn = function() { arr.pop() },
Expand Down Expand Up @@ -1931,6 +1931,13 @@ describe('expect', function () {
expect(popFn).to.decrease(lenFn).but.not.by(2);
expect(nFn).to.not.decrease(lenFn);
expect(pFn).to.not.decrease(lenFn);

err(function() {
expect(incFn).to.increase(obj, 'noop');
}, 'expected null to be a number');
err(function() {
expect(incFn).to.decrease(obj, 'noop');
}, 'expected null to be a number');
});

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