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

add Len matcher #368

Merged
merged 1 commit into from
Dec 23, 2019
Merged

add Len matcher #368

merged 1 commit into from
Dec 23, 2019

Conversation

codyoss
Copy link
Member

@codyoss codyoss commented Dec 20, 2019

This matcher will check the length with any type that allows this
behavior via reflection. Implementation inspired by #189.

This matcher will check the length with any type that allows this
behavior via reflection. Implementation inspired by #189.
@codyoss codyoss requested review from cvgw and minicuts December 20, 2019 21:24
func (m lenMatcher) Matches(x interface{}) bool {
v := reflect.ValueOf(x)
switch v.Kind() {
case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice, reflect.String:
Copy link
Collaborator

Choose a reason for hiding this comment

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

curious, does this cover *[]string and []string ?

Copy link
Member Author

Choose a reason for hiding this comment

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

This would not, do you think we should support that?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think it's necessarily needed. If it supports []string but not *[]string the user can just dereference.

@@ -192,6 +210,12 @@ func Any() Matcher { return anyMatcher{} }
// Eq(5).Matches(4) // returns false
func Eq(x interface{}) Matcher { return eqMatcher{x} }

// Len returns a matcher that matches on length. This matcher returns false if
// is compared to a type that is not an array, chan, map, slice, or string.
func Len(i int) Matcher {
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: I'm imagining mockFoo.EXPECT().Bar(gomock.Len(4)) which feels like it could be more natural but I can't think of a better alternative.

@codyoss codyoss merged commit b4b7d21 into golang:master Dec 23, 2019
@codyoss codyoss deleted the len-matcher branch December 23, 2019 16:01
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.

2 participants