uber-go / mock

GoMock is a mocking framework for the Go programming language.
Apache License 2.0
1.85k stars 106 forks source link

A matcher that matches at least one of the given matchers #58

Closed favonia closed 10 months ago

favonia commented 11 months ago

Requested feature

A new matcher combinator like All but returns true when one of the matchers returns true.

Why the feature is needed

We currently have All that matches all the matchers, just like the universal quantifier in the logic. It would be great to have AnyOf that matches at least one of them, similar to the existential quantifier in the logic. This is helpful when an argument can be one of several values. For example, with the new AnyOf matcher, we may write the following:

gomock.InOrder(
    m.EXPECT().SetDirection(AnyOf("north", "south", "west", "east"), true),
    m.EXPECT().Run(),
)

This means we expect a method call SetDirection("north", true), SetDirection("south", true), SetDirection("west", true) or SetDirection("east", true), which is then followed by a method call Run().

(Optional) Proposed solution

Here is a sample implementation. It's trivial for me to make a PR if the maintainers feel positive about this feature.

type anyOfMatcher struct {
    matchers []Matcher
}

func (am anyOfMatcher) Matches(x any) bool {
    for _, m := range am.matchers {
        if m.Matches(x) {
            return true
        }
    }
    return false
}

func (am anyOfMatcher) String() string {
    ss := make([]string, 0, len(am.matchers))
    for _, matcher := range am.matchers {
        ss = append(ss, matcher.String())
    }
    return strings.Join(ss, " | ")
}

func AnyOf(xs ...any) Matcher {
    ms := make([]Matcher, 0, len(xs))
    for _, x := range xs {
        if m, ok := x.(Matcher); ok {
            ms = append(ms, m)
        } else {
            ms = append(ms, Eq(x))
        }
    }
    return anyOfMatcher{ms}
}

Related issues and PRs (from the original gomock)

JacobOaks commented 11 months ago

Hey @favonia, I think this makes sense as a nice addition to the library. I think AnyOf would be a better name than Some though, since Some leaves ambiguous how many would need to match.

favonia commented 11 months ago

@JacobOaks Done. Any other suggestions before I make a PR?

JacobOaks commented 11 months ago

@favonia awesome, nope - go ahead and thanks!