Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

ArgumentCaptor struct and tests#263

Closed
tylersammann wants to merge 22 commits intogolang:mainfrom
tylersammann:262-argument-captor-fork
Closed

ArgumentCaptor struct and tests#263
tylersammann wants to merge 22 commits intogolang:mainfrom
tylersammann:262-argument-captor-fork

Conversation

@tylersammann
Copy link
Copy Markdown

@tylersammann tylersammann commented Feb 7, 2019

Resolves Issue: #262

  • added ArgumentCaptor struct that composes Matcher
  • added constructors for ArgumentCaptor
  • included unit tests for ArgumentCaptor methods

The Captor() method takes a Matcher interface to make it as flexible as possible. AnyCaptor() is just there because it seems like the anyMatcher is probably going to be used with captors the most.

- added new ArguemntCaptor interface
- added argumentCaptor implementation that composes Matcher
- added constructors for ArgumentCaptor
- included unit tests for ArgumentCaptor methods
- added sample service and dao interface
- added unit test that demonstrates `AnyCaptor` usage
@poy poy self-assigned this Sep 27, 2019
@poy
Copy link
Copy Markdown
Collaborator

poy commented Sep 27, 2019

What is the advantage of this over just using .Do()? This adds quite a bit to the existing API.

@poy
Copy link
Copy Markdown
Collaborator

poy commented Sep 30, 2019

Also, I would like to say, I like the idea of gomock being able to generate "spies" in some way, but I want to ensure the API is solid.

@tylersammann
Copy link
Copy Markdown
Author

@poy gotcha. I wrote a little bit about using .Do() here. #262 (comment)
Mostly it just requires a lot of custom code to set up anything that stores arguments for later inspection, especially if you want to inspect multiple arguments. Also, in the multiple arg senario I find that it is nice to use the named captors rather than relying on argument order or re-implementing everything in the .Do() clause for each situation.

In terms of API change size, I think I'm only adding one 37 line file to the API itself. https://github.com/golang/mock/blob/bdfd0e24d230faaafd3c9d815aab7c1266ce9685/gomock/argument_captor.go The rest of the additions are testing or sample code related. (plus one .gitignore change that we can scrap if you don't want that)

I am happy to discuss changes to the API though if you can see places to improve it! My team has been using my forked ArgumentCaptors for the last six months with a lot of success and I'd love to see them added to the main branch.

@minicuts minicuts unassigned poy Jan 7, 2020
@codyoss codyoss requested a review from minicuts March 4, 2020 16:51
Copy link
Copy Markdown
Contributor

@minicuts minicuts left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great feature! Can you please add some godoc?

Comment thread gomock/argument_captor_test.go
Comment thread gomock/argument_captor.go Outdated
Comment thread gomock/argument_captor.go Outdated
Comment thread gomock/argument_captor.go Outdated
@minicuts
Copy link
Copy Markdown
Contributor

@codyoss the naming of ArgumentCaptor seems fine to me. Any other suggestions?

* Added GoDoc
* Removed the interface for ArgumentCaptor
* Renamed (and exported) the struct to ArgumentCaptor
* Renamed Value and AllValues to LastValue and Values respectively
* Updated unit tests
* added some small grammar changes to the docs for LastValue and Values methods
* Updated test names to reflect the changes to the method names LastValue and Values
@tylersammann tylersammann changed the title ArgumentCaptor interface and tests ArgumentCaptor struct and tests Mar 27, 2020
@codyoss
Copy link
Copy Markdown
Member

codyoss commented Mar 28, 2020

Thoughts on just calling it Captor vs ArgumentCaptor? The latter seems more Java like to me, but it is nice an explicit.

Comment thread gomock/argument_captor.go Outdated
Comment thread gomock/argument_captor.go
}

