/
hasfunction.go
31 lines (28 loc) · 989 Bytes
/
hasfunction.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package has
import (
"fmt"
"github.com/corbym/gocrest"
"reflect"
)
//FunctionNamed implementation for testing if a Type has a particular method name. Does not check parameters.
//Returns a matcher that will use reflect to check if the actual has the method given by expected.
func FunctionNamed(expected string) *gocrest.Matcher {
matcher := new(gocrest.Matcher)
matcher.Describe = fmt.Sprintf("interface with function %s", expected)
matcher.Matches = func(actual interface{}) bool {
typeOfActual := reflect.TypeOf(actual)
matcher.Actual = actualStringValue(typeOfActual)
expectedName := reflect.ValueOf(expected).String()
_, ok := typeOfActual.Elem().MethodByName(expectedName)
return ok
}
return matcher
}
func actualStringValue(actualType reflect.Type) string {
description := actualType.Elem().Name() + "{"
for x := 0; x < actualType.Elem().NumMethod(); x++ {
description += actualType.Elem().Method(x).Name + "()"
}
description += "}"
return description
}