Skip to content

Commit

Permalink
fix equality for dynamic structs
Browse files Browse the repository at this point in the history
  • Loading branch information
ovechkin-dm committed Sep 10, 2024
1 parent 3d6f55c commit 091bb4b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
9 changes: 7 additions & 2 deletions mock/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,15 @@ func SliceEqualUnordered[T any](values []T) []T {
func Exact[T comparable](value T) T {
desc := fmt.Sprintf("Exact(%v)", value)
m := registry.FunMatcher(desc, func(m []any, actual T) bool {
if !reflect.ValueOf(value).Comparable() {
vrv := reflect.ValueOf(value)
arv := reflect.ValueOf(actual)
if vrv.Kind() == reflect.Struct && arv.Kind() == reflect.Struct {
return vrv == arv
}
if !vrv.Comparable() {
return false
}
if !reflect.ValueOf(actual).Comparable() {
if !arv.Comparable() {
return false
}
return value == actual
Expand Down
12 changes: 12 additions & 0 deletions tests/match/match_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ type Iface interface {
Test(i interface{}) bool
}

type Greeter interface {
Greet(name any) string
}

type St struct {
value int
}
Expand Down Expand Up @@ -320,3 +324,11 @@ func TestUnexpectedUseOfMatchers(t *testing.T) {
Verify(m, Once()).Test("test")
r.AssertErrorContains(r.GetError(), "Unexpected matchers declaration")
}

func TestExactNotComparable(t *testing.T) {
SetUp(t)
greeter := Mock[Greeter]()
var data any = []int{1, 2}
When(greeter.Greet(Exact(data))).ThenReturn("hello world")
greeter.Greet(data)
}
4 changes: 2 additions & 2 deletions tests/mocking/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ func TestMockWithMockedArg(t *testing.T) {
SetUp(r)
callingMock := Mock[CallingIface]()
otherMock := Mock[OtherIface]()
WhenSingle(callingMock.GetMocked(Exact[OtherIface](otherMock))).ThenReturn(otherMock)
WhenSingle(callingMock.GetMocked(Exact(otherMock))).ThenReturn(otherMock)
res := callingMock.GetMocked(otherMock)
Verify(callingMock, Times(1)).GetMocked(Exact[OtherIface](otherMock))
Verify(callingMock, Times(1)).GetMocked(Exact(otherMock))
VerifyNoMoreInteractions(callingMock)
r.AssertEqual(otherMock, res)
r.AssertNoError()
Expand Down

0 comments on commit 091bb4b

Please sign in to comment.