https://github.com/leorolland/matchergen
Generate gomock matchers from your models
https://github.com/leorolland/matchergen
generator go gomock matcher testing tool
Last synced: 5 months ago
JSON representation
Generate gomock matchers from your models
- Host: GitHub
- URL: https://github.com/leorolland/matchergen
- Owner: leorolland
- Archived: true
- Created: 2023-10-28T15:32:24.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-11-06T19:49:22.000Z (over 2 years ago)
- Last Synced: 2024-06-19T16:40:34.189Z (about 2 years ago)
- Topics: generator, go, gomock, matcher, testing, tool
- Language: Go
- Homepage:
- Size: 7.81 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# gomock matcher generator
Project has been continued [here](https://github.com/Joffref/genz)
Generate [gomock matcher](https://pkg.go.dev/github.com/golang/mock/gomock#Matcher) implementations from your models.
```shell
go install github.com/leorolland/matchergen
```
## How it works
1. Run `matchergen -Type Human ./mypackage`
2. Generator searches for all accessor methods of type `Human`, they will be used for the match assertion.
4. Generator creates a `HumanMatcher` which implements [gomock Matcher](https://pkg.go.dev/github.com/golang/mock/gomock#Matcher) that can be instanciated with `NewHumanMatcher(expectedName, expectedAge)`. The matcher can be passed to gomocks `.EXPECT()` methods that expect a `Human` as a parameter.
5. gomock calls `Matches` on the generated matcher, which validates that all the accessors return the expected value (which are passed in `NewHumanMatcher`)
## Usage
1. Add `//go:generate matchergen` to your model:
(try in `/examples` directory)
```go
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
}
```
2. Run `go generate ./examples/model`
3. Use generated modeltest
Generated file:
```go
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
```go
humanServiceMock = NewHumanServiceMock(gomock.NewController())
...
humanServiceMock.EXPECT().CreateHuman(NewHumanMatcher("joe", 30)).Return(nil)
```