8000 Field Type Matcher · Issue #237 · uber-go/mock · GitHub
[go: up one dir, main page]

Skip to content

Field Type Matcher #237

Open
Open

Description

@denizgursoy

Is it possible to add a matcher per type and use it? Whenever I use decimal.Decimal from github.com/shopspring/decimal as a field in in one of my structs, my mocks fail because precision in the type.

type MyType struct {
        Field1 string
	Field2 string
	Field3 decimal.Decimal
}
func Test_MyType(t *testing.T) {
	type MyType struct {
		Field1 string
		Field2 string
		Field3 decimal.Decimal
	}

	controller := gomock.NewController(t)
	client := NewMock(controller)
	client.EXPECT().MyFunction(gomock.Any(), MyType{}).Return().Times(1) // this fails
}

Is there a way to match these types in gomock?

If no, is there a way to extend gomock to match by specfic type in this case it is decimal.Decimal instead of writing a matcher for whole MyType struct

As a POC I did something like this:

func Test_MyType(t *testing.T) {
	type MyType struct {
		Field1 string
		Field2 string
		Field3 decimal.Decimal
	}

	decimalMatcher := DecimalMatcher{}
	controller := gomock.NewController(t, gomock.WithFieldMatcher[decimal.Decimal](decimalMatcher))
	client := NewMock(controller)
	client.EXPECT().MyFunction(gomock.Any(), MyType{}).Return().Times(1)
	fmt.Println(client)
}

type DecimalMatcher struct {
}

func (d DecimalMatcher) Matches(x any) bool {
	return true
}

func (d DecimalMatcher) String() string {
	return "foo"
}

This is for extending go mock whenever it sees a ``decimal.Decimal` type, it uses the custom matcher given instead of writing a new matcher for whole struct

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0