-
-
Notifications
You must be signed in to change notification settings - Fork 702
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
property assertion should only accept strings if nested, fixes #1043 #1044
Changes from 5 commits
e167009
3f2ce29
335fd33
5fd4fa2
8ff7cb7
a078a6d
e1442d0
d58d2bf
84062b1
f9e5a68
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
describe('assert', function () { | ||
var assert = chai.assert; | ||
var expect = chai.expect; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's not add |
||
|
||
it('assert', function () { | ||
var foo = 'bar'; | ||
|
@@ -1395,6 +1396,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); | ||
|
@@ -1454,6 +1456,18 @@ describe('assert', function () { | |
err(function () { | ||
assert.property(undefined, 'a', 'blah'); | ||
}, "blah: Target cannot be null or undefined."); | ||
|
||
err(function () { | ||
assert.propertyVal(dummyObj, 'a', '2', 'blah'); | ||
}, "blah: expected { a: '1' } to have property 'a' of '2', but got '1'"); | ||
|
||
expect(function () { | ||
assert.nestedProperty({a:1}, '{a:1}'); | ||
}).to.not.throw('the argument to `property` must be a string'); | ||
|
||
expect(function () { | ||
assert.nestedProperty({a:1}, {'a':'1'}); | ||
}).to.throw('the argument to `property` must be a string'); | ||
}); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we are using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's shouldn't be wrong to use But I agree with @meeber that we must try and be able to use own You can replace the ones you added on this PR with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure @vieiralucas . I have replaced them and also surely will open a seperate PR in future to replace the remaining. |
||
it('deepPropertyVal', function () { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1462,6 +1462,10 @@ describe('expect', function () { | |
err(function () { | ||
expect(undefined, 'blah').to.have.property("a"); | ||
}, "blah: Target cannot be null or undefined."); | ||
|
||
expect(function () { | ||
expect({a:1}).to.have.property(null); | ||
}).to.throw('the argument to `property` must be either of type string, number or symbol'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's repeat these three new error tests in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay , so just duplicating them in both the files. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @keithamus @meeber There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @solodynamo I'm not sure what you mean. On a separate note, I just noticed a couple of things:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @meeber I meant to ask |
||
}); | ||
|
||
it('property(name, val)', function(){ | ||
|
@@ -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'"); | ||
|
||
expect(function () { | ||
expect({a:1}).to.have.nested.property({'a':'1'}); | ||
}).to.throw('the argument to `property` must be a string'); | ||
}); | ||
|
||
it('nested.property(name, val)', function(){ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
describe('should', function() { | ||
var should = chai.Should(); | ||
var expect = chai.expect; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same thing here. Let's not add |
||
|
||
it('assertion', function(){ | ||
'test'.should.be.a('string'); | ||
|
@@ -1407,6 +1408,16 @@ describe('should', function() { | |
|
||
({ 'foo.bar[]': 'baz'}).should.have.nested.property('foo\\.bar\\[\\]'); | ||
|
||
({a:1}).should.have.nested.property('a'); | ||
|
||
expect(function () { | ||
({a:1}).should.have.nested.property('{a:1}'); | ||
}).to.not.throw('the argument to `property` must be a string'); | ||
|
||
expect(function () { | ||
({a:1}).should.have.nested.property({'a':'1'}); | ||
}).to.throw('the argument to `property` must be a string'); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One more test should be added here to the |
||
err(function(){ | ||
({ 'foo.bar': 'baz' }).should.have.nested.property('foo.bar'); | ||
}, "expected { 'foo.bar': 'baz' } to have nested property 'foo.bar'"); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about telling that it must be string because the nested flag is on?
Something like:
I'm afraid that it will be confusing to sometimes say that it can be a
string
,number
orsymbol
and sometimes just saying that it must be astring
Thoughts? @keithamus @solodynamo @meeber
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But rather than writing
nested flag
we should think something else as end user may not know about internal logic ofisNested
. Suggestions ? @vieiralucas @meeber @keithamusThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, we can safely assume to get here they've written
.nested.propery()
so we can use that instead.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A user could reach this error via the
assert
interface too, though, sonested.property
wouldn't always be entirely accurate.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm having a hard time thinking on a good message
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about:
Normal: the argument to
property
must be astring
,number
, orsymbol
Nested: the argument to
property
must be astring
when usingnested
syntax