-
Notifications
You must be signed in to change notification settings - Fork 1.7k
mock: [feature] dynamic return values #1726
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jlou2u
wants to merge
17
commits into
stretchr:master
Choose a base branch
from
jlou2u:feature/dynamic-return-values
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+169
−2
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
649b123
allow mocked.On("X").Return(func(a Arguments) Arguments { return nil })
gburt c4b8210
better mock ReturnFunc test
gburt 2a1ff62
simpler implementation per review feedback
gburt 74b30a3
remove unused variable
gburt 9feebd4
reimplement via Run() func
gburt 1f763ab
fix typo in comment
gburt 53d1b3c
assert.FunctionalOptions: fix go doc
snirye 1313916
mock: document more alternatives to deprecated AnythingOfTypeArgument
dolmen ff43def
mock: simpler deprecation doc for AnythingOfTypeArgument
dolmen 4f3bd10
Generate better comments for require package
Neokil 64dc131
start implementing as RunWithReturn
jlou2u 3072693
update tests and all for RunWithReturn
jlou2u 2448386
Merge branch 'master' into feature/dynamic-return-values
jlou2u 9edc440
pr feedback - make sure to handle order of execution correctly
jlou2u f2ac7eb
go fmt
jlou2u 8231ec4
show test intent better
jlou2u ad9d9f3
Merge branch 'master' into feature/dynamic-return-values
jlou2u File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,6 +70,9 @@ type Call struct { | |
// decoders. | ||
RunFn func(Arguments) | ||
|
||
// Holds a handler to a function that will be called before returning. | ||
returnFn func(Arguments) Arguments | ||
|
||
// PanicMsg holds msg to be used to mock panic on the function call | ||
// if the PanicMsg is set to a non nil string the function call will panic | ||
// irrespective of other settings | ||
|
@@ -109,6 +112,7 @@ func (c *Call) Return(returnArguments ...interface{}) *Call { | |
defer c.unlock() | ||
|
||
c.ReturnArguments = returnArguments | ||
c.returnFn = nil | ||
|
||
return c | ||
} | ||
|
@@ -186,6 +190,19 @@ func (c *Call) Run(fn func(args Arguments)) *Call { | |
return c | ||
} | ||
|
||
// ReturnFn sets a handler to be called before returning. | ||
// | ||
// Mock.On("MyMethod", arg1, arg2).ReturnFn(func(args Arguments) Arguments { | ||
// return Arguments{args.Get(0) + args.Get(1)} | ||
// }) | ||
func (c *Call) ReturnFn(fn func(args Arguments) Arguments) *Call { | ||
c.lock() | ||
defer c.unlock() | ||
c.returnFn = fn | ||
c.ReturnArguments = nil | ||
return c | ||
} | ||
|
||
// Maybe allows the method call to be optional. Not calling an optional method | ||
// will not cause an error while asserting expectations | ||
func (c *Call) Maybe() *Call { | ||
|
@@ -583,6 +600,14 @@ func (m *Mock) MethodCalled(methodName string, arguments ...interface{}) Argumen | |
returnArgs := call.ReturnArguments | ||
m.mutex.Unlock() | ||
|
||
m.mutex.Lock() | ||
returnFn := call.returnFn | ||
m.mutex.Unlock() | ||
|
||
if returnFn != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because call.ReturnArguments is exported it should probably be checked by this condition rather than returnFn, eg: m = &myMock{}
m.On("Do").ReturnFn(func(args mock.Arguments) mock.Arguments { return mock.Arguments{"two"} })
m.ExpectedCalls[0].ReturnArguments = mock.Arguments{"one"}
assert.Equal(t, "one", m.Do()) ie. Call.ReturnArguments should always override Call.returnFn |
||
returnArgs = returnFn(arguments) | ||
} | ||
|
||
return returnArgs | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The example should be valid Go, did you mean Int rather than Get?