From 57ef36621f5e9b9718a5a0cb23834420a1912e0a Mon Sep 17 00:00:00 2001 From: eldritch fossicker Date: Mon, 2 Mar 2015 14:00:46 -0600 Subject: [PATCH 1/2] fix indexing into array with deep propery * added unit test to expect with code from documentation * added default to res in _getPathValue --- lib/chai/utils/getPathInfo.js | 2 +- test/expect.js | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/chai/utils/getPathInfo.js b/lib/chai/utils/getPathInfo.js index 538b2a1c9..dac2194ad 100644 --- a/lib/chai/utils/getPathInfo.js +++ b/lib/chai/utils/getPathInfo.js @@ -89,7 +89,7 @@ function parsePath (path) { function _getPathValue (parsed, obj, index) { var tmp = obj - , res; + , res = obj; index = (index === undefined ? parsed.length : index); diff --git a/test/expect.js b/test/expect.js index 9db12dee0..cd204a9b0 100644 --- a/test/expect.js +++ b/test/expect.js @@ -446,6 +446,12 @@ describe('expect', function () { expect(deepObj).to.have.deep.property('green.tea', 'matcha'); expect(deepObj).to.have.deep.property('teas[1]', 'matcha'); expect(deepObj).to.have.deep.property('teas[2].tea', 'konacha'); + + expect(deepObj).to.have.property('teas') + .that.is.an('array') + .with.deep.property('[2]') + .that.deep.equals({tea: 'konacha'}); + err(function(){ expect(deepObj).to.have.deep.property('teas[3]'); }, "expected { Object (green, teas) } to have a deep property 'teas[3]'"); From bb8d0bf7a4290cefe8b88485689b3ff72b468a09 Mon Sep 17 00:00:00 2001 From: eldritch fossicker Date: Tue, 3 Mar 2015 14:28:44 -0600 Subject: [PATCH 2/2] updates to reflect code style preference from @keithamus --- lib/chai/utils/getPathInfo.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/chai/utils/getPathInfo.js b/lib/chai/utils/getPathInfo.js index dac2194ad..67aa66198 100644 --- a/lib/chai/utils/getPathInfo.js +++ b/lib/chai/utils/getPathInfo.js @@ -32,7 +32,7 @@ module.exports = function getPathInfo(path, obj) { last = parsed[parsed.length - 1]; var info = { - parent: _getPathValue(parsed, obj, parsed.length - 1), + parent: parsed.length > 1 ? _getPathValue(parsed, obj, parsed.length - 1) : obj, name: last.p || last.i, value: _getPathValue(parsed, obj), }; @@ -89,7 +89,7 @@ function parsePath (path) { function _getPathValue (parsed, obj, index) { var tmp = obj - , res = obj; + , res; index = (index === undefined ? parsed.length : index);