Code Monkey home page Code Monkey logo

matchergen's Introduction

gomock matcher generator

Project has been continued here

Generate gomock matcher implementations from your models.

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.
  3. Generator creates a HumanMatcher which implements 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.
  4. 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)
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
}
  1. Run go generate ./examples/model

  2. 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)

matchergen's People

Contributors

leorolland avatar

Watchers

 avatar

matchergen's Issues

match value semantic

currently, matcher only allows to match objects by pointer
I want to be able (if I configure it to) to generate a matcher by value

optionals

I want to be able to specify a set of attributes that are optional (If I do not pass them in the constructor, they will not be checked by the matcher)

package importation

add imports in generated matcher, being able to import user's specific packages

slices

handle slices in matchable elements
that will

  • compare length of expected and got slice
  • assert elements one-by-one

customize template

I want to be able to use my own matcher generation template via mustache/go-template.

matcher in matcher

For nested structures, I want to be able to create a matcher that takes another matcher (for the nested structure) as a parameter.
Example

//go:generate matchergen... -type A .
type A struct {
 b B
}
type B struct {}
...
// generates
func NewAMatcher(b BMatcher)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.