refactor: improve code structure and add tests

This commit is contained in:
2022-02-15 01:07:05 +00:00
parent e844b45865
commit f5cfcf86af
6 changed files with 822 additions and 71 deletions

View File

@@ -4,8 +4,6 @@ package gomockctx
import (
"context"
"fmt"
"reflect"
"github.com/golang/mock/gomock"
)
@@ -15,76 +13,10 @@ type (
contextValue string
)
var ctxKey contextKey = "gomockctx context ID"
// New returns a context as a child of the given parent, and with a randomized
// gomockctx ID value set, making it a gomockctx context. This can then be used
// with Is to get a gomock Matcher which returns true for the context from New,
// or any child contexts of it.
//
// If crypto/rand returns an error, this will panic trying to generate the
// gomockctx ID. In practise though, crypto/rand should never return a error.
func New(parent context.Context) context.Context {
return context.WithValue(parent, ctxKey, newCtxID())
}
// Is accepts a context with a gomockctx ID value (as returned from New), and
// returns a gomock.Matcher which returns true for the given context, of any
// child contexts of it.
//
// If ctx was not returned from New, the resulting matcher will ALWAYS return
// false.
func Is(ctx context.Context) gomock.Matcher {
return WithValue(ctxKey, value(ctx))
}
// ID returns the gomockctx ID value in the given context, or a empty string if
// the context does not have a gomockctx ID value.
func ID(ctx context.Context) string {
if ctx == nil {
return ""
}
return string(value(ctx))
}
// Any returns a gomock.Matcher which matches any context.Context object.
func Any() gomock.Matcher {
return gomock.AssignableToTypeOf(
reflect.TypeOf((*context.Context)(nil)).Elem(),
)
}
// WithValue returns a gomock.Matcher which matches any context that has the
// specified key and value.
func WithValue(key interface{}, value interface{}) gomock.Matcher {
return &contextMatcher{
key: key,
value: value,
}
}
type contextMatcher struct {
key interface{}
value interface{}
}
var _ gomock.Matcher = &contextMatcher{}
func (cm *contextMatcher) Matches(x interface{}) bool {
if ctx, ok := x.(context.Context); ok {
return reflect.DeepEqual(cm.value, ctx.Value(cm.key))
}
return false
}
func (cm *contextMatcher) String() string {
return fmt.Sprintf(`context with "%+v" = "%+v"`, cm.key, cm.value)
}
var ctxKey contextKey = "gomockctx ID"
func newCtxID() contextValue {
id, err := randString(64)
id, err := randString(32)
if err != nil {
panic(err)
}
@@ -92,7 +24,7 @@ func newCtxID() contextValue {
return contextValue(id)
}
func value(ctx context.Context) contextValue {
func getValue(ctx context.Context) contextValue {
var value contextValue
if ctx == nil {
return value
@@ -105,3 +37,34 @@ func value(ctx context.Context) contextValue {
return value
}
// New returns a context as a child of the given parent, which includes a
// randomized gomockctx ID value set, which makes it a gomockctx context. This
// can then be used with Is to get a gomock Matcher which returns true for the
// context from New, or any child contexts of it.
//
// If crypto/rand returns an error, this will panic trying to generate the
// gomockctx ID. In practice though, crypto/rand should never return a error.
func New(parent context.Context) context.Context {
return context.WithValue(parent, ctxKey, newCtxID())
}
// Is accepts a context with a gomockctx ID value (as returned from New), and
// returns a gomock.Matcher which returns true for the given context, of any
// child contexts of it.
//
// If ctx was not returned from New, the resulting matcher will ALWAYS return
// false.
func Is(ctx context.Context) gomock.Matcher {
return WithValue(ctxKey, getValue(ctx))
}
// ID returns the gomockctx ID value in the given context, or a empty string if
// the context does not have a gomockctx ID value.
func ID(ctx context.Context) string {
if ctx == nil {
return ""
}
return string(getValue(ctx))
}