|
| 1 | +// Package gomockctx contains gomock helpers for matching context.Context |
| 2 | +// objects. |
| 3 | +package gomockctx |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "fmt" |
| 8 | + "reflect" |
| 9 | + |
| 10 | + "github.com/golang/mock/gomock" |
| 11 | +) |
| 12 | + |
| 13 | +type ( |
| 14 | + contextKey string |
| 15 | + contextValue string |
| 16 | +) |
| 17 | + |
| 18 | +var ctxKey contextKey = "gomockctx context ID" |
| 19 | + |
| 20 | +func newCtxID() contextValue { |
| 21 | + id, err := randString(64) |
| 22 | + if err != nil { |
| 23 | + panic(err) |
| 24 | + } |
| 25 | + |
| 26 | + return contextValue(id) |
| 27 | +} |
| 28 | + |
| 29 | +func value(ctx context.Context) contextValue { |
| 30 | + var value contextValue |
| 31 | + v := ctx.Value(ctxKey) |
| 32 | + if s, ok := v.(contextValue); ok { |
| 33 | + value = s |
| 34 | + } |
| 35 | + |
| 36 | + return value |
| 37 | +} |
| 38 | + |
| 39 | +// New returns a context as a child of the given parent, and with a randomized |
| 40 | +// gomockctx ID value set, making it a gomockctx context. This can then be used |
| 41 | +// with Is to get a gomock Matcher which returns true for the context from New, |
| 42 | +// or any child contexts of it. |
| 43 | +// |
| 44 | +// If crypto/rand returns an error, this will panic trying to generate the |
| 45 | +// gomockctx ID. In practise though, crypto/rand should never return a error. |
| 46 | +func New(parent context.Context) context.Context { |
| 47 | + return context.WithValue(parent, ctxKey, newCtxID()) |
| 48 | +} |
| 49 | + |
| 50 | +// Is accepts a context with a gomockctx ID value (as returned from New), and |
| 51 | +// returns a gomock.Matcher which returns true for the given context, of any |
| 52 | +// child contexts of it. |
| 53 | +// |
| 54 | +// If ctx was not returned from New, the resulting matcher will ALWAYS return |
| 55 | +// false. |
| 56 | +func Is(ctx context.Context) gomock.Matcher { |
| 57 | + return WithValue(ctxKey, value(ctx)) |
| 58 | +} |
| 59 | + |
| 60 | +// WithValue creates a generic gomock context matcher which returns true for any |
| 61 | +// context which has the specified key/value. |
| 62 | +func WithValue(key interface{}, value interface{}) gomock.Matcher { |
| 63 | + return &contextMatcher{ |
| 64 | + key: key, |
| 65 | + value: value, |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +// ID returns the gomockctx ID value in the given context, or a empty string if |
| 70 | +// it not a gomockctx context. |
| 71 | +func ID(ctx context.Context) string { |
| 72 | + return string(value(ctx)) |
| 73 | +} |
| 74 | + |
| 75 | +type contextMatcher struct { |
| 76 | + key interface{} |
| 77 | + value interface{} |
| 78 | +} |
| 79 | + |
| 80 | +var _ gomock.Matcher = &contextMatcher{} |
| 81 | + |
| 82 | +func (e *contextMatcher) Matches(x interface{}) bool { |
| 83 | + ctx, ok := x.(context.Context) |
| 84 | + if !ok { |
| 85 | + return false |
| 86 | + } |
| 87 | + |
| 88 | + return reflect.DeepEqual(e.value, ctx.Value(e.key)) |
| 89 | +} |
| 90 | + |
| 91 | +func (e *contextMatcher) String() string { |
| 92 | + return fmt.Sprintf(`context with "%+v" = "%+v"`, e.key, e.value) |
| 93 | +} |
0 commit comments