Project has been continued here
Generate gomock matcher implementations from your models.
go install github.com/leorolland/matchergen
- Run
matchergen -Type Human ./mypackage
- Generator searches for all accessor methods of type
Human
, they will be used for the match assertion. - Generator creates a
HumanMatcher
which implements gomock Matcher that can be instanciated withNewHumanMatcher(expectedName, expectedAge)
. The matcher can be passed to gomocks.EXPECT()
methods that expect aHuman
as a parameter. - gomock calls
Matches
on the generated matcher, which validates that all the accessors return the expected value (which are passed inNewHumanMatcher
)
- Add
//go:generate matchergen
to your model: (try in/examples
directory)
package model
//go:generate matchergen -type Human -output ../modeltest/human_matcher.go .
type Human struct {
name string
age int
}
func NewHuman(name string, age int) *Human {
return &Human{name, age}
}
func (h *Human) Name() string {
return h.name
}
func (h *Human) Age() int {
return h.age
}
-
Run
go generate ./examples/model
-
Use generated modeltest Generated file:
type HumanMatcher struct {
comparator *comparator
name string
age int
}
func NewHumanMatcher(name string, age int) *HumanMatcher {
return &HumanMatcher{
name: name,
age: age,
}
}
func (h *HumanMatcher) Matches(arg interface{}) bool {
h.comparator = &comparator{}
human, ok := arg.(*model.Human)
if !ok {
h.comparator.equal("type", "*model.Human", fmt.Sprintf("%T", arg))
return h.comparator.matches()
}
h.comparator.equal("name", h.name, human.Name())
h.comparator.equal("age", h.age, human.Age())
return h.comparator.matches()
}
func (h *HumanMatcher) Got(got interface{}) string {
return getDiff(h.comparator.got)
}
func (h *HumanMatcher) String() string {
return getValue(h.comparator.wanted)
}
Usge in tests
humanServiceMock = NewHumanServiceMock(gomock.NewController())
...
humanServiceMock.EXPECT().CreateHuman(NewHumanMatcher("joe", 30)).Return(nil)