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

Commit

Permalink
Allow AssignableToTypeOf reflect.Type (#365)
Browse files Browse the repository at this point in the history
Fixes #320
  • Loading branch information
islishude authored and codyoss committed Dec 27, 2019
1 parent b4b7d21 commit 1bf84fa
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions gomock/matchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,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 @@ -123,4 +125,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`)
}
}

0 comments on commit 1bf84fa

Please sign in to comment.