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

37
with_value.go Normal file
View File

@@ -0,0 +1,37 @@
package gomockctx
import (
"context"
"fmt"
"reflect"
"github.com/golang/mock/gomock"
)
// 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 &valueMatcher{
key: key,
value: value,
}
}
type valueMatcher struct {
key interface{}
value interface{}
}
var _ gomock.Matcher = &valueMatcher{}
func (cm *valueMatcher) Matches(x interface{}) bool {
if ctx, ok := x.(context.Context); ok {
return reflect.DeepEqual(cm.value, ctx.Value(cm.key))
}
return false
}
func (cm *valueMatcher) String() string {
return fmt.Sprintf(`context with "%+v" = "%+v"`, cm.key, cm.value)
}