github.com/m4gshm/gollections@v0.0.13-0.20240331203319-a34a86e58a24/internal/examples/boilerplate/predicate_test.go (about) 1 package boilerplate 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 8 "github.com/m4gshm/gollections/op/string_" 9 "github.com/m4gshm/gollections/predicate/less" 10 "github.com/m4gshm/gollections/predicate/match" 11 "github.com/m4gshm/gollections/predicate/where" 12 "github.com/m4gshm/gollections/slice" 13 ) 14 15 func Test_Predicate(t *testing.T) { 16 17 bob, _ := slice.First(users, where.Eq(User.Name, "Bob")) 18 19 assert.Equal(t, "Bob", bob.Name()) 20 21 } 22 23 func Test_ExtendedPredicate(t *testing.T) { 24 25 userWithRoles := slice.Filter(users, match.To(User.Roles, slice.NotEmpty[[]Role])) 26 27 assert.Equal(t, "Bob", userWithRoles[0].Name()) 28 assert.Equal(t, "Alice", userWithRoles[1].Name()) 29 30 userWithNamedRoles := slice.Filter(users, where.Any(User.Roles, match.To(Role.Name, string_.NotEmpty))) 31 32 assert.Equal(t, "Bob", userWithNamedRoles[0].Name()) 33 assert.Equal(t, "Alice", userWithNamedRoles[1].Name()) 34 35 youngUsers := slice.Filter(users, where.Match(User.Age, less.OrEq(18))) 36 37 assert.Equal(t, "Tom", youngUsers[0].Name()) 38 39 }