Skip to content

Commit

Permalink
property assertion should only accept strings if nested, fixes #1043 (#…
Browse files Browse the repository at this point in the history
…1044)

* property assertion should only accept strings if nested, fixes #1043

* similar logic seperated out

* test cases for fix #1043

* type check if not isNested with tests

* Tests for assert and should inteface

* Error message change

* changes in should and assert tests as per review

* assert tests modified

* Review comments changes

* review comments fixes
  • Loading branch information
solodynamo authored and vieiralucas committed Oct 2, 2017
1 parent 19e2c18 commit 3ace4a0
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
25 changes: 22 additions & 3 deletions lib/chai/core/assertions.js
Expand Up @@ -1762,10 +1762,30 @@ module.exports = function (chai, _) {
, isOwn = flag(this, 'own')
, flagMsg = flag(this, 'message')
, obj = flag(this, 'object')
, ssfi = flag(this, 'ssfi');
, ssfi = flag(this, 'ssfi')
, nameType = typeof name;

flagMsg = flagMsg ? flagMsg + ': ' : '';

if (isNested) {
if (nameType !== 'string') {
throw new AssertionError(
flagMsg + 'the argument to property must be a string when using nested syntax',
undefined,
ssfi
);
}
} else {
if (nameType !== 'string' && nameType !== 'number' && nameType !== 'symbol') {
throw new AssertionError(
flagMsg + 'the argument to property must be a string, number, or symbol',
undefined,
ssfi
);
}
}

if (isNested && isOwn) {
flagMsg = flagMsg ? flagMsg + ': ' : '';
throw new AssertionError(
flagMsg + 'The "nested" and "own" flags cannot be combined.',
undefined,
Expand All @@ -1774,7 +1794,6 @@ module.exports = function (chai, _) {
}

if (obj === null || obj === undefined) {
flagMsg = flagMsg ? flagMsg + ': ' : '';
throw new AssertionError(
flagMsg + 'Target cannot be null or undefined.',
undefined,
Expand Down
13 changes: 13 additions & 0 deletions test/assert.js
Expand Up @@ -1393,6 +1393,7 @@ describe('assert', function () {
var obj = { foo: { bar: 'baz' } };
var simpleObj = { foo: 'bar' };
var undefinedKeyObj = { foo: undefined };
var dummyObj = { a: '1' };
assert.property(obj, 'foo');
assert.property(obj, 'toString');
assert.propertyVal(obj, 'toString', Object.prototype.toString);
Expand Down Expand Up @@ -1452,6 +1453,18 @@ describe('assert', function () {
err(function () {
assert.property(undefined, 'a', 'blah');
}, "blah: Target cannot be null or undefined.");

err(function () {
assert.property({a:1}, {'a':'1'}, 'blah');
}, 'blah: the argument to property must be a string, number, or symbol');

err(function () {
assert.propertyVal(dummyObj, 'a', '2', 'blah');
}, "blah: expected { a: '1' } to have property 'a' of '2', but got '1'");

err(function () {
assert.nestedProperty({a:1}, {'a':'1'}, 'blah');
}, 'blah: the argument to property must be a string when using nested syntax');
});

it('deepPropertyVal', function () {
Expand Down
8 changes: 8 additions & 0 deletions test/expect.js
Expand Up @@ -1462,6 +1462,10 @@ describe('expect', function () {
err(function () {
expect(undefined, 'blah').to.have.property("a");
}, "blah: Target cannot be null or undefined.");

err(function () {
expect({a:1}, 'blah').to.have.property(null)
}, "blah: the argument to property must be a string, number, or symbol");
});

it('property(name, val)', function(){
Expand Down Expand Up @@ -1755,6 +1759,10 @@ describe('expect', function () {
expect({ 'foo.bar': 'baz' })
.to.have.nested.property('foo.bar');
}, "expected { 'foo.bar': 'baz' } to have nested property 'foo.bar'");

err(function () {
expect({a:1}, 'blah').to.have.nested.property({'a':'1'});
}, "blah: the argument to property must be a string when using nested syntax");
});

it('nested.property(name, val)', function(){
Expand Down
10 changes: 10 additions & 0 deletions test/should.js
Expand Up @@ -1182,6 +1182,10 @@ describe('should', function() {
err(function() {
({a: {b: 1}}).should.have.own.nested.property("a.b");
}, "The \"nested\" and \"own\" flags cannot be combined.");

err(function () {
({a:1}).should.have.property(undefined);
}, "the argument to property must be a string, number, or symbol");
});

it('property(name, val)', function(){
Expand Down Expand Up @@ -1407,6 +1411,12 @@ describe('should', function() {

({ 'foo.bar[]': 'baz'}).should.have.nested.property('foo\\.bar\\[\\]');

({a:1}).should.have.nested.property('a');

err(function(){
({a:1}).should.have.nested.property({'a':'1'});
}, "the argument to property must be a string when using nested syntax");

err(function(){
({ 'foo.bar': 'baz' }).should.have.nested.property('foo.bar');
}, "expected { 'foo.bar': 'baz' } to have nested property 'foo.bar'");
Expand Down

0 comments on commit 3ace4a0

Please sign in to comment.