Skip to content

Commit

Permalink
test: added variants of SliceContainsAll
Browse files Browse the repository at this point in the history
  • Loading branch information
brandondyck authored and shoenig committed Nov 4, 2024
1 parent 2bb71ca commit e20ba67
Show file tree
Hide file tree
Showing 7 changed files with 343 additions and 27 deletions.
36 changes: 36 additions & 0 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,42 @@ func ExampleSliceContainsAll() {
// Output:
}

func ExampleSliceContainsAllEqual() {
dave := &employee{first: "dave", id: 8}
armon := &employee{first: "armon", id: 2}
mitchell := &employee{first: "mitchell", id: 1}
SliceContainsAllEqual(t,
[]*employee{dave, armon, mitchell},
[]*employee{mitchell, dave, armon})
// Output:
}

func ExampleSliceContainsAllFunc() {
// comparing slice to element of same type
SliceContainsAllFunc(t,
[]string{"UP", "DoWn", "LefT", "RiGHT"},
[]string{"left", "down", "up", "right"},
func(a, b string) bool {
return strings.EqualFold(a, b)
})

// comparing slice to element of different type
SliceContainsAllFunc(t,
[]string{"2", "4", "6", "8"},
[]int{2, 6, 4, 8},
func(a string, b int) bool {
return a == strconv.Itoa(b)
})
// Output:
}

func ExampleSliceContainsAllOp() {
SliceContainsAllOp(t,
[]int{1, 2, 3, 4, 5},
[]int{5, 4, 3, 2, 1})
// Output:
}

func ExampleSliceContainsEqual() {
dave := &employee{first: "dave", id: 8}
armon := &employee{first: "armon", id: 2}
Expand Down
30 changes: 30 additions & 0 deletions internal/assertions/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,36 @@ func SliceNotContainsFunc[A, B any](slice []A, item B, eq func(a A, b B) bool) (
return
}

func SliceContainsAllOp[C comparable](slice, items []C) (s string) {
if len(slice) != len(items) {
s = "expected slice and items to contain same number of elements\n"
s += bullet("len(slice): %d\n", len(slice))
s += bullet("len(items): %d\n", len(items))
return s
}
return SliceContainsSubsetOp(slice, items)
}

func SliceContainsAllFunc[A, B any](slice []A, items []B, eq func(a A, b B) bool) (s string) {
if len(slice) != len(items) {
s = "expected slice and items to contain same number of elements\n"
s += bullet("len(slice): %d\n", len(slice))
s += bullet("len(items): %d\n", len(items))
return s
}
return SliceContainsSubsetFunc(slice, items, eq)
}

func SliceContainsAllEqual[E interfaces.EqualFunc[E]](slice, items []E) (s string) {
if len(slice) != len(items) {
s = "expected slice and items to contain same number of elements\n"
s += bullet("len(slice): %d\n", len(slice))
s += bullet("len(items): %d\n", len(items))
return s
}
return SliceContainsSubsetEqual(slice, items)
}

func SliceContainsAll[A any](slice, items []A, opts ...cmp.Option) (s string) {
if len(slice) != len(items) {
s = "expected slice and items to contain same number of elements\n"
Expand Down
38 changes: 37 additions & 1 deletion must/examples_test.go

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

28 changes: 26 additions & 2 deletions must/must.go

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

105 changes: 94 additions & 11 deletions must/must_test.go

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

28 changes: 26 additions & 2 deletions test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,33 @@ func SliceNotContainsFunc[A, B any](t T, slice []A, item B, eq func(a A, b B) bo
invoke(t, assertions.SliceNotContainsFunc(slice, item, eq), settings...)
}

// SliceContainsAllOp asserts slice and items contain the same elements, but in
// no particular order, using the == operator. The number of elements
// in slice and items must be the same.
func SliceContainsAllOp[C comparable](t T, slice, items []C, settings ...Setting) {
t.Helper()
invoke(t, assertions.SliceContainsAllOp(slice, items), settings...)
}

// SliceContainsAllFunc asserts slice and items contain the same elements, but in
// no particular order, using eq to compare elements. The number of elements
// in slice and items must be the same.
func SliceContainsAllFunc[A, B any](t T, slice []A, items []B, eq func(a A, b B) bool, settings ...Setting) {
t.Helper()
invoke(t, assertions.SliceContainsAllFunc(slice, items, eq), settings...)
}

// SliceContainsAllEqual asserts slice and items contain the same elements, but in
// no particular order, using Equal to compare elements. The number of elements
// in slice and items must be the same.
func SliceContainsAllEqual[E interfaces.EqualFunc[E]](t T, slice, items []E, settings ...Setting) {
t.Helper()
invoke(t, assertions.SliceContainsAllEqual(slice, items), settings...)
}

// SliceContainsAll asserts slice and items contain the same elements, but in
// no particular order. The number of elements in slice and items must be the
// same.
// no particular order, using cmp.Equal to compare elements. The number of elements
// in slice and items must be the same.
func SliceContainsAll[A any](t T, slice, items []A, settings ...Setting) {
t.Helper()
invoke(t, assertions.SliceContainsAll(slice, items, options(settings...)...), settings...)
Expand Down
Loading

0 comments on commit e20ba67

Please sign in to comment.