Skip to content

Commit 7b743b7

Browse files
committed
feat: initial implementation
0 parents  commit 7b743b7

File tree

5 files changed

+155
-0
lines changed

5 files changed

+155
-0
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# gomockctx
2+
3+
Go package with [gomock](https://github.com/golang/mock) helpers for matching
4+
[context.Context](https://pkg.go.dev/context).

go.mod

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/romdo/gomockctx
2+
3+
go 1.17
4+
5+
require github.com/golang/mock v1.6.0

go.sum

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
2+
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
3+
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
4+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
5+
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
6+
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
7+
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
8+
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
9+
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
10+
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
11+
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
12+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
13+
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
14+
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
15+
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
16+
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
17+
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
18+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
19+
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
20+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
21+
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
22+
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
23+
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
24+
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
25+
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

gomockctx.go

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
}

rand.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package gomockctx
2+
3+
import (
4+
"crypto/rand"
5+
"math/big"
6+
)
7+
8+
const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
9+
"abcdefghijklmnopqrstuvwxyz" +
10+
"0123456789"
11+
12+
// randString returns a cryptographically secure random string of alphanumeric
13+
// characters of n length.
14+
//
15+
// Borrowed from github.com/jimeh/rands package.
16+
func randString(n int) (string, error) {
17+
l := big.NewInt(int64(len(alphabet)))
18+
b := make([]byte, n)
19+
for i := 0; i < n; i++ {
20+
index, err := rand.Int(rand.Reader, l)
21+
if err != nil {
22+
return "", err
23+
}
24+
b[i] = alphabet[index.Int64()]
25+
}
26+
27+
return string(b), nil
28+
}

0 commit comments

Comments
 (0)