Skip to content

Commit

Permalink
Merge pull request #796 from vieiralucas/verify-type-round2
Browse files Browse the repository at this point in the history
Verify types on increase | decrease
  • Loading branch information
lucasfcosta committed Sep 12, 2016
2 parents 95b511a + 5f3c4ae commit 662b76c
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 46 deletions.
42 changes: 31 additions & 11 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 @@ -1959,19 +1959,26 @@ module.exports = function (chai, _) {
Assertion.addChainableMethod('changes', assertChanges);

/**
* ### .increase(function)
* ### .increase(target[, 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 = 15 };
* expect(fn).to.increase(obj, 'val');
*
* It can also receive a function which returns the property
*
* var obj = { val: 10 };
* var fn = function() { obj.val = 5 };
* var getVal = function() { return obj.val };
* expect(fn).to.increase(getVal);
*
* @name increase
* @alias increases
* @alias Increase
* @param {String} target
* @param {String} property name
* @param {String|Function} target
* @param {String} property name _optional_
* @param {String} message _optional_
* @namespace BDD
* @api public
Expand All @@ -1991,6 +1998,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 @@ -2013,19 +2023,26 @@ module.exports = function (chai, _) {
Assertion.addChainableMethod('increases', assertIncreases);

/**
* ### .decrease(function)
* ### .decrease(target[, 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 };
* expect(fn).to.decrease(obj, 'val');
*
* It can also receive a function which returns the property
*
* var obj = { val: 10 };
* var fn = function() { obj.val = 5 };
* var getVal = function() { return obj.val };
* expect(fn).to.decrease(getVal);
*
* @name decrease
* @alias decreases
* @alias Decrease
* @param {String} target
* @param {String} property name
* @param {String|Function} target
* @param {String} property name _optional_
* @param {String} message _optional_
* @namespace BDD
* @api public
Expand All @@ -2045,6 +2062,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 @@ -2184,7 +2184,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 @@ -2211,7 +2211,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 @@ -2243,7 +2243,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 @@ -2270,7 +2270,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 @@ -2302,7 +2302,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 @@ -2329,7 +2329,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 @@ -2361,7 +2361,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 @@ -2388,7 +2388,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 @@ -2420,7 +2420,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

0 comments on commit 662b76c

Please sign in to comment.