Giter VIP home page Giter VIP logo

mokku's Introduction

logo

Build Status Coverage Status Go Report Card Latest version Go Documentation License

Because you don't want to study the details of yet another framework, mokku is 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.

mokku's People

Contributors

cloverrose avatar kinbiko avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

mokku's Issues

GitHub templates

ISSUE_TEMPLATE.md

Should provide a template for how bug reports should be structured.
Should be as small as possible but include the following:

  • Indication that the template is for bug reports only, and can be ignored for other types of issues (e.g. ideas, missing docs etc.).
  • H2 headers for 'What I did', 'What I expected', 'What happened', 'Suggestions'. Suggestions are optional.
  • Whether or not you're interested in addressing the bug yourself.
  • HTML comments containing instructions for adding labels.

PULL_REQUEST_TEMPLATE.md

Should include a reminder to create/show interest in an issue before raising a pull request, and add a link to the issue within the pull request description.

Code changes

Once the issue template has been set up then we should include a link to create a new issue in the error messages in the case of a failed mock generation.

Missing parameter names in interface methods

Did

Copied the following struct:

type foobar interface {
    baz(context.Context, int)
}

and ran mokku, and pasted the result.

Expected

A mock struct that compiles.

Got

A compile error, because the resulting mocked method attempts to pass context as a parameter to the underlying Func.

Suggestion

In the case that parameters are not named, we should create a dummy variable name.
a,b, etc is probably fine for unnamed parameters. If users have more than 26 variable names then, well... there are bigger design issues at hand.

Improve error messages

Right now the error messages are directed to the developer and not the end user. The error messages should be re-written to be actionable by the user.

Repo cleanup and consistency

Setting up the repository

This document serves as both a list of all the rules and steps to set up a very good open source Go project, and also the issue contents of the GitHub issue generated by github.com/kinbiko/go-template's astroturf.sh script that automates a bunch of these very steps.

CI

  • Builds against one of the 2 latest Go versions
  • Has golangci-lint v1.45.2 passing
  • Runs tests with the race detector
  • Runs integration tests, if applicable
  • Reports coverage to coveralls
  • Code scanning with CodeQL

Community Standards

  • Description w/ tags
  • README
  • Code of conduct
  • Contributing guideline
  • License
  • Issue template(s)
  • Pull request template
  • CODEOWNERS

GitHub settings

  • Wiki pages disabled
  • Sponsorship enabled
  • Projects disabled (my OSS work is managed in a central project board)
  • GitHub archive program enabled
  • Discussions disabled
  • Merge commits disabled
  • Squash merging enabled
  • Rebase merging disabled
  • Suggest updating branches enabled
  • Auto-merge disabled
  • Auto-delete head branches enabled
  • Limit "approve" or "request changes" to users with explicit permissions
  • Default branch is main, with the following branch protection rules enabled (everything else disabled):
    • Require PRs before merging (no approvals required, don't dismiss PR approvals on new commits, don't require codeowner review)
    • Linear history enabled (assumption: only enabling squash merges globally means linear history in default branch)
  • Allow kinbiko and select non-kinbiko actions only:
    • Allow actions created by GitHub enabled
    • Allow actions by Marketplace verified creators enabled
    • shogo82148/actions-goveralls@v1 added as allowed action
  • Require approval before running actions for first-time contributors
  • GitHub actions approving PRs disabled
  • Dependabot alerts enabled for security vulns
  • Dependabot updates enabled for security vulns
  • Code scanning (CodeQL) enabled
  • Consistent labels:
    • bug
    • dependencies
    • documentation
    • enhancement
    • good first issue
    • help wanted
    • question

README

  • Has tags:
    • Build status
    • Coverage percentage
    • Go report link
    • Latest version
    • Godoc with link to pkg.go.dev
    • License tag
  • Explains the why and then the what.
  • Usage information incl any installation guidelines
  • Link to docs

Report Coverage

  • See if there's an easy-to-use github action for this, otherwise Integrate with coveralls
  • Add badge to README

Document how to integrate with Goland

Document (in the README) how to integrate mokku with goland.
If there's an even more convenient method of shelling out and copying, e.g. binding shelling out to run 'mokku' to a key binding, then that's preferable.

List on Go awesome

  • Request to be added to go awesome
  • Put go awesome badge on README

Depends on reporting coverage

Document how to integrate with vim

Document the key binding to put in your .vimrc that is required to put the mock representation of the interface under the cursor in the default register by hitting (for example) <leader>m.

trailing comma causes error

This Add method has trailing comma in parameters part.

type Calculator interface {
	Add(a int,
		b int,
	) int
}

It causes error like this.

10:30: expected statement, found ')' (and 1 more errors)
Usage:
1. Copy the interface you want to mock
2. Run 'mokku'
3. Paste the mocked implementation that has been written to your clipboard

Pointer return values are missing the *

Given:

type O11y interface {
	Wrap(ctx context.Context, err error, args ...interface{}) *bugsnag.Error
}

Got:

type O11yMock struct {
	WrapFunc     func(ctx context.Context, err error, args ...interface{}) bugsnag.Error
}

func (m *O11yMock) Wrap(ctx context.Context, err error, args ...interface{}) bugsnag.Error {
	if m.WrapFunc == nil {
		panic("unexpected call to Wrap")
	}
	return m.WrapFunc(ctx, err, args...)
}

the * in *bugsnag.Error is missing.

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.