Skip to content

Commit

Permalink
update:intersect containsby utest
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-xian committed Mar 8, 2022
1 parent 0cfa22a commit 2491b05
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion intersect.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func Contains[T comparable](collection []T, element T) bool {
}

// ContainsBy returns true if predicate function return true.
func ContainsBy[T any](collection []T, predicate func(T)bool) bool {
func ContainsBy[T any](collection []T, predicate func(T) bool) bool {
for _, item := range collection {
if predicate(item) {
return true
Expand Down
22 changes: 22 additions & 0 deletions intersect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,28 @@ func TestContains(t *testing.T) {
is.Equal(result2, false)
}

func TestContainsBy(t *testing.T) {
is := assert.New(t)

type a struct {
A int
B string
}

a1 := []a{a{A: 1, B: "1"}, a{A: 2, B: "2"}, a{A: 3, B: "3"}}
result1 := ContainsBy[a](a1, func(t a) bool { return t.A == 1 && t.B == "2" })
result2 := ContainsBy[a](a1, func(t a) bool { return t.A == 2 && t.B == "2" })

a2 := []string{"aaa", "bbb", "ccc"}
result3 := ContainsBy[string](a2, func(t string) bool { return t == "ccc" })
result4 := ContainsBy[string](a2, func(t string) bool { return t == "ddd" })

is.Equal(result1, false)
is.Equal(result2, true)
is.Equal(result3, true)
is.Equal(result4, false)
}

func TestSome(t *testing.T) {
is := assert.New(t)

Expand Down

0 comments on commit 2491b05

Please sign in to comment.