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

MemberEquals assertion #148

Closed
NickHeiner opened this issue Apr 9, 2013 · 7 comments
Closed

MemberEquals assertion #148

NickHeiner opened this issue Apr 9, 2013 · 7 comments

Comments

@NickHeiner
Copy link

It would be nice to assert that two arrays have the same members, regardless of order. (This would be used when we are treating arrays as sets, which aren't widely available in js yet.)

var expect = require('chai').expect;

it('should view two arrays as equal', function() {
    expect([1, 2, 3]).to.have.same.members.as([3, 2, 1]);

    // alternate proposed syntax
    expect([1, 2, 3]).to.be.memberEqual([3, 2, 1]);
});

Can I submit a PR for this?

@domenic
Copy link
Contributor

domenic commented Apr 9, 2013

+1, want want want.

@logicalparadox
Copy link
Member

+1

How about this for language:

// needle and haystack must have all of the same members
expect([ 1, 2, 3 ]).to.have.members([ 3, 2, 1 ]);

// needle can be subset of haystack
expect([ 1, 2, 3 ]).to.include.members([ 3, 1 ]);

Thoughts?

@domenic
Copy link
Contributor

domenic commented Apr 9, 2013

@logicalparadox seems like that would be subset, not set equality.

@logicalparadox
Copy link
Member

@domenic The second example is subset, the first would behave the same as @NickHeiner's example.

@NickHeiner
Copy link
Author

to.have.members implies subset instead of member equality to me, but I agree that subset would be nice as well.

@logicalparadox
Copy link
Member

have is a noop chain so it can be used however you wish. In the documentation have is used sometimes to demonstrate subset (expect('foobar').to.have.string('foo')). However, there is one scenario which is particularly relevant where it does not and that is for the keys assertion:

expect({ foo: 'bar', baz: 'zing' }).to.have.keys('foo', 'baz');
expect({ foo: 'bar', baz: 'zing' }).to.contain.key('foo');

Given this proposal is essentially the same thing but for arrays not objects it would be appropriate if the semantics matched... and this must be done without breaking .keys.

Since these are all noops it is more of a matter of preference (and documentation) than anything else. Perhaps the best way to accomplish this is by adding another chainable that would allow you to be more expressive if you wish: exact.

expect([ 1, 2, 3 ]).to.have.exact.members(3, 2, 1); // or [ 3, 2, 1 ]
expect({ foo: 'bar', baz: 'zing' }).to.have.exact.keys('foo', 'baz'); // or [ 'foo', 'baz' ]

NickHeiner pushed a commit to NickHeiner/chai that referenced this issue Apr 14, 2013
NickHeiner pushed a commit to NickHeiner/chai that referenced this issue Apr 14, 2013
NickHeiner pushed a commit to NickHeiner/chai that referenced this issue Apr 14, 2013
logicalparadox added a commit that referenced this issue Apr 29, 2013
* 'master' of github.com:chaijs/chai:
  giving members a no-flag assertion
  Code review comments - changing syntax
  Code review comments
  Adding members and memberEquals assertions for checking for subsets and set equality. Implements #148.
  Restore the `call` and `apply` methods of Function when adding a chainable method.
@logicalparadox
Copy link
Member

Merged into master...

kinland pushed a commit to kinland/chai-have-all-properties that referenced this issue Mar 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants