Skip to content

Commit

Permalink
Fixes matching of expected arguments in mock_verify.go (#23)
Browse files Browse the repository at this point in the history
* Fixes matching of expected arguments in mock_verify.go
  • Loading branch information
matzefriedrich authored Sep 9, 2024
1 parent ca31b47 commit 21ab333
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 20 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Fixed

* Fixes matching of expected arguments in `mock_verify.go`


## [v0.9.1] - 2024-09-09

### Changed
Expand Down
19 changes: 16 additions & 3 deletions internal/tests/features/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,34 @@ func Test_GreeterMock_SayHello(t *testing.T) {

// Arrange
mock := NewGreeterMock()
mock.SayHelloFunc = func(name string) (string, error) {
mock.SayHelloFunc = func(name string, polite bool) (string, error) {
return fmt.Sprintf("Hi, %s", name), nil
}

const expectedName = "John"

// Act
_, _ = mock.SayHello("Max")
actual, err := mock.SayHello(expectedName)
_, _ = mock.SayHello("Max", false)
actual, err := mock.SayHello(expectedName, true)

// Assert
assert.NoError(t, err)
assert.Equal(t, "Hi, John", actual)

// Verify that John has been greeted, regardless of the politeness flag
assert.True(t, mock.Verify(FunctionSayHello, features.TimesOnce(), features.Exact(expectedName)))

// Verify that John has not been greeted non-politely
assert.True(t, mock.Verify(FunctionSayHello, features.TimesNever(), features.Exact(expectedName), features.Exact(false)))

// Verify that somebody was greeted politely
assert.True(t, mock.Verify(FunctionSayHello, features.TimesOnce(), features.IsAny(), features.Exact(true)))

// Verify that Jane was not greeted
assert.True(t, mock.Verify(FunctionSayHello, features.TimesNever(), features.Exact("Jane")))

// Verify that SayHello was called twice in total, regardless of the call parameters
assert.True(t, mock.Verify(FunctionSayHello, features.TimesExactly(2)))
assert.True(t, mock.Verify(FunctionSayHello, features.TimesExactly(2), features.IsAny()))
assert.True(t, mock.Verify(FunctionSayHello, features.TimesExactly(2), features.IsAny(), features.IsAny()))
}
4 changes: 2 additions & 2 deletions internal/tests/features/register_proxy_error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func Test_Register_generated_proxy_type_handles_error(t *testing.T) {

// Act
proxy, _ := resolving.ResolveRequiredService[GreeterProxy](resolver, ctx)
msg, _ := proxy.SayHello("Jane")
msg, _ := proxy.SayHello("Jane", false)
fmt.Println(msg)

// Assert
Expand All @@ -73,7 +73,7 @@ type johnGreeter struct {
func (g johnGreeter) SayNothing() {
}

func (g johnGreeter) SayHello(name string) (string, error) {
func (g johnGreeter) SayHello(name string, _ bool) (string, error) {
if name != "John" {
return "", fmt.Errorf("name is not John")
}
Expand Down
4 changes: 2 additions & 2 deletions internal/tests/features/register_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func Test_Register_generated_proxy_type(t *testing.T) {

// Act
proxy, _ := resolving.ResolveRequiredService[GreeterProxy](resolver, ctx)
msg, _ := proxy.SayHello("John")
msg, _ := proxy.SayHello("John", false)
fmt.Println(msg)

// Assert
Expand Down Expand Up @@ -89,7 +89,7 @@ type greeter struct {
func (g greeter) SayNothing() {
}

func (g greeter) SayHello(name string) (string, error) {
func (g greeter) SayHello(name string, _ bool) (string, error) {
return "Hello " + name, nil
}

Expand Down
2 changes: 1 addition & 1 deletion internal/tests/features/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ package features
//go:generate parsley-cli generate mocks

type Greeter interface {
SayHello(name string) (string, error)
SayHello(name string, polite bool) (string, error)
SayNothing()
}
12 changes: 6 additions & 6 deletions internal/tests/features/types.mocks.g.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions internal/tests/features/types.proxy.g.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion pkg/features/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ type MockFunction struct {
tracedCalls []methodCall
}

func (m MockFunction) String() string {
if len(m.signature) > 0 {
return m.signature
}
return m.name
}

type methodCall struct {
args []any
}
Expand Down Expand Up @@ -38,4 +45,3 @@ func (m *MockBase) TraceMethodCall(name string, arguments ...any) {
m.functions[name] = function
}
}

9 changes: 6 additions & 3 deletions pkg/features/mock_verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,14 @@ func (m *MockBase) Verify(name string, times TimesFunc, matches ...ArgMatch) boo
numMatches := 0
callsLoop:
for _, call := range function.tracedCalls {
for _, arg := range call.args {
for _, doesMatch := range matches {
if doesMatch(arg) == false {
for i, arg := range call.args {
if i < len(matches) {
match := matches[i]
if match(arg) == false {
continue callsLoop
}
} else {
break
}
}
numMatches++
Expand Down

0 comments on commit 21ab333

Please sign in to comment.