mirror of
https://github.com/samber/lo.git
synced 2026-04-22 23:47:11 +08:00
update:intersect containsby utest
This commit is contained in:
+1
-1
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user