// AnyCaptor is a helper method that returns a new *ArgumentCaptor struct with the matcher set to an anyMatcher
func AnyCaptor() *ArgumentCaptor {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thoughts on CaptureAny()?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be ok with me if the call usually happened in the EXPECT line, but because captors aren't that useful unless they're declared in advance, I think it makes more sense to keep it as is and view it like a constructor.

Here's an example:

// here we don't have a way to grab the values
mockDao.EXPECT().InsertIDs(gomock.CaptureAny()).Times(1)

// we need to store the captor first 
idCaptor := gomock.AnyCaptor()
mockDao.EXPECT().InsertIDs(idCaptor).Times(1)
actualIDs := idCaptor.LastValue().([]int)

Comment thread sample/captor/service_test.go Outdated
Comment thread gomock/argument_captor.go
@codyoss
Copy link
Copy Markdown
Member

codyoss commented Mar 28, 2020

@tylersammann Thanks for working on this!

@tylersammann
Copy link
Copy Markdown
Author

@codyoss I like the name ArgumentCaptor because it is explicit. Captor isn't too bad though. I'm up to change it if others agree.

@tylersammann
Copy link
Copy Markdown
Author

@nguyenfilip @codyoss I think I've addressed most of the PR comments and change requests. Let me know how you feel re: Value() vs. LastValue() and I'll update accordingly.

@codyoss
Copy link
Copy Markdown
Member

codyoss commented Apr 8, 2020

Thanks, sorry for the delay. I will get back to review this by the end of the week.

@minicuts
Copy link
Copy Markdown
Contributor

@tylersammann I don't have any more comments. I will let @codyoss approve because there is stil the discussion open about naming.

minicuts
minicuts previously approved these changes Apr 16, 2020
@tylersammann
Copy link
Copy Markdown
Author

@nguyenfilip @codyoss this should be ready for another look. I've reverted the method names back to Value and AllValues

Copy link
Copy Markdown
Member

@codyoss codyoss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did a quick review mostly of the docs. Will come back and add some more this weekend.

Comment thread gomock/argument_captor.go Outdated
Comment thread gomock/argument_captor.go
Comment thread gomock/argument_captor.go Outdated
Comment thread gomock/argument_captor.go Outdated
Comment thread gomock/argument_captor.go
Comment thread gomock/argument_captor.go
Comment thread gomock/argument_captor_test.go Outdated
Comment thread mockgen/internal/tests/captor/dao.go
Comment thread gomock/argument_captor.go
// See the License for the specific language governing permissions and
// limitations under the License.

package gomock
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you add a couple examples of both a simple and more complex case using this as an end-user.

@codyoss
Copy link
Copy Markdown
Member

codyoss commented Jun 12, 2020

@tylersammann Are you still interested in this change?

@tylersammann
Copy link
Copy Markdown
Author

Yep definitely @codyoss. I can address your review this weekend

@codyoss
Copy link
Copy Markdown
Member

codyoss commented Jun 12, 2020

No worries, I still owe you another through review too. I apologize for not being more responsive here. These have been unprecedented times.

@tylersammann
Copy link
Copy Markdown
Author

I'll wait for you to review again before I push any changes. Thanks for looking @codyoss

@tylersammann
Copy link
Copy Markdown
Author

@codyoss I came back through and addressed your first review because I had some time. Feel free to take another look!

@hgl
Copy link
Copy Markdown

hgl commented Apr 16, 2021

Thanks everyone for the hard work.

Any progress on this PR?

@isopropylcyanide
Copy link
Copy Markdown

Thanks folks. Can we get this merged? This'd be a great feature add.

@tylersammann
Copy link
Copy Markdown
Author

I'm happy to continue on this. It'll need a re-review from @codyoss and it's been a while, so a few tests and a merge conflict needs fixing.

@codyoss
Copy link
Copy Markdown
Member

codyoss commented Jan 7, 2022

@tylersammann Apologies I dropped the ball on this PR. Let me take a look again here this week. One thing to consider for the future now is also generics with 1.18 coming soon. I would like to think about if generics might change surface. I will get back to you soon!

@tylersammann
Copy link
Copy Markdown
Author

@codyoss thanks! I agree, generics might remove the need for type casting captured values, and that would be a big win.

@codyoss
Copy link
Copy Markdown
Member

codyoss commented Jul 8, 2022

I think I am going to close this for now. I believe there might be a better surface design for this given generics now. Let's keep the chat going in the issue though.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants