...
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
12
13
14
15
16
17
18
19 func IgnoringCreator(creatorfname string) types.GomegaMatcher {
20 if strings.HasSuffix(creatorfname, "...") {
21 expectedCreatorFunction := creatorfname[:len(creatorfname)-3+1]
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
38
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
51
52
53 func (matcher *ignoringCreator) FailureMessage(actual interface{}) (message string) {
54 return format.Message(actual, matcher.message())
55 }
56
57
58
59
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 }
70
View as plain text