This repository was archived by the owner on Jun 27, 2023. It is now read-only.
File tree 2 files changed +18
-0
lines changed
2 files changed +18
-0
lines changed Original file line number Diff line number Diff line change @@ -244,6 +244,12 @@ func Not(x interface{}) Matcher {
244
244
// var s fmt.Stringer = &bytes.Buffer{}
245
245
// AssignableToTypeOf(s).Matches(time.Second) // returns true
246
246
// AssignableToTypeOf(s).Matches(99) // returns false
247
+ //
248
+ // var ctx = reflect.TypeOf((*context.Context)).Elem()
249
+ // AssignableToTypeOf(ctx).Matches(context.Background()) // returns true
247
250
func AssignableToTypeOf (x interface {}) Matcher {
251
+ if xt , ok := x .(reflect.Type ); ok {
252
+ return assignableToTypeOfMatcher {xt }
253
+ }
248
254
return assignableToTypeOfMatcher {reflect .TypeOf (x )}
249
255
}
Original file line number Diff line number Diff line change 17
17
package gomock_test
18
18
19
19
import (
20
+ "context"
20
21
"errors"
22
+ "reflect"
21
23
"testing"
22
24
23
25
"github.com/golang/mock/gomock"
@@ -123,4 +125,14 @@ func TestAssignableToTypeOfMatcher(t *testing.T) {
123
125
if match := gomock .AssignableToTypeOf (& Dog {}).Matches (& Dog {Breed : "pug" , Name : "Fido" }); ! match {
124
126
t .Errorf (`AssignableToTypeOf(&Dog{}) should match &Dog{Breed: "pug", Name: "Fido"}` )
125
127
}
128
+
129
+ ctxInterface := reflect .TypeOf ((* context .Context )(nil )).Elem ()
130
+ if match := gomock .AssignableToTypeOf (ctxInterface ).Matches (context .Background ()); ! match {
131
+ t .Errorf (`AssignableToTypeOf(context.Context) should not match context.Background()` )
132
+ }
133
+
134
+ ctxWithValue := context .WithValue (context .Background (), "key" , "val" )
135
+ if match := gomock .AssignableToTypeOf (ctxInterface ).Matches (ctxWithValue ); ! match {
136
+ t .Errorf (`AssignableToTypeOf(context.Context) should not match ctxWithValue` )
137
+ }
126
138
}
You can’t perform that action at this time.
0 commit comments