github.com/m4gshm/gollections@v0.0.13-0.20240331203319-a34a86e58a24/predicate/internal/test/predicate_test.go (about) 1 package test 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 8 "github.com/m4gshm/gollections/convert/as" 9 "github.com/m4gshm/gollections/predicate/less" 10 "github.com/m4gshm/gollections/predicate/match" 11 "github.com/m4gshm/gollections/predicate/more" 12 "github.com/m4gshm/gollections/predicate/one" 13 "github.com/m4gshm/gollections/slice" 14 ) 15 16 func Test_lessThan(t *testing.T) { 17 lessC := less.Than("C") 18 assert.True(t, lessC("A")) 19 assert.True(t, lessC("B")) 20 assert.False(t, lessC("C")) 21 assert.False(t, lessC("D")) 22 } 23 24 func Test_lessOrEq(t *testing.T) { 25 lessOrEqC := less.OrEq("C") 26 assert.True(t, lessOrEqC("A")) 27 assert.True(t, lessOrEqC("B")) 28 assert.True(t, lessOrEqC("C")) 29 assert.False(t, lessOrEqC("D")) 30 } 31 32 func Test_gtThan(t *testing.T) { 33 gtC := more.Than("C") 34 assert.False(t, gtC("A")) 35 assert.False(t, gtC("B")) 36 assert.False(t, gtC("C")) 37 assert.True(t, gtC("D")) 38 assert.True(t, gtC("E")) 39 } 40 41 func Test_gtOrEq(t *testing.T) { 42 gtOrEqC := more.OrEq("C") 43 assert.False(t, gtOrEqC("A")) 44 assert.False(t, gtOrEqC("B")) 45 assert.True(t, gtOrEqC("C")) 46 assert.True(t, gtOrEqC("D")) 47 assert.True(t, gtOrEqC("E")) 48 } 49 50 func Test_OneOf(t *testing.T) { 51 oneOfACE := one.Of("A", "C", "E") 52 assert.True(t, oneOfACE("A")) 53 assert.False(t, oneOfACE("B")) 54 assert.True(t, oneOfACE("C")) 55 assert.False(t, oneOfACE("D")) 56 assert.True(t, oneOfACE("E")) 57 } 58 59 func Test_Match_To(t *testing.T) { 60 61 oneEvenMatcher := match.To(as.Is[int], func(i int) bool { return i%2 == 0 }) 62 63 assert.False(t, oneEvenMatcher(1)) 64 assert.True(t, oneEvenMatcher(2)) 65 66 anyEvenMatcher := match.To(as.Is, func(i []int) bool { return len(i) > 2 }) 67 68 assert.False(t, anyEvenMatcher(slice.Of(1, 2))) 69 assert.True(t, anyEvenMatcher(slice.Of(1, 2, 3))) 70 71 } 72 73 func Test_Match_Any(t *testing.T) { 74 75 anyEvenMatcher := match.Any(as.Is, func(i int) bool { return i%2 == 0 }) 76 77 assert.False(t, anyEvenMatcher(slice.Of(1, 3))) 78 assert.True(t, anyEvenMatcher(slice.Of(1, 2))) 79 80 }