Skip to content

Commit

Permalink
Merge pull request #402 from umireon/escaping-dot-should-be-taken
Browse files Browse the repository at this point in the history
Escaping dot should be taken in deep property
  • Loading branch information
keithamus committed Mar 20, 2015
2 parents 7ca2a3b + 1aae1b0 commit 32e890e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
16 changes: 16 additions & 0 deletions lib/chai/core/assertions.js
Expand Up @@ -75,6 +75,10 @@ module.exports = function (chai, _) {
* expect({ foo: { bar: { baz: 'quux' } } })
* .to.have.deep.property('foo.bar.baz', 'quux');
*
* In the `property` assertion, setting `deep` flag may require
* to escape dot and brackets, while that is a rare case.
* See also the documentation of `.deep.property`.
*
* @name deep
* @api public
*/
Expand Down Expand Up @@ -793,6 +797,18 @@ module.exports = function (chai, _) {
* .with.deep.property('[2]')
* .that.deep.equals({ tea: 'konacha' });
*
* Note that dots and bracket in `name` must be backslash-escaped when
* the `deep` flag is set, while they must NOT be escaped when the `deep`
* flag is not set.
*
* // simple referencing
* var css = { '.link[target]': 42 };
* expect(css).to.have.property('.link[target]', 42);
*
* // deep referencing
* var deepCss = { '.link': { '[target]': 42 }};
* expect(deepCss).to.have.deep.property('\\.link.\\[target\\]', 42);
*
* @name property
* @alias deep.property
* @param {String} name
Expand Down
7 changes: 4 additions & 3 deletions lib/chai/utils/getPathInfo.js
Expand Up @@ -54,20 +54,21 @@ module.exports = function getPathInfo(path, obj) {
*
* * Can be as near infinitely deep and nested
* * Arrays are also valid using the formal `myobject.document[3].property`.
* * Literal dots and brackets (not delimiter) must be backslash-escaped.
*
* @param {String} path
* @returns {Object} parsed
* @api private
*/

function parsePath (path) {
var str = path.replace(/\[/g, '.[')
var str = path.replace(/([^\\])\[/g, '$1.[')
, parts = str.match(/(\\\.|[^.]+?)+/g);
return parts.map(function (value) {
var re = /\[(\d+)\]$/
var re = /^\[(\d+)\]$/
, mArr = re.exec(value);
if (mArr) return { i: parseFloat(mArr[1]) };
else return { p: value };
else return { p: value.replace(/\\([.\[\]])/g, '$1') };
});
}

Expand Down
6 changes: 6 additions & 0 deletions test/expect.js
Expand Up @@ -411,6 +411,9 @@ describe('expect', function () {
expect(obj).to.have.property('foo');
expect(obj).to.have.property('bar');

expect({ 'foo.bar[]': 'baz'})
.to.have.property('foo.bar[]');

err(function(){
expect('asd').to.have.property('foo');
}, "expected 'asd' to have a property 'foo'");
Expand All @@ -429,6 +432,9 @@ describe('expect', function () {
expect({ 'foo': [1, 2, 3] })
.to.have.deep.property('foo[1]');

expect({ 'foo.bar[]': 'baz'})
.to.have.deep.property('foo\\.bar\\[\\]');

err(function(){
expect({ 'foo.bar': 'baz' })
.to.have.deep.property('foo.bar');
Expand Down
12 changes: 12 additions & 0 deletions test/utilities.js
Expand Up @@ -90,6 +90,9 @@ describe('utilities', function () {
dimensions: {
units: 'mm',
lengths: [[1.2, 3.5], [2.2, 1.5], [5, 7]]
},
'dimensions.lengths': {
'[2]': [1.2, 3.5]
}
};

Expand Down Expand Up @@ -152,6 +155,15 @@ describe('utilities', function () {
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() {
Expand Down

0 comments on commit 32e890e

Please sign in to comment.