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

Allow AssignableToTypeOf reflect.Type #365

Merged
merged 1 commit into from
Dec 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions gomock/matchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,12 @@ func Not(x interface{}) Matcher {
// var s fmt.Stringer = &bytes.Buffer{}
// AssignableToTypeOf(s).Matches(time.Second) // returns true
// AssignableToTypeOf(s).Matches(99) // returns false
//
// var ctx = reflect.TypeOf((*context.Context)).Elem()
// AssignableToTypeOf(ctx).Matches(context.Background()) // returns true
func AssignableToTypeOf(x interface{}) Matcher {
if xt, ok := x.(reflect.Type); ok {
return assignableToTypeOfMatcher{xt}
}
return assignableToTypeOfMatcher{reflect.TypeOf(x)}
}
12 changes: 12 additions & 0 deletions gomock/matchers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
package gomock_test

import (
"context"
"errors"
"reflect"
"testing"

"github.com/golang/mock/gomock"
Expand Down Expand Up @@ -116,4 +118,14 @@ func TestAssignableToTypeOfMatcher(t *testing.T) {
if match := gomock.AssignableToTypeOf(&Dog{}).Matches(&Dog{Breed: "pug", Name: "Fido"}); !match {
t.Errorf(`AssignableToTypeOf(&Dog{}) should match &Dog{Breed: "pug", Name: "Fido"}`)
}

ctxInterface := reflect.TypeOf((*context.Context)(nil)).Elem()
if match := gomock.AssignableToTypeOf(ctxInterface).Matches(context.Background()); !match {
t.Errorf(`AssignableToTypeOf(context.Context) should not match context.Background()`)
}

ctxWithValue := context.WithValue(context.Background(), "key", "val")
if match := gomock.AssignableToTypeOf(ctxInterface).Matches(ctxWithValue); !match {
t.Errorf(`AssignableToTypeOf(context.Context) should not match ctxWithValue`)
}
}