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

Add support for deep property checking #9

Closed
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 52 additions & 10 deletions Assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ class Assert {
have: true,
own: true,
exactly: true,
deep: true,

'a,an': {
evaluate (actual, expected) {
Expand Down Expand Up @@ -362,34 +363,75 @@ class Assert {

property: {
evaluate (object, property, value) {
let only = this._modifiers.only;
//TODO deep properties "foo.bar.baz"
const { deep, only, own } = this._modifiers;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could be a good deal simpler / perfier in this form:

    let obj = object;
    let prop = property;

    if (deep) {
        let parts = property.split(/.../);
        prop = parts.pop();

        for (let part of parts) {
            if (!obj) {
                return false;
            }
            obj = obj[part]; // ish
        }
    }

    // pretty much as before using obj / prop instead of object / property

?


let obj = object;
let prop = property;

if (deep) {
const parts = property
/**
* Splits the property for dot and bracket notations:
*
* - foo.bar
* - foo[1]
* - foo[0].bar
* - foo[0][1]
* - foo[0][3].bar[1].baz
* - foo['bar'][1].baz
* - foo.bar[0]["baz"]
*
* For brackets, it will split on the bracket so the
* part is whatever is between the brackets (number for
* array, even text for object).
*/
.split(/\['?"?|'?"?\]\.?\[?'?"?|\./)
/**
* Removes any empty strings that can occur
* with bracket notation.
*/
.filter(item => item !== '');

prop = parts.pop();

for (let part of parts) {
if (!obj) {
return false;
}

obj = obj[part]; // ish
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just realized we should probably check only here as well.. So that

    expect({ a: { b: 2}, c: true }).to.only.have.deep.property('a.b', 2);

Is reported as a failure.

Agree?

}
}

if (!obj) {
return false;
}

if (this._modifiers.own) {
if (!object.hasOwnProperty(property)) {
if (own) {
if (!obj.hasOwnProperty(prop)) {
return false;
}
if (only) {
for (let s of Object.keys(object)) {
if (s !== property) {
for (let s of Object.keys(obj)) {
if (s !== prop) {
return false;
}
}
}
}
else if (!(property in object)) {
else if (typeof obj === 'object' && !(prop in obj)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

if this check is to prevent in from throwing... won't we have the same issue in the for loop below? Would this handle both?

    if (typeof obj !== 'object' || !(prop in obj)) {

return false;
}
else if (only) {
for (let s in object) {
if (s !== property) {
for (let s in obj) {
if (s !== prop) {
return false;
}
}
}

if (value !== undefined) {
return object[property] === value;
return obj[prop] === value;
}

return true;
Expand Down
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,30 @@ all of the enumerable properties.

Serves only to aid readability.

### deep

Used with `property` to allow deeply checking properties. This allows `property` to
accept a string containing dot and bracket notation for object/array traversal:

const test = {
foo : {
bar : [
1,
[
{
baz : 2
}
]
]
}
};

expect(test).to.have.deep.property(`foo.bar[0]`, 1); //success
expect(test).to.have.deep.property(`foo.bar[1][0].baz`, 2); //success

expect(test).to.have.deep.property(`foo.bar[2]`); //failure
expect(test).to.have.deep.property(`foo.bar[1][0].baz.foobar`, 6); //failure

## Methods

Asserts can also provide methods. These methods look syntactically like assertions
Expand Down
Loading