Skip to content

Commit e20ba67

Browse files
brandondyckshoenig
authored andcommitted
test: added variants of SliceContainsAll
1 parent 2bb71ca commit e20ba67

7 files changed

Lines changed: 343 additions & 27 deletions

File tree

examples_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,42 @@ func ExampleSliceContainsAll() {
720720
// Output:
721721
}
722722

723+
func ExampleSliceContainsAllEqual() {
724+
dave := &employee{first: "dave", id: 8}
725+
armon := &employee{first: "armon", id: 2}
726+
mitchell := &employee{first: "mitchell", id: 1}
727+
SliceContainsAllEqual(t,
728+
[]*employee{dave, armon, mitchell},
729+
[]*employee{mitchell, dave, armon})
730+
// Output:
731+
}
732+
733+
func ExampleSliceContainsAllFunc() {
734+
// comparing slice to element of same type
735+
SliceContainsAllFunc(t,
736+
[]string{"UP", "DoWn", "LefT", "RiGHT"},
737+
[]string{"left", "down", "up", "right"},
738+
func(a, b string) bool {
739+
return strings.EqualFold(a, b)
740+
})
741+
742+
// comparing slice to element of different type
743+
SliceContainsAllFunc(t,
744+
[]string{"2", "4", "6", "8"},
745+
[]int{2, 6, 4, 8},
746+
func(a string, b int) bool {
747+
return a == strconv.Itoa(b)
748+
})
749+
// Output:
750+
}
751+
752+
func ExampleSliceContainsAllOp() {
753+
SliceContainsAllOp(t,
754+
[]int{1, 2, 3, 4, 5},
755+
[]int{5, 4, 3, 2, 1})
756+
// Output:
757+
}
758+
723759
func ExampleSliceContainsEqual() {
724760
dave := &employee{first: "dave", id: 8}
725761
armon := &employee{first: "armon", id: 2}

internal/assertions/assertions.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,36 @@ func SliceNotContainsFunc[A, B any](slice []A, item B, eq func(a A, b B) bool) (
470470
return
471471
}
472472

473+
func SliceContainsAllOp[C comparable](slice, items []C) (s string) {
474+
if len(slice) != len(items) {
475+
s = "expected slice and items to contain same number of elements\n"
476+
s += bullet("len(slice): %d\n", len(slice))
477+
s += bullet("len(items): %d\n", len(items))
478+
return s
479+
}
480+
return SliceContainsSubsetOp(slice, items)
481+
}
482+
483+
func SliceContainsAllFunc[A, B any](slice []A, items []B, eq func(a A, b B) bool) (s string) {
484+
if len(slice) != len(items) {
485+
s = "expected slice and items to contain same number of elements\n"
486+
s += bullet("len(slice): %d\n", len(slice))
487+
s += bullet("len(items): %d\n", len(items))
488+
return s
489+
}
490+
return SliceContainsSubsetFunc(slice, items, eq)
491+
}
492+
493+
func SliceContainsAllEqual[E interfaces.EqualFunc[E]](slice, items []E) (s string) {
494+
if len(slice) != len(items) {
495+
s = "expected slice and items to contain same number of elements\n"
496+
s += bullet("len(slice): %d\n", len(slice))
497+
s += bullet("len(items): %d\n", len(items))
498+
return s
499+
}
500+
return SliceContainsSubsetEqual(slice, items)
501+
}
502+
473503
func SliceContainsAll[A any](slice, items []A, opts ...cmp.Option) (s string) {
474504
if len(slice) != len(items) {
475505
s = "expected slice and items to contain same number of elements\n"

must/examples_test.go

Lines changed: 37 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

must/must.go

Lines changed: 26 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

must/must_test.go

Lines changed: 94 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,33 @@ func SliceNotContainsFunc[A, B any](t T, slice []A, item B, eq func(a A, b B) bo
241241
invoke(t, assertions.SliceNotContainsFunc(slice, item, eq), settings...)
242242
}
243243

244+
// SliceContainsAllOp asserts slice and items contain the same elements, but in
245+
// no particular order, using the == operator. The number of elements
246+
// in slice and items must be the same.
247+
func SliceContainsAllOp[C comparable](t T, slice, items []C, settings ...Setting) {
248+
t.Helper()
249+
invoke(t, assertions.SliceContainsAllOp(slice, items), settings...)
250+
}
251+
252+
// SliceContainsAllFunc asserts slice and items contain the same elements, but in
253+
// no particular order, using eq to compare elements. The number of elements
254+
// in slice and items must be the same.
255+
func SliceContainsAllFunc[A, B any](t T, slice []A, items []B, eq func(a A, b B) bool, settings ...Setting) {
256+
t.Helper()
257+
invoke(t, assertions.SliceContainsAllFunc(slice, items, eq), settings...)
258+
}
259+
260+
// SliceContainsAllEqual asserts slice and items contain the same elements, but in
261+
// no particular order, using Equal to compare elements. The number of elements
262+
// in slice and items must be the same.
263+
func SliceContainsAllEqual[E interfaces.EqualFunc[E]](t T, slice, items []E, settings ...Setting) {
264+
t.Helper()
265+
invoke(t, assertions.SliceContainsAllEqual(slice, items), settings...)
266+
}
267+
244268
// SliceContainsAll asserts slice and items contain the same elements, but in
245-
// no particular order. The number of elements in slice and items must be the
246-
// same.
269+
// no particular order, using cmp.Equal to compare elements. The number of elements
270+
// in slice and items must be the same.
247271
func SliceContainsAll[A any](t T, slice, items []A, settings ...Setting) {
248272
t.Helper()
249273
invoke(t, assertions.SliceContainsAll(slice, items, options(settings...)...), settings...)

0 commit comments

Comments
 (0)