Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escaping dot should be taken in deep property #402

Merged
merged 4 commits into from Mar 20, 2015
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/chai/core/assertions.js
Expand Up @@ -793,6 +793,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.[')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without (([^\\]), $1), a\\[results in accessing a.[.

, parts = str.match(/(\\\.|[^.]+?)+/g);
return parts.map(function (value) {
var re = /\[(\d+)\]$/
var re = /^\[(\d+)\]$/
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This ^ ensures test\\[1] parsed as the property test[1], not the index 1

, 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