Skip to content

mock: cleanup argument matching API #1578

@dolmen

Description

@dolmen

Description

The mock package exposes public types that should have stayed implementation details:

With #1441 a mitigation effort has been started by deprecating AnythingOfTypeArgument (released since v1.9.0). Unfortunately that mitigation can't be applied to the other cases.

Proposed solution

I propose to define the following private (can't be implemented outside of the package) interface:

type ArgumentMatcher interface {
    matchesArg(arg interface{}) bool
}

and then to retrofit the types and functions to use that interface:

const Anything = anythingArgument{}

type anythingArgument struct{}

func (anythingArgument) matchesArg(interface{}) bool {
     return true
}


func AnythingOfType(string) ArgumentMatcher

type AnythingOfTypeArgument = anythingOfTypeArgument

type anythingOfTypeArgument string

func (anythingOfTypeArgument) matchesArg(interface{}) bool


func IsType(interface{}) ArgumentMatcher

type isTypeArgument struct {
    T reflect.Type
}

func (*isTypeArgument) matchesArg(interface{}) bool


func MatchedBy(fn interface{}) ArgumentMatcher

type matchedByArgument struct {
    F reflect.Value
}

func (*matchedByArgument) matchesArg(interface{}) bool


func FunctionalOptions(value ...interface{}) ArgumentMatcher

type functionalOptionsArgument struct {
	values []interface{}
}

func (*functionalOptionsArgument) matchesArg(interface{}) bool

Method Arguments.Diff will be rewritten (simplified) by using the ArgumentsMatcher interface (instead of the type switch that directly refer to each type).

I think that this plan can be implemented without waiting for a v2 as the proposed changes would not affect correct uses of the existing API.

Misc

This ticket will also track effort to provide fixes to downstream projects which have strong references to those mock implementation details:

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions