Skip to content

Commit

Permalink
Merge pull request #783 from meeber/4.x.x
Browse files Browse the repository at this point in the history
Merge rebased 4.x.x branch into master
  • Loading branch information
lucasfcosta authored Sep 10, 2016
2 parents e5e6f5c + 4a009de commit 8166f33
Show file tree
Hide file tree
Showing 11 changed files with 908 additions and 65 deletions.
3 changes: 2 additions & 1 deletion lib/chai/assertion.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ module.exports = function (_chai, util) {

Assertion.prototype.assert = function (expr, msg, negateMsg, expected, _actual, showDiff) {
var ok = util.test(this, arguments);
if (true !== showDiff) showDiff = false;
if (false !== showDiff) showDiff = true;
if (undefined === expected && undefined === _actual) showDiff = false;
if (true !== config.showDiff) showDiff = false;

if (!ok) {
Expand Down
165 changes: 125 additions & 40 deletions lib/chai/core/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ module.exports = function (chai, _) {
* ### .NaN
* Asserts that the target is `NaN`.
*
* expect('foo').to.be.NaN;
* expect(NaN).to.be.NaN;
* expect(4).not.to.be.NaN;
*
* @name NaN
Expand All @@ -456,7 +456,7 @@ module.exports = function (chai, _) {

Assertion.addProperty('NaN', function () {
this.assert(
isNaN(flag(this, 'object'))
_.isNaN(flag(this, 'object'))
, 'expected #{this} to be NaN'
, 'expected #{this} not to be NaN'
);
Expand Down Expand Up @@ -507,17 +507,10 @@ module.exports = function (chai, _) {
*/

Assertion.addProperty('empty', function () {
var obj = flag(this, 'object')
, expected = obj;

if (Array.isArray(obj) || 'string' === typeof object) {
expected = obj.length;
} else if (typeof obj === 'object') {
expected = Object.keys(obj).length;
}

var obj = flag(this, 'object');
new Assertion(obj).to.exist;
this.assert(
!expected
Object.keys(Object(obj)).length === 0
, 'expected #{this} to be empty'
, 'expected #{this} not to be empty'
);
Expand Down Expand Up @@ -629,14 +622,15 @@ module.exports = function (chai, _) {
/**
* ### .above(value)
*
* Asserts that the target is greater than `value`.
* Asserts that the number target is greater than `value`.
*
* expect(10).to.be.above(5);
*
* Can also be used in conjunction with `length` to
* assert a minimum length. The benefit being a
* more informative error message than if the length
* was supplied directly.
* was supplied directly. In this case the target must
* have a length property.
*
* expect('foo').to.have.length.above(2);
* expect([ 1, 2, 3 ]).to.have.length.above(2);
Expand All @@ -652,9 +646,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 All @@ -679,14 +684,15 @@ module.exports = function (chai, _) {
/**
* ### .least(value)
*
* Asserts that the target is greater than or equal to `value`.
* Asserts that the number target is greater than or equal to `value`.
*
* expect(10).to.be.at.least(10);
*
* Can also be used in conjunction with `length` to
* assert a minimum length. The benefit being a
* more informative error message than if the length
* was supplied directly.
* was supplied directly. In this case the target must
* have a length property.
*
* expect('foo').to.have.length.of.at.least(2);
* expect([ 1, 2, 3 ]).to.have.length.of.at.least(3);
Expand All @@ -701,9 +707,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 All @@ -727,14 +744,15 @@ module.exports = function (chai, _) {
/**
* ### .below(value)
*
* Asserts that the target is less than `value`.
* Asserts that the number target is less than `value`.
*
* expect(5).to.be.below(10);
*
* Can also be used in conjunction with `length` to
* assert a maximum length. The benefit being a
* more informative error message than if the length
* was supplied directly.
* was supplied directly. In this case the target must
* have a length property.
*
* expect('foo').to.have.length.below(4);
* expect([ 1, 2, 3 ]).to.have.length.below(4);
Expand All @@ -750,9 +768,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 All @@ -777,14 +806,15 @@ module.exports = function (chai, _) {
/**
* ### .most(value)
*
* Asserts that the target is less than or equal to `value`.
* Asserts that the number target is less than or equal to `value`.
*
* expect(5).to.be.at.most(5);
*
* Can also be used in conjunction with `length` to
* assert a maximum length. The benefit being a
* more informative error message than if the length
* was supplied directly.
* was supplied directly. In this case the target must
* have a length property.
*
* expect('foo').to.have.length.of.at.most(4);
* expect([ 1, 2, 3 ]).to.have.length.of.at.most(3);
Expand All @@ -799,9 +829,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 All @@ -825,14 +866,15 @@ module.exports = function (chai, _) {
/**
* ### .within(start, finish)
*
* Asserts that the target is within a range.
* Asserts that the number target is within a range of numbers.
*
* expect(7).to.be.within(5,10);
*
* Can also be used in conjunction with `length` to
* assert a length range. The benefit being a
* more informative error message than if the length
* was supplied directly.
* was supplied directly. In this case the target must
* have a length property.
*
* expect('foo').to.have.length.within(2,4);
* expect([ 1, 2, 3 ]).to.have.length.within(2,4);
Expand All @@ -848,9 +890,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 Expand Up @@ -1028,28 +1081,59 @@ module.exports = function (chai, _) {


/**
* ### .ownProperty(name)
* ### .ownProperty(name, [value])
*
* Asserts that the target has an own property `name`.
* Asserts that the target has an own property `name` and, optionally, if it has
* (or not, if using `.not`) the desired `value`.
*
* expect('test').to.have.ownProperty('length');
* expect('test').to.haveOwnProperty('length');
* expect('test').to.not.have.ownProperty('foo');
* expect('test').to.not.haveOwnProperty('foo');
* expect({ length: 12 }).to.have.ownProperty('length', 12);
* expect({ length: 1337 }).to.not.have.ownProperty('length', 20);
* expect({ length: 12 }).to.haveOwnProperty('length', 12);
* expect({ length: 1337 }).to.not.haveOwnProperty('length', 20);
*
* @name ownProperty
* @alias haveOwnProperty
* @param {String} name
* @param {Mixed} value (optional)
* @param {String} message _optional_
* @namespace BDD
* @api public
*/

function assertOwnProperty (name, msg) {
function assertOwnProperty (name, value, msg) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object');
this.assert(
Object.prototype.hasOwnProperty.call(obj, name)
, 'expected #{this} to have own property ' + _.inspect(name)
, 'expected #{this} to not have own property ' + _.inspect(name)
);
var negate = flag(this, 'negate');
var objHasProperty = Object.prototype.hasOwnProperty.call(obj, name)
var actualValue = obj[name];

if (negate && value !== undefined) {
if (actualValue === undefined) {
throw new Error(_.inspect(obj) + ' does not have own property ' + _.inspect(name));
}
} else {
this.assert(
objHasProperty
, 'expected #{this} to have own property ' + _.inspect(name)
, 'expected #{this} to not have own property ' + _.inspect(name)
);
}

if (value !== undefined) {
this.assert(
actualValue === value
, 'expected #{this} to have own property ' + _.inspect(name) + ' of #{exp}, but got #{act}'
, 'expected #{this} to not have own property ' + _.inspect(name) + ' of #{act}'
, value
, actualValue
);
}

flag(this, 'object', actualValue);
}

Assertion.addMethod('ownProperty', assertOwnProperty);
Expand Down Expand Up @@ -1774,6 +1858,7 @@ module.exports = function (chai, _) {
, failNegateMsg
, subset
, obj
, true
);
});

Expand Down
Loading

0 comments on commit 8166f33

Please sign in to comment.