Skip to content

Commit

Permalink
slices: make slice equality comparator more flexible (#100)
Browse files Browse the repository at this point in the history
This PR enables SliceContainsFunc to accept a different type as the
target from the slice elements type. For example if we have a slice
of Person objects but want to detect if the slice contains a person
with a particular name (string), we can now use a comparator of
signature func(Person, string) bool - rather than needing a whole
Person struct for the target.
  • Loading branch information
shoenig authored Jan 21, 2023
1 parent c3fdef6 commit 7d8bca6
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions internal/assertions/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func contains[C comparable](slice []C, item C) bool {
return found
}

func containsFunc[A any](slice []A, item A, eq func(a, b A) bool) bool {
func containsFunc[A, B any](slice []A, item B, eq func(a A, b B) bool) bool {
found := false
for i := 0; i < len(slice); i++ {
if eq(slice[i], item) {
Expand Down Expand Up @@ -354,7 +354,7 @@ func SliceContainsOp[C comparable](slice []C, item C) (s string) {
return
}

func SliceContainsFunc[A any](slice []A, item A, eq func(a, b A) bool) (s string) {
func SliceContainsFunc[A, B any](slice []A, item B, eq func(a A, b B) bool) (s string) {
if !containsFunc(slice, item, eq) {
s = "expected slice to contain missing item via 'eq' function\n"
s += fmt.Sprintf("↪ slice is missing %#v\n", item)
Expand Down
2 changes: 1 addition & 1 deletion must/must.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions must/must_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func SliceContainsOp[C comparable](t T, slice []C, item C, settings ...Setting)
}

// SliceContainsFunc asserts item exists in slice, using eq to compare elements.
func SliceContainsFunc[A any](t T, slice []A, item A, eq func(a, b A) bool, settings ...Setting) {
func SliceContainsFunc[A, B any](t T, slice []A, item B, eq func(a A, b B) bool, settings ...Setting) {
t.Helper()
invoke(t, assertions.SliceContainsFunc(slice, item, eq), settings...)
}
Expand Down
4 changes: 2 additions & 2 deletions test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,8 +579,8 @@ func TestSliceContainsFunc(t *testing.T) {
{ID: 101, Name: "Bob"},
}

SliceContainsFunc(tc, s, &Person{ID: 102, Name: "Carl"}, func(a, b *Person) bool {
return a.ID == b.ID && a.Name == b.Name
SliceContainsFunc(tc, s, "Carl", func(a *Person, name string) bool {
return a.Name == name
})
}

Expand Down

0 comments on commit 7d8bca6

Please sign in to comment.