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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
package gleak
import (
"fmt"
"strings"
"github.com/onsi/gomega/format"
"github.com/onsi/gomega/types"
)
// IgnoringCreator succeeds if the goroutine was created by a function matching
// the specified name. The expected creator function name is either in the form
// of "creatorfunction-name" or "creatorfunction-name...".
//
// An ellipsis "..." after a creatorfunction-name matches any creator function
// name if creatorfunction-name is a prefix and the goroutine's creator function
// name is at least one level deeper. For instance, "foo.bar..." matches
// "foo.bar.baz", but doesn't match "foo.bar".
func IgnoringCreator(creatorfname string) types.GomegaMatcher {
if strings.HasSuffix(creatorfname, "...") {
expectedCreatorFunction := creatorfname[:len(creatorfname)-3+1] // ...one trailing dot still expected
return &ignoringCreator{
expectedCreatorFunction: expectedCreatorFunction,
matchPrefix: true,
}
}
return &ignoringCreator{
expectedCreatorFunction: creatorfname,
}
}
type ignoringCreator struct {
expectedCreatorFunction string
matchPrefix bool
}
// Match succeeds if an actual goroutine's creator function in the backtrace
// matches the specified function name or function name prefix.
func (matcher *ignoringCreator) Match(actual interface{}) (success bool, err error) {
g, err := G(actual, "IgnoringCreator")
if err != nil {
return false, err
}
if matcher.matchPrefix {
return strings.HasPrefix(g.CreatorFunction, matcher.expectedCreatorFunction), nil
}
return g.CreatorFunction == matcher.expectedCreatorFunction, nil
}
// FailureMessage returns a failure message if the actual goroutine doesn't have
// the specified function name/prefix (and optional state) at the top of the
// backtrace.
func (matcher *ignoringCreator) FailureMessage(actual interface{}) (message string) {
return format.Message(actual, matcher.message())
}
// NegatedFailureMessage returns a failure message if the actual goroutine has
// the specified function name/prefix (and optional state) at the top of the
// backtrace.
func (matcher *ignoringCreator) NegatedFailureMessage(actual interface{}) (message string) {
return format.Message(actual, "not "+matcher.message())
}
func (matcher *ignoringCreator) message() string {
if matcher.matchPrefix {
return fmt.Sprintf("to be created by a function with prefix %q", matcher.expectedCreatorFunction)
}
return fmt.Sprintf("to be created by %q", matcher.expectedCreatorFunction)
}
|