@@ -180,6 +180,27 @@ func (m assignableToTypeOfMatcher) String() string {
180180 return "is assignable to " + m .targetType .Name ()
181181}
182182
183+ type anyOfMatcher struct {
184+ matchers []Matcher
185+ }
186+
187+ func (am anyOfMatcher ) Matches (x any ) bool {
188+ for _ , m := range am .matchers {
189+ if m .Matches (x ) {
190+ return true
191+ }
192+ }
193+ return false
194+ }
195+
196+ func (am anyOfMatcher ) String () string {
197+ ss := make ([]string , 0 , len (am .matchers ))
198+ for _ , matcher := range am .matchers {
199+ ss = append (ss , matcher .String ())
200+ }
201+ return strings .Join (ss , " | " )
202+ }
203+
183204type allMatcher struct {
184205 matchers []Matcher
185206}
@@ -302,6 +323,28 @@ func Any() Matcher { return anyMatcher{} }
302323// Cond(func(x any){return x.(int) == 2}).Matches(1) // returns false
303324func Cond (fn func (x any ) bool ) Matcher { return condMatcher {fn } }
304325
326+ // AnyOf returns a composite Matcher that returns true if at least one of the
327+ // matchers returns true.
328+ //
329+ // Example usage:
330+ //
331+ // AnyOf(1, 2, 3).Matches(2) // returns true
332+ // AnyOf(1, 2, 3).Matches(10) // returns false
333+ // AnyOf(Nil(), Len(2)).Matches(nil) // returns true
334+ // AnyOf(Nil(), Len(2)).Matches("hi") // returns true
335+ // AnyOf(Nil(), Len(2)).Matches("hello") // returns false
336+ func AnyOf (xs ... any ) Matcher {
337+ ms := make ([]Matcher , 0 , len (xs ))
338+ for _ , x := range xs {
339+ if m , ok := x .(Matcher ); ok {
340+ ms = append (ms , m )
341+ } else {
342+ ms = append (ms , Eq (x ))
343+ }
344+ }
345+ return anyOfMatcher {ms }
346+ }
347+
305348// Eq returns a matcher that matches on equality.
306349//
307350// Example usage:
0 commit comments