gomock

command module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 16, 2020 License: MIT Imports: 10 Imported by: 0

README

gomock

Automatically generates mock code from a Go interface.

Installation
$ go get -v github.com/vibridi/gomock
$ cd $GOPATH/github.com/vibridi/gomock
$ make install
Requirements:

Go 1.11

Optionally: goimports, dep

Usage

Type gomock help for detailed usage tips.

In short, it supports the following flags:

$ -f FILE        Read go code from FILE
$ -o FILE        Output mock code to FILE
$ -i IDENTIFIER  Mock the interface with IDENTIFIER
$ -q             Qualify types with the package name
$ -x             Export 'with' and 'new' functions
$ -u             Output func signatures with unnamed parameters where possible
$ --help, -h     show help
$ --version, -v  print the version

The -u flag allows to output default functions and With* functions with unnamed arguments. The default behavior is to always output named arguments, as some IDEs reference them in code completion.

Features

This tool is able to resolve composed interfaces, however all declarations must live in the same directory or sub-directories relative to the main file. To see this in action, run make example-compose.

Example

To try out the tool after cloning the repo:

$ make build
$ ./build/gomock -f _example/_example.go

It will print out the generated mock code:

type mockTestInterface struct {
	options mockTestInterfaceOptions
}

type mockTestInterfaceOptions struct {
	funcGet  func() string
	funcSet  func(v string) 
	
}

var defaultMockTestInterfaceOptions = mockTestInterfaceOptions{
	funcGet: func() string {
		return ""
	},
	funcSet: func(string)  {
		return 
	},
	
}

type mockTestInterfaceOption func(*mockTestInterfaceOptions)


func withFuncGet(f func() string) mockTestInterfaceOption {
	return func(o *mockTestInterfaceOptions) {
		o.funcGet = f
	}
}

func withFuncSet(f func(string) ) mockTestInterfaceOption {
	return func(o *mockTestInterfaceOptions) {
		o.funcSet = f
	}
}



func (m *mockTestInterface) Get() string {
	return m.options.funcGet()
}

func (m *mockTestInterface) Set(v string)  {
	return 
}


func newMockTestInterface(opt ...mockTestInterfaceOption) TestInterface {
	opts := defaultMockTestInterfaceOptions
	for _, o := range opt {
		o(&opts)
	}
	return &mockTestInterface{
		options: opts,
	}
}

Then you can use the generated code in your unit tests:

myMock := newMockTestInterface(
    withFuncGet(f func() string {
        return "test-value"
    }),
)
myMock.Get() // "test-value"

objectThatUsesTestInterface := NewObject(myMock)
// ...

Authors

Currently there are no other contributors

TODOs

  • Make unnamed parameters optional in default and with* functions
  • Remove extra space between signature and { when the function has no return types

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
_example
sub

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL