mokku

package module
Version: v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2020 License: MIT Imports: 8 Imported by: 0

README

logo

Build Status Go Report Card Latest version Go Documentation License

A clipboard-based mocking framework for Go that gets out of your way.

This tool has been built with inspiration lovingly taken from Moq, and fuelled by the frustration of using gomock.

Key ideas:

  • Invisible: No need to introduce dependencies or //go:generate directives in your codebase.
  • Integration friendly: Easy to integrate in your workflow, regardless of editor.
  • File/package agnostic: mokku doesn't care where exactly your code lives on your system or even whether it compiles yet.

Installation

$ go get github.com/kinbiko/mokku/cmd/mokku

Usage

# Copy the interface
$ mokku
# Paste the mock

That's it. Below is an example of how to use mokku within vim.

demo

Using the generated code

If you have been writing your own mocks or if you're familiar with Moq then the usage will look very familiar to you:

func TestRegisterUser(t *testing.T) {
	var (
		got = ""
		exp = "kinbiko"
	)

	mock := &UserRepositoryMock{
		CreateFunc: func(userName string) error {
			got = userName
			return nil
		},
	}

	RegisterUser(exp, mock)

	if got != exp {
		t.Errorf("expected user name '%s' but got '%s'", exp, got)
	}
}

func RegisterUser(userName string, repo UserRepository) {
	// ... Code that includes a call to 'repo.Create(userName)'
}

Defining Custom Templates

You can also define your own custom template to use for your mocks, by defining MOKKU_TEMPLATE_PATH that is the path to a file containing a Go text template, that uses the same variables found in the default template:

const defaultTemplate = `
type {{.TypeName}}Mock struct { {{ range .Methods }}
	{{.Name}}Func func{{.Signature}}{{ end }}
}
{{if .Methods }}{{$typeName := .TypeName}}
{{range $val := .Methods}}func (m *{{$typeName}}Mock) {{$val.Name}}{{$val.Signature}} {
	if m.{{$val.Name}}Func == nil {
		panic("unexpected call to {{$val.Name}}")
	}
	{{if $val.HasReturn}}return {{ end }}m.{{$val.Name}}Func{{$val.OrderedParams}}
}
{{ end }}{{ end }}`

See the GoDocs for a more detailed explanation of the template variables.

Contributing

Please raise an issue to discuss any changes you'd like to make to this project. If you wish to contribute, but need some ideas, please check out the GitHub project for this repository.

Gratitude

This project is has been made possible thanks to my employer who let me work on this project during Mercari Hack Week.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Mock

func Mock(config Config, src []byte) ([]byte, error)

Mock creates the sourcecode of a mock implementation of the interface sourcecode defined in the given byte array.

Types

type Config

type Config struct {
	// TemplateStr is mock template string.
	// The template will attempt to fill in the following fields:
	//  TypeName -- string. E.g. "MyInterface"
	//	Methods  -- composite type containing:
	//		Name			-- string. E.g. "DoStuff"
	//		Signature		-- string. E.g. "(a, b int) error"
	//		OrderedParams	-- string. E.g. "(a, b)"
	//		HasReturn		-- bool. Identifies whether or not the mocked method should return anything.
	TemplateStr string
}

Config is defines all configuration options for mokku. In particular, this package treats additions to this struct as *non-breaking* changes.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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