From f158523321db467c9b0172020d2a11366fdf5be9 Mon Sep 17 00:00:00 2001 From: lucasfcosta Date: Mon, 1 Feb 2016 20:01:54 -0200 Subject: [PATCH] Increases and Decreases assertions accepting function for value --- lib/chai/core/assertions.js | 43 +++++++++++++++++++++++++++--------- lib/chai/interface/assert.js | 36 +++++++++++++++++++++++------- test/assert.js | 10 +++++++++ test/expect.js | 13 +++++++++++ test/should.js | 13 +++++++++++ 5 files changed, 96 insertions(+), 19 deletions(-) diff --git a/lib/chai/core/assertions.js b/lib/chai/core/assertions.js index f5803a340..330dc03e4 100644 --- a/lib/chai/core/assertions.js +++ b/lib/chai/core/assertions.js @@ -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]; @@ -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' ); } @@ -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' ); } diff --git a/lib/chai/interface/assert.js b/lib/chai/interface/assert.js index 7cd428782..0d1107d89 100644 --- a/lib/chai/interface/assert.js +++ b/lib/chai/interface/assert.js @@ -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); } @@ -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); } @@ -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); } @@ -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); } diff --git a/test/assert.js b/test/assert.js index 1e88717f0..f5c5a61f2 100644 --- a/test/assert.js +++ b/test/assert.js @@ -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 }; @@ -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() { diff --git a/test/expect.js b/test/expect.js index 7c3d38d80..df4d142be 100644 --- a/test/expect.js +++ b/test/expect.js @@ -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 }; @@ -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() { diff --git a/test/should.js b/test/should.js index dbe12760e..b3cb65da8 100644 --- a/test/should.js +++ b/test/should.js @@ -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 }; @@ -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() {