Giter Site home page Giter Site logo

go-mockgen-tool's Introduction

go-mockgen-tool

go-mockgen-tool is a package that creates mock object definitions based on an interface.

It uses code generation to generate a mock with solid types (no reflection or interface{} needed!). The resulting mock type has properties that you can fill in to determine how the function behaves.

Installation with: go get github.com/jamesrr39/go-mockgen-tool.

You can create a mock by using the go:generate go-mockgen-tool --type <my type name>, or simply by running the go-mockgen-tool inside the package directory. You must specify the name of the type you want to create a definition for.

With an interface with the following definition:

//go:generate go-mockgen-tool --type Vehicle
type Vehicle interface {
	WheelCount() (int, error)
	GetDriveFunc() func() error
}

go-mockgen-tool generates the following code:

type MockVehicle struct {
	WheelCountFunc   func() (int, error) 
	GetDriveFuncFunc func() func() error 
}

func (o *MockVehicle) WheelCount() (int, error) {
	if o.WheelCountFunc == nil {
		panic("WheelCountFunc not defined")
	}
	return o.WheelCountFunc()
}

func (o *MockVehicle) GetDriveFunc() func() error {
	if o.GetDriveFuncFunc == nil {
		panic("GetDriveFuncFunc not defined")
	}
	return o.GetDriveFuncFunc()
}

The "Mock" struct can then be supplied with custom functions to return the objects you wish during test runs (this tool is intended for use in tests, but not limited to use in tests).

For a more detailed example, see the example folder. It shows an interface with a variety of different return types, embedded interfaces etc, and the generated code.

Features supported:

  • Functions with and without parameters and return types
  • Functions with more complex parameters and return types, e.g. functions that return functions
  • Embedded interfaces both in the same package and different packages
  • Package aliasing

This probably don't support every way to declare an interface. If you find something that doesn't work, but is valid Go, please open an issue.

Related projects

go-mockgen-tool's People

Contributors

jamesrr39 avatar

Watchers

James Cloos avatar  avatar

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.