Skip to content

Commit

Permalink
Add IsPredicate helper
Browse files Browse the repository at this point in the history
- Write tests for `IsPredicate`
- Write missing tests for `IsFunction`
  • Loading branch information
vellotis committed Mar 10, 2021
1 parent 5035611 commit d1bdc9c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
23 changes: 22 additions & 1 deletion helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func PtrOf(itf interface{}) interface{} {
func IsFunction(in interface{}, num ...int) bool {
funcType := reflect.TypeOf(in)

result := funcType.Kind() == reflect.Func
result := funcType != nil && funcType.Kind() == reflect.Func

if len(num) >= 1 {
result = result && funcType.NumIn() == num[0]
Expand All @@ -88,6 +88,27 @@ func IsFunction(in interface{}, num ...int) bool {
return result
}

// IsPredicate returns if the argument is a predicate function.
func IsPredicate(in interface{}, inTypes ...reflect.Type) bool {
if len(inTypes) == 0 {
inTypes = append(inTypes, nil)
}

funcType := reflect.TypeOf(in)

result := funcType != nil && funcType.Kind() == reflect.Func

result = result && funcType.NumOut() == 1 && funcType.Out(0).Kind() == reflect.Bool
result = result && funcType.NumIn() == len(inTypes)

for i := 0; result && i < len(inTypes); i++ {
inType := inTypes[i]
result = inType == nil || inType.ConvertibleTo(funcType.In(i))
}

return result
}

// IsEqual returns if the two objects are equal
func IsEqual(expected interface{}, actual interface{}) bool {
if expected == nil || actual == nil {
Expand Down
29 changes: 29 additions & 0 deletions helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,32 @@ func TestIsIteratee(t *testing.T) {

is.False(IsIteratee(nil))
}

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

is.False(IsFunction(nil))
is.False(IsFunction(""))
is.True(IsFunction(func() {}))
is.True(IsFunction(func(string, string, string) bool { return false }, 3))
is.False(IsFunction(func(string, string, string) bool { return false }, 3, 0))
is.True(IsFunction(func(string, string, string) (bool, error) { return false, nil }, 3, 2))
}

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

is.False(IsPredicate(nil))
is.False(IsPredicate(""))
is.False(IsPredicate(func() {}))
is.False(IsPredicate(func() bool { return false}))
is.True(IsPredicate(func(int) bool { return false}))
is.True(IsPredicate(func(int) bool { return false}, nil))
is.False(IsPredicate(func(int) bool { return false}, reflect.TypeOf("")))
is.True(IsPredicate(func(int) bool { return false}, reflect.TypeOf(0)))
is.False(IsPredicate(func(int, string) bool { return false}, reflect.TypeOf("")))
is.False(IsPredicate(func(int, string) bool { return false}, reflect.TypeOf(""), reflect.TypeOf(0)))
is.True(IsPredicate(func(int, string) bool { return false}, reflect.TypeOf(0), reflect.TypeOf("")))
is.False(IsPredicate(func(struct{}, string) bool { return false}, reflect.TypeOf(0), reflect.TypeOf("")))
is.True(IsPredicate(func(struct{}, string) bool { return false}, nil, reflect.TypeOf("")))
}

0 comments on commit d1bdc9c

Please sign in to comment.