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