specta / expecta

A Matcher Framework for Objective-C/Cocoa
MIT License
1.59k stars 158 forks source link

Add equalsInAnyOrder Matcher for NSArray #156

Closed jmburges closed 9 years ago

jmburges commented 9 years ago

There are a bunch of Gists and blogs on this, but I find myself doing beSupersetOf and hasCountOf together all the time to get the same effect. Figured I would submit this back in.

Converts the NSArrays to NSSets and compares them so that order doesn't matter.

First PR to Expecta so feedback welcome!!

orta commented 9 years ago

I like this idea, but I feel like the current implementation is lossy:

expect(@[@1,@1,@2]).to.equalInAnyOrder(@[@1,@2]);

Would end up passing here, but they are not the same arrays, in a different order. Perhaps the implementation could order them both by description, then loop through one array and check if the other arrays item is the same?

orta commented 9 years ago

Also, CHANGELOG please :)

robb commented 9 years ago

Perhaps the implementation could order them both by description, then loop through one array and check if the other arrays item is the same?

NSCountedSet should do the trick here

robb commented 9 years ago

Thanks for the pull request! :sparkling_heart:

However, I'm not sure I think this is worth adding to expecta. It seems that if you frequently need to compare two arrays independent of order, using an NSArray is the actual problem.

jmburges commented 9 years ago

@robb Thanks for doing so much else! :sparkles:

I end up using this a ton for times where I want to ensure the same exact keys in an NSDictionary and allKeys returns an NSArray.

I 100% see your point, if you don't care about ordering stop using an NSArray. Up to you really :) If we don't include it in expecta, I'll just make a cocoapod.

orta commented 9 years ago

Yeah, I think I'm onboard with the having as an extension, it could fit quite well in Expecta-Comparison or it could be it's own independent thing.