Skip to content

Commit

Permalink
safe exact matching
Browse files Browse the repository at this point in the history
  • Loading branch information
ovechkin-dm authored Sep 10, 2024
1 parent dff5bfc commit 9f0f1dd
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 2 deletions.
8 changes: 8 additions & 0 deletions docs/sponsors.md
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions mock/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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 9f0f1dd

Please sign in to comment.