Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Add Equal, Func, and Op variants of SliceContainsSubset and SliceContainsAll #176

Merged
merged 4 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1 @@
*.go text eol=lf
* text eol=lf
70 changes: 70 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 Expand Up @@ -756,6 +792,40 @@ func ExampleSliceContainsSubset() {
// Output:
}

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

func ExampleSliceContainsSubsetFunc() {
// comparing slice to element of same type
words := []string{"UP", "DoWn", "LefT", "RiGHT"}
wordsSubset := []string{"left", "down"}
SliceContainsSubsetFunc(t, words, wordsSubset, func(a, b string) bool {
return strings.EqualFold(a, b)
})

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

func ExampleSliceContainsSubsetOp() {
nums := []int{1, 2, 3, 4, 5}
subset := []int{5, 4, 3}
SliceContainsSubsetOp(t, nums, subset)
// Output:
}

func ExampleSliceEmpty() {
var ints []int
SliceEmpty(t, ints)
Expand Down
78 changes: 78 additions & 0 deletions internal/assertions/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,20 @@ func containsFunc[A, B any](slice []A, item B, eq func(a A, b B) bool) bool {
return found
}

func containsSubsetFunc[A, B any](slice []A, items []B, eq func(a A, b B) bool) (ok bool, missing B) {
OUTER:
for _, target := range items {
var item A
for _, item = range slice {
if eq(item, target) {
continue OUTER
}
}
return false, target
}
return true, missing
}

func isNil(a any) bool {
// comparable check only works for simple types
if a == nil {
Expand Down Expand Up @@ -456,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 All @@ -466,6 +510,40 @@ func SliceContainsAll[A any](slice, items []A, opts ...cmp.Option) (s string) {
return SliceContainsSubset(slice, items, opts...)
}

func SliceContainsSubsetOp[C comparable](slice, items []C) (s string) {
OUTER:
for _, target := range items {
var item C
for _, item = range slice {
if target == item {
continue OUTER
}
}
s = "expected slice to contain missing item via == operator\n"
s += bullet("slice is missing %#v\n", item)
return
}
return
}

func SliceContainsSubsetFunc[A, B any](slice []A, items []B, eq func(a A, b B) bool) (s string) {
ok, missing := containsSubsetFunc(slice, items, eq)
if !ok {
s = "expected slice to contain missing item via 'eq' function\n"
s += bullet("slice is missing %#v\n", missing)
}
return
}

func SliceContainsSubsetEqual[E interfaces.EqualFunc[E]](slice, items []E) (s string) {
ok, missing := containsSubsetFunc(slice, items, E.Equal)
if !ok {
s = "expected slice to contain missing item via .Equal method\n"
s += bullet("slice is missing %#v\n", missing)
}
return
}

func SliceContainsSubset[A any](slice, items []A, opts ...cmp.Option) (s string) {
OUTER:
for _, target := range items {
Expand Down
70 changes: 70 additions & 0 deletions must/examples_test.go

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

55 changes: 52 additions & 3 deletions must/must.go

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

Loading
Loading