Skip to content

Commit

Permalink
Use external pathval module instead of internal util modules
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfcosta committed Oct 8, 2016
1 parent 2ef786d commit 4968213
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 318 deletions.
4 changes: 2 additions & 2 deletions lib/chai/core/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1111,7 +1111,7 @@ module.exports = function (chai, _) {
var isDeep = flag(this, 'deep')
, negate = flag(this, 'negate')
, obj = flag(this, 'object')
, pathInfo = isNested ? _.getPathInfo(name, obj) : null
, pathInfo = isNested ? _.getPathInfo(obj, name) : null
, value = isNested ? pathInfo.value : obj[name];

var descriptor = '';
Expand All @@ -1123,7 +1123,7 @@ module.exports = function (chai, _) {
var hasProperty;
if (isOwn) hasProperty = Object.prototype.hasOwnProperty.call(obj, name);
else if (isNested) hasProperty = pathInfo.exists;
else hasProperty = _.hasProperty(name, obj);
else hasProperty = _.hasProperty(obj, name);

// When performing a negated assertion for both name and val, merely having
// a property with the given name isn't enough to cause the assertion to
Expand Down
111 changes: 0 additions & 111 deletions lib/chai/utils/getPathInfo.js

This file was deleted.

43 changes: 0 additions & 43 deletions lib/chai/utils/getPathValue.js

This file was deleted.

16 changes: 8 additions & 8 deletions lib/chai/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
* MIT Licensed
*/

/*!
* Dependencies that are used for multiple exports are required here only once
*/

var pathval = require('pathval');

/*!
* test utility
*/
Expand Down Expand Up @@ -63,23 +69,17 @@ exports.transferFlags = require('./transferFlags');

exports.eql = require('deep-eql');

/*!
* Deep path value
*/

exports.getPathValue = require('./getPathValue');

/*!
* Deep path info
*/

exports.getPathInfo = require('./getPathInfo');
exports.getPathInfo = pathval.getPathInfo;

/*!
* Check if a property exists
*/

exports.hasProperty = require('./hasProperty');
exports.hasProperty = pathval.hasProperty;

/*!
* Function name
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"assertion-error": "^1.0.1",
"check-error": "^1.0.1",
"deep-eql": "^0.1.3",
"pathval": "^1.0.0",
"type-detect": "^2.0.1"
},
"devDependencies": {
Expand Down
154 changes: 0 additions & 154 deletions test/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,160 +69,6 @@ describe('utilities', function () {
});
});

it('getPathValue', function () {
var object = {
hello: 'universe'
, universe: {
hello: 'world'
}
, world: [ 'hello', 'universe' ]
, complex: [
{ hello: 'universe' }
, { universe: 'world' }
, [ { hello: 'world' } ]
]
}

var arr = [ [ true ] ];

chai.use(function (_chai, utils) {
var gpv = utils.getPathValue;
expect(gpv('hello', object)).to.equal('universe');
expect(gpv('universe.hello', object)).to.equal('world');
expect(gpv('world[1]', object)).to.equal('universe');
expect(gpv('complex[1].universe', object)).to.equal('world');
expect(gpv('complex[2][0].hello', object)).to.equal('world');
expect(gpv('[0][0]', arr)).to.be.true;
});
});

describe('getPathInfo', function() {
var gpi,
obj = {
id: '10702S300W',
primes: [2, 3, 5, 7, 11],
dimensions: {
units: 'mm',
lengths: [[1.2, 3.5], [2.2, 1.5], [5, 7]]
},
'dimensions.lengths': {
'[2]': [1.2, 3.5]
}
};

beforeEach(function() {
chai.use(function (_chai, utils) {
gpi = utils.getPathInfo;
});
});

it('should handle simple property', function() {
var info = gpi('dimensions.units', obj);

info.parent.should.equal(obj.dimensions);
info.value.should.equal(obj.dimensions.units);
info.name.should.equal('units');
info.exists.should.be.true;
});

it('should handle non-existent property', function() {
var info = gpi('dimensions.size', obj);

info.parent.should.equal(obj.dimensions);
expect(info.value).to.be.undefined;
info.name.should.equal('size');
info.exists.should.be.false;
});

it('should handle array index', function() {
var info = gpi('primes[2]', obj);

info.parent.should.equal(obj.primes);
info.value.should.equal(obj.primes[2]);
info.name.should.equal(2);
info.exists.should.be.true;
});

it('should handle dimensional array', function() {
var info = gpi('dimensions.lengths[2][1]', obj);

info.parent.should.equal(obj.dimensions.lengths[2]);
info.value.should.equal(obj.dimensions.lengths[2][1]);
info.name.should.equal(1);
info.exists.should.be.true;
});

it('should handle out of bounds array index', function() {
var info = gpi('dimensions.lengths[3]', obj);

info.parent.should.equal(obj.dimensions.lengths);
expect(info.value).to.be.undefined;
info.name.should.equal(3);
info.exists.should.be.false;
});

it('should handle out of bounds dimensional array index', function() {
var info = gpi('dimensions.lengths[2][5]', obj);

info.parent.should.equal(obj.dimensions.lengths[2]);
expect(info.value).to.be.undefined;
info.name.should.equal(5);
info.exists.should.be.false;
});

it('should handle backslash-escaping for .[]', function() {
var info = gpi('dimensions\\.lengths.\\[2\\][1]', obj);

info.parent.should.equal(obj['dimensions.lengths']['[2]']);
info.value.should.equal(obj['dimensions.lengths']['[2]'][1]);
info.name.should.equal(1);
info.exists.should.be.true;
});
});

describe('hasProperty', function() {
var hp;
beforeEach(function() {
chai.use(function (_chai, utils) {
hp = utils.hasProperty;
});
});

it('should handle array index', function() {
var arr = [1, 2, 'cheeseburger'];

hp(1, arr).should.be.true;
hp(3, arr).should.be.false;
});

it('should handle literal types', function() {
var s = 'string literal';
hp('length', s).should.be.true;
hp(3, s).should.be.true;
hp(14, s).should.be.false;

hp('foo', 1).should.be.false;
});

it('should handle undefined', function() {
var o = {
foo: 'bar'
};

hp('foo', o).should.be.true;
hp('baz', o).should.be.false;
hp(0, o).should.be.false;
});

it('should handle undefined', function() {
hp('foo', undefined).should.be.false;
});

it('should handle null', function() {
hp('foo', null).should.be.false;
});
});

describe('addMethod', function() {
var assertionConstructor;

Expand Down

0 comments on commit 4968213

Please sign in to comment.