Skip to content

Commit d1bdc9c

Browse files
committed
Add IsPredicate helper
- Write tests for `IsPredicate` - Write missing tests for `IsFunction`
1 parent 5035611 commit d1bdc9c

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

helpers.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func PtrOf(itf interface{}) interface{} {
7575
func IsFunction(in interface{}, num ...int) bool {
7676
funcType := reflect.TypeOf(in)
7777

78-
result := funcType.Kind() == reflect.Func
78+
result := funcType != nil && funcType.Kind() == reflect.Func
7979

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

91+
// IsPredicate returns if the argument is a predicate function.
92+
func IsPredicate(in interface{}, inTypes ...reflect.Type) bool {
93+
if len(inTypes) == 0 {
94+
inTypes = append(inTypes, nil)
95+
}
96+
97+
funcType := reflect.TypeOf(in)
98+
99+
result := funcType != nil && funcType.Kind() == reflect.Func
100+
101+
result = result && funcType.NumOut() == 1 && funcType.Out(0).Kind() == reflect.Bool
102+
result = result && funcType.NumIn() == len(inTypes)
103+
104+
for i := 0; result && i < len(inTypes); i++ {
105+
inType := inTypes[i]
106+
result = inType == nil || inType.ConvertibleTo(funcType.In(i))
107+
}
108+
109+
return result
110+
}
111+
91112
// IsEqual returns if the two objects are equal
92113
func IsEqual(expected interface{}, actual interface{}) bool {
93114
if expected == nil || actual == nil {

helpers_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,3 +269,32 @@ func TestIsIteratee(t *testing.T) {
269269

270270
is.False(IsIteratee(nil))
271271
}
272+
273+
func TestIsFunction(t *testing.T) {
274+
is := assert.New(t)
275+
276+
is.False(IsFunction(nil))
277+
is.False(IsFunction(""))
278+
is.True(IsFunction(func() {}))
279+
is.True(IsFunction(func(string, string, string) bool { return false }, 3))
280+
is.False(IsFunction(func(string, string, string) bool { return false }, 3, 0))
281+
is.True(IsFunction(func(string, string, string) (bool, error) { return false, nil }, 3, 2))
282+
}
283+
284+
func TestIsPredicate(t *testing.T) {
285+
is := assert.New(t)
286+
287+
is.False(IsPredicate(nil))
288+
is.False(IsPredicate(""))
289+
is.False(IsPredicate(func() {}))
290+
is.False(IsPredicate(func() bool { return false}))
291+
is.True(IsPredicate(func(int) bool { return false}))
292+
is.True(IsPredicate(func(int) bool { return false}, nil))
293+
is.False(IsPredicate(func(int) bool { return false}, reflect.TypeOf("")))
294+
is.True(IsPredicate(func(int) bool { return false}, reflect.TypeOf(0)))
295+
is.False(IsPredicate(func(int, string) bool { return false}, reflect.TypeOf("")))
296+
is.False(IsPredicate(func(int, string) bool { return false}, reflect.TypeOf(""), reflect.TypeOf(0)))
297+
is.True(IsPredicate(func(int, string) bool { return false}, reflect.TypeOf(0), reflect.TypeOf("")))
298+
is.False(IsPredicate(func(struct{}, string) bool { return false}, reflect.TypeOf(0), reflect.TypeOf("")))
299+
is.True(IsPredicate(func(struct{}, string) bool { return false}, nil, reflect.TypeOf("")))
300+
}

0 commit comments

Comments
 (0)