From 9f0f1ddcc85352b22ff4fc1df5d68c79fc4129f8 Mon Sep 17 00:00:00 2001 From: ovechkin-dm Date: Tue, 10 Sep 2024 11:37:10 +0200 Subject: [PATCH] safe exact matching --- docs/sponsors.md | 8 ++++++++ mkdocs.yml | 1 + mock/api.go | 11 +++++++++++ tests/match/match_test.go | 12 ++++++++++++ tests/mocking/mock_test.go | 4 ++-- 5 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 docs/sponsors.md diff --git a/docs/sponsors.md b/docs/sponsors.md new file mode 100644 index 0000000..0c1f13a --- /dev/null +++ b/docs/sponsors.md @@ -0,0 +1,8 @@ +# Sponsors list + +Thanks to all the people who supported this project by donating money, +time or resources: + +* [Ovechkin Dmitry](https://github.com/ovechkin-dm) +* [Eray Ates](https://github.com/rytsh) +* [Emilien Puget](https://github.com/emilien-puget) \ No newline at end of file diff --git a/mkdocs.yml b/mkdocs.yml index 0d7a260..a9f8215 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -61,6 +61,7 @@ nav: - Parallel execution: features/parallel-execution.md - Error reporting: features/error-reporting.md - Limitations: limitations.md + - Sponsors: sponsors.md extra_css: - stylesheets/extra.css diff --git a/mock/api.go b/mock/api.go index 83a55dd..aa66f0c 100644 --- a/mock/api.go +++ b/mock/api.go @@ -286,6 +286,17 @@ 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 { + 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 !arv.Comparable() { + return false + } return value == actual }) registry.AddMatcher(m) diff --git a/tests/match/match_test.go b/tests/match/match_test.go index bc896f9..1209ff2 100644 --- a/tests/match/match_test.go +++ b/tests/match/match_test.go @@ -12,6 +12,10 @@ type Iface interface { Test(i interface{}) bool } +type Greeter interface { + Greet(name any) string +} + type St struct { value int } @@ -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) +} diff --git a/tests/mocking/mock_test.go b/tests/mocking/mock_test.go index a5343d4..4b6cf9c 100644 --- a/tests/mocking/mock_test.go +++ b/tests/mocking/mock_test.go @@ -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()