feat: Support iter.Seq
s in [Not]Contains
and [Not]ElementsMatch
#1685
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds support
iter.Seq[...]
sequences (introduced in Go 1.23, or earlier viarangefunc
experiment) in theContains
andElementsMatch
(+ derived) asserters.Changes
seqToSlice
helper that transparently converts aniter.Seq
(which is a function of signaturefunc(func(T) bool)
) to a corresponding slice of type[]T
, if support for sequences is enabled. Otherwise, it does nothing. Note: this function is reflection-based and does not introduce any source dependencies on newer language features, thereby respecting the Go version constraint in thego.mod
file.seqToSlice
helper in the above-mentioned asserters to contain any inputs from sequence to slice form (this has to happen early on because of the empty check)Motivation
Sequences are a new language feature that as of go1.23 has become standard for obtaining sequences of values w/o requiring to materialize the values into a slice (e.g.,
maps.Keys(...)
,maps.Values(...)
). Currently, using these sequences in matchers requires materialization viaslices.Collect
. However, sinceElementsMatch
andContains
are conceptually applicable to sequences in addition to slices, arrays etc., this should be handled by the testify framework.Example usage
requires go1.23