diff --git a/gomock/matchers.go b/gomock/matchers.go index cf5566ef..770aba5a 100644 --- a/gomock/matchers.go +++ b/gomock/matchers.go @@ -102,7 +102,21 @@ type eqMatcher struct { } func (e eqMatcher) Matches(x interface{}) bool { - return reflect.DeepEqual(e.x, x) + // In case, some value is nil + if e.x == nil || x == nil { + return reflect.DeepEqual(e.x, x) + } + + // Check if types assignable and convert them to common type + x1Val := reflect.ValueOf(e.x) + x2Val := reflect.ValueOf(x) + + if x1Val.Type().AssignableTo(x2Val.Type()) { + x1ValConverted := x1Val.Convert(x2Val.Type()) + return reflect.DeepEqual(x1ValConverted.Interface(), x2Val.Interface()) + } + + return false } func (e eqMatcher) String() string { diff --git a/gomock/matchers_test.go b/gomock/matchers_test.go index ebddf0c6..35bd2409 100644 --- a/gomock/matchers_test.go +++ b/gomock/matchers_test.go @@ -26,6 +26,8 @@ import ( "github.com/golang/mock/gomock/internal/mock_gomock" ) +type A []string + func TestMatchers(t *testing.T) { type e interface{} tests := []struct { @@ -44,6 +46,10 @@ func TestMatchers(t *testing.T) { []e{[]int{1, 2}, "ab", map[string]int{"a": 0, "b": 1}, [2]string{"a", "b"}}, []e{[]int{1}, "a", 42, 42.0, false, [1]string{"a"}}, }, + {"test assignable types", gomock.Eq(A{"a", "b"}), + []e{[]string{"a", "b"}, A{"a", "b"}}, + []e{[]string{"a"}, A{"b"}}, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {