Quick / Nimble

A Matcher Framework for Swift and Objective-C
https://quick.github.io/Nimble/documentation/nimble/
Apache License 2.0
4.79k stars 595 forks source link

Add a `map` matcher. #1112

Closed younata closed 6 months ago

younata commented 6 months ago

map allows you to transform the expression to another value, and then run that new value against another matcher.

I find map to be most useful when combined with satisfyAllOf, as a kind of "fuzzy equals" matcher. Or, when you only care about some parts of a value matching but not other.

In fact, without composing map with satisfyAllOf, I don't really see the point of map at all. After all expect(foo.property).to(equal(1)) is much more readable and succinct than expect(foo).to(map(\.property, equal(1)))).

I also added some guidance for when to use map. In my view, map sits in a weird case where you want to check multiple multiple properties of a value at the same time (perhaps as part of a toEventually-style matcher), but don't want the value to conform to Equatable, nor do you check that value enough to write a custom matcher for it.

Some trivia: Originally, this matcher was named lens, from the functional programming concept. However, I changed it to map which is both closer to how it's actually used/written, and much more idiomatic to swift.

Checklist - While not every PR needs it, new features should consider this list: