mirror of
https://github.com/romdo/gomockctx.git
synced 2026-02-19 08:06:40 +00:00
refactor: improve code structure and add tests
This commit is contained in:
37
with_value.go
Normal file
37
with_value.go
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user