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

Add builtin custom matcher function #695

Open
ilya-hontarau opened this issue Feb 18, 2023 · 1 comment
Open

Add builtin custom matcher function #695

ilya-hontarau opened this issue Feb 18, 2023 · 1 comment

Comments

@ilya-hontarau
Copy link

Requested feature
Add builtin custom matcher function

Why the feature is needed
Currently, we should implement Matcher interface in our own code, and it can be useful to provide the ability to create custom matcher using a function in the lib.

Proposed solution
We can create a constructor function, that accepts fmt.Stringer, a match function and returns a new matcher.

	mockMatcher.EXPECT().Matches(gomock.Custom(
		gomock.StringerFunc(func() string {
			return `expect string contains"test"`
		}),
		func(i interface{}) bool {
			v, ok := i.(string)
			if !ok {
				return false
			}
			return strings.Contains(v, "test")
		},
	)).Return(true)
@ichekrygin
Copy link

ichekrygin commented Mar 16, 2023

With generics support, it could be something as simple as:
[NOT A CONTRIBUTION] 😮‍💨

type CustomMatcher[T any] struct {
	value T
}
func (c *CustomMatcher[T]) Matches(value interface{}) bool {
	return reflect.DeepEqual(c.value, value)
}
func (c *CustomMatcher[T]) String() string {
	return fmt.Sprintf("%v", c.value)
}

func Custom[T any](value T) gomock.Matcher {
	return &CustomMatcher[T]{
		value: value,
	}
}

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

No branches or pull requests

2 participants