Skip to content

Commit

Permalink
Increases and Decreases assertions accepting function for value
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfcosta committed Feb 11, 2016
1 parent 0fe4718 commit f158523
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 19 deletions.
43 changes: 32 additions & 11 deletions lib/chai/core/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1646,9 +1646,10 @@ module.exports = function (chai, _) {
var fn = flag(this, 'object');
new Assertion(fn).is.a('function');

var initial;
if (!prop) {
new Assertion(object).is.a('function');
var initial = object();
initial = object();
} else {
new Assertion(object, msg).to.have.property(prop);
initial = object[prop];
Expand Down Expand Up @@ -1691,16 +1692,26 @@ module.exports = function (chai, _) {
function assertIncreases (object, prop, msg) {
if (msg) flag(this, 'message', msg);
var fn = flag(this, 'object');
new Assertion(object, msg).to.have.property(prop);
new Assertion(fn).is.a('function');

var initial = object[prop];
var initial;
if (!prop) {
new Assertion(object).is.a('function');
initial = object();
} else {
new Assertion(object, msg).to.have.property(prop);
initial = object[prop];
}

fn();

var final = prop === undefined ? object() : object[prop];
var msgObj = prop === undefined ? initial : '.' + prop;

this.assert(
object[prop] - initial > 0
, 'expected .' + prop + ' to increase'
, 'expected .' + prop + ' to not increase'
final - initial > 0
, 'expected ' + msgObj + ' to increase'
, 'expected ' + msgObj + ' to not increase'
);
}

Expand Down Expand Up @@ -1729,16 +1740,26 @@ module.exports = function (chai, _) {
function assertDecreases (object, prop, msg) {
if (msg) flag(this, 'message', msg);
var fn = flag(this, 'object');
new Assertion(object, msg).to.have.property(prop);
new Assertion(fn).is.a('function');

var initial = object[prop];
var initial;
if (!prop) {
new Assertion(object).is.a('function');
initial = object();
} else {
new Assertion(object, msg).to.have.property(prop);
initial = object[prop];
}

fn();

var final = prop === undefined ? object() : object[prop];
var msgObj = prop === undefined ? initial : '.' + prop;

this.assert(
object[prop] - initial < 0
, 'expected .' + prop + ' to decrease'
, 'expected .' + prop + ' to not decrease'
final - initial < 0
, 'expected ' + msgObj + ' to decrease'
, 'expected ' + msgObj + ' to not decrease'
);
}

Expand Down
36 changes: 28 additions & 8 deletions lib/chai/interface/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -1408,14 +1408,19 @@ module.exports = function (chai, util) {
*
* @name increases
* @param {Function} modifier function
* @param {Object} object
* @param {String} property name
* @param {Object} object or getter function
* @param {String} property name _optional_
* @param {String} message _optional_
* @namespace Assert
* @api public
*/

assert.increases = function (fn, obj, prop, msg) {
if (arguments.length === 3 && typeof obj === 'function') {
msg = prop;
prop = null;
}

new Assertion(fn, msg).to.increase(obj, prop);
}

Expand All @@ -1430,14 +1435,19 @@ module.exports = function (chai, util) {
*
* @name doesNotIncrease
* @param {Function} modifier function
* @param {Object} object
* @param {String} property name
* @param {Object} object or getter function
* @param {String} property name _optional_
* @param {String} message _optional_
* @namespace Assert
* @api public
*/

assert.doesNotIncrease = function (fn, obj, prop, msg) {
if (arguments.length === 3 && typeof obj === 'function') {
msg = prop;
prop = null;
}

new Assertion(fn, msg).to.not.increase(obj, prop);
}

Expand All @@ -1452,14 +1462,19 @@ module.exports = function (chai, util) {
*
* @name decreases
* @param {Function} modifier function
* @param {Object} object
* @param {String} property name
* @param {Object} object or getter function
* @param {String} property name _optional_
* @param {String} message _optional_
* @namespace Assert
* @api public
*/

assert.decreases = function (fn, obj, prop, msg) {
if (arguments.length === 3 && typeof obj === 'function') {
msg = prop;
prop = null;
}

new Assertion(fn, msg).to.decrease(obj, prop);
}

Expand All @@ -1474,14 +1489,19 @@ module.exports = function (chai, util) {
*
* @name doesNotDecrease
* @param {Function} modifier function
* @param {Object} object
* @param {String} property name
* @param {Object} object or getter function
* @param {String} property name _optional_
* @param {String} message _optional_
* @namespace Assert
* @api public
*/

assert.doesNotDecrease = function (fn, obj, prop, msg) {
if (arguments.length === 3 && typeof obj === 'function') {
msg = prop;
prop = null;
}

new Assertion(fn, msg).to.not.decrease(obj, prop);
}

Expand Down
10 changes: 10 additions & 0 deletions test/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,10 @@ describe('assert', function () {

it('increase, decrease', function() {
var obj = { value: 10 },
arr = ['one', 'two'],
pFn = function() { arr.push('three') },
popFn = function() { arr.pop() },
lenFn = function() { return arr.length },
incFn = function() { obj.value += 2 },
decFn = function() { obj.value -= 3 },
smFn = function() { obj.value += 0 };
Expand All @@ -898,6 +902,12 @@ describe('assert', function () {

assert.increases(incFn, obj, 'value');
assert.doesNotIncrease(smFn, obj, 'value');

assert.decreases(popFn, lenFn);
assert.doesNotDecrease(pFn, lenFn);

assert.increases(pFn, lenFn);
assert.doesNotIncrease(popFn, lenFn);
});

it('isExtensible / extensible', function() {
Expand Down
13 changes: 13 additions & 0 deletions test/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -1138,6 +1138,11 @@ describe('expect', function () {

it('increase, decrease', function() {
var obj = { value: 10 },
arr = ['one', 'two'],
pFn = function() { arr.push('three') },
popFn = function() { arr.pop() },
nFn = function() { return null },
lenFn = function() { return arr.length },
incFn = function() { obj.value += 2 },
decFn = function() { obj.value -= 3 },
smFn = function() { obj.value += 0 };
Expand All @@ -1149,6 +1154,14 @@ describe('expect', function () {
expect(smFn).to.not.decrease(obj, 'value');
expect(incFn).to.not.decrease(obj, 'value');
expect(decFn).to.decrease(obj, 'value');

expect(popFn).to.not.increase(lenFn);
expect(nFn).to.not.increase(lenFn);
expect(pFn).to.increase(lenFn);

expect(popFn).to.decrease(lenFn);
expect(nFn).to.not.decrease(lenFn);
expect(pFn).to.not.decrease(lenFn);
});

it('extensible', function() {
Expand Down
13 changes: 13 additions & 0 deletions test/should.js
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,11 @@ describe('should', function() {

it('increase, decrease', function() {
var obj = { value: 10 },
arr = ['one', 'two'],
pFn = function() { arr.push('three') },
popFn = function() { arr.pop() },
lenFn = function() { return arr.length },
nFn = function() { return null },
incFn = function() { obj.value += 2 },
decFn = function() { obj.value -= 3 },
smFn = function() { obj.value += 0 };
Expand All @@ -980,6 +985,14 @@ describe('should', function() {
smFn.should.not.decrease(obj, 'value');
incFn.should.not.decrease(obj, 'value');
decFn.should.decrease(obj, 'value');

nFn.should.not.increase(lenFn);
popFn.should.not.increase(lenFn);
pFn.should.increase(lenFn);

nFn.should.not.decrease(lenFn);
pFn.should.not.decrease(lenFn);
popFn.should.decrease(lenFn);
});

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

0 comments on commit f158523

Please sign in to comment.