Someone please name this matcher for me

I’ve had a matcher in my head for a couple months, that I frequently
want but never get around to writing because I can’t think of the name
for it. Here’s how it would look

[1, 2, 3, 4, 1].should … [1, 3, 1, 4, 2]

I have a couple ideas for names, but I’ll hold off on them so I don’t
influence anyone’s opinion.

btw, I realize that .sort works in this case. But that’s slightly
less clear than I’d like, and it doesn’t work for non-Comparable
objects (like activerecords).

Pat

perhaps verbose but how about

foo.should equal_in_any_order [1, 3, 1, 4, 2]

On Wed, Aug 6, 2008 at 1:50 PM, Pat M. [email protected] wrote:

less clear than I’d like, and it doesn’t work for non-Comparable
objects (like activerecords).

[1, 2, 3, 4, 1].should consist_of([1, 3, 1, 4, 2])

Since consist means “to be composed or made up of”, which matches the
usage.

To me it reads like, this thing should be made up of these other things.

WDYT?


Zach D.
http://www.continuousthinking.com

On Wed, Aug 6, 2008 at 12:50 PM, Pat M. [email protected] wrote:

I’ve had a matcher in my head for a couple months, that I frequently
want but never get around to writing because I can’t think of the name
for it. Here’s how it would look

[1, 2, 3, 4, 1].should … [1, 3, 1, 4, 2]

[1, 2, 3, 4, 1].should include_all [1, 3, 1, 4, 2]

Consider:
should_have_the_same_members_as

How about
[1, 2, 3, 4, 1].should contain([1, 3, 1, 4, 2])

[1, 2, 3, 4, 1].should contain_only([1, 3, 1, 4, 2])

or (riffing off Zach)

[1, 2, 3, 4, 1].should be_composed_from([1, 3, 1, 4, 2])

Matt

.should be_bag_of( [1, 3, 1, 4, 2] )

On Wed, 6 Aug 2008 13:50:27 -0400, you wrote:

I’ve had a matcher in my head for a couple months, that I frequently
want but never get around to writing because I can’t think of the name
for it. Here’s how it would look

[1, 2, 3, 4, 1].should … [1, 3, 1, 4, 2]

An unordered collection with possibly multiple occurrences of its
members is known in mathematics as a “multiset,” or, informally, a
“bag.”

-Steve

2008-08-06 13:11, Mike V.:

[1, 2, 3, 4, 1].should … [1, 3, 1, 4, 2]
[1, 2, 3, 4, 1].should include_all [1, 3, 1, 4, 2]

I’d then think that

[1, 1, 1, 2, 3, 4].should include_all [1, 3, 1, 4, 2]
[1, 2, 3, 4, 5, 6].should include_all [1, 3, 1, 4, 2]

Which afaik was not what Pat had in mind. Or was it?

And include_all_and_nothing_but is a bit verbose. :slight_smile:

2008-08-06 14:33, Steve Schafer:

An unordered collection with possibly multiple occurrences of its
members is known in mathematics as a “multiset,” or, informally, a
“bag.”

I (standing knee-deep in mathematics) was about to offer the following

be_multiset_equal_to
be_set_equal_to

Set equality is slightly (imo not fatally) incorrect, and multiset
might be alien to majority of hackers out there. I’d prefer either of
these or modifications.

On 07/08/2008, at 05:04 , Jonathan L. wrote:

.should be_bag_of( [1, 3, 1, 4, 2] )

FWIW, I am in favour of this naming. Now back to lurking :slight_smile:

On Wed, Aug 6, 2008 at 1:25 PM, Cohen, Joseph [email protected] wrote:

Consider:
should_have_the_same_members_as

Similarly, I have a have_same_elements_as(arr) matcher.

k

Lots of good, interesting suggestions.
If you don’t mind its length, for the non-mathematically inclined
something like

has_same_members_as

may be easier to remember and corresponds to Ruby’s member? predicate.

i wrote this matcher myself for a project i’m working on and have been
calling it `equals_without_regard_to_order’. it’s an unwieldy name
and i’ve never really been comfortable with it but i’ve continued to
use it, mostly because i’m dim.

very curious to find a winning replacement on this list!


Dhruv Bansal
http://www.infochimps.org || http://www.dhruvbansal.com ||
[email protected]

“Mark W.” [email protected] writes:

Zach D. wrote:

[1, 2, 3, 4, 1].should consist_of([1, 3, 1, 4, 2])

+1

Would such a matcher also work in this instance:

Car.should_receive(:find).with(bag_of[@ford.id, @fiat.id])

Such that if I don’t know (or care) what order the IDs will be passed to
the find method, this will pass?

Or is there already some other way to achieve that?

is_permutation_of would be technically correct, but it doesn’t really
express equality to me, which is the intent of the match.

///ark

On 07/08/2008, at 05:04 , Jonathan L. wrote:

.should be_bag_of( [1, 3, 1, 4, 2] )

+1