Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

Commit 1bf84fa

Browse files
islishudecodyoss
authored andcommittedDec 27, 2019
Allow AssignableToTypeOf reflect.Type (#365)
Fixes #320
1 parent b4b7d21 commit 1bf84fa

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed
 

‎gomock/matchers.go

+6
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,12 @@ func Not(x interface{}) Matcher {
244244
// var s fmt.Stringer = &bytes.Buffer{}
245245
// AssignableToTypeOf(s).Matches(time.Second) // returns true
246246
// AssignableToTypeOf(s).Matches(99) // returns false
247+
//
248+
// var ctx = reflect.TypeOf((*context.Context)).Elem()
249+
// AssignableToTypeOf(ctx).Matches(context.Background()) // returns true
247250
func AssignableToTypeOf(x interface{}) Matcher {
251+
if xt, ok := x.(reflect.Type); ok {
252+
return assignableToTypeOfMatcher{xt}
253+
}
248254
return assignableToTypeOfMatcher{reflect.TypeOf(x)}
249255
}

‎gomock/matchers_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
package gomock_test
1818

1919
import (
20+
"context"
2021
"errors"
22+
"reflect"
2123
"testing"
2224

2325
"github.com/golang/mock/gomock"
@@ -123,4 +125,14 @@ func TestAssignableToTypeOfMatcher(t *testing.T) {
123125
if match := gomock.AssignableToTypeOf(&Dog{}).Matches(&Dog{Breed: "pug", Name: "Fido"}); !match {
124126
t.Errorf(`AssignableToTypeOf(&Dog{}) should match &Dog{Breed: "pug", Name: "Fido"}`)
125127
}
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+
}
126138
}

0 commit comments

Comments
 (0)
This repository has been archived.