Skip to content

Commit

Permalink
Merge pull request #30 from kinbiko/next
Browse files Browse the repository at this point in the history
  • Loading branch information
kinbiko authored Aug 7, 2020
2 parents 241fb80 + feaaec9 commit 4db948f
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 25 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,27 @@ func RegisterUser(userName string, repo UserRepository) {
}
```

### 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:

```go
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](https://pkg.go.dev/github.com/kinbiko/mokku?tab=doc) 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.
Expand Down
37 changes: 36 additions & 1 deletion cmd/mokku/mokku.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"

"github.com/atotto/clipboard"
"github.com/kinbiko/mokku"
Expand All @@ -14,16 +16,37 @@ const usage = `Usage:
2. Run 'mokku'
3. Paste the mocked implementation that has been written to your clipboard`

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 }}`

func main() {
flag.Usage = func() { fmt.Println(usage) }
flag.Parse()

templateStr, err := loadTemplate(os.Getenv("MOKKU_TEMPLATE_PATH"))
if err != nil {
errorOut(err)
}
if templateStr == "" {
templateStr = defaultTemplate
}

s, err := clipboard.ReadAll()
if err != nil {
errorOut(err)
}

mock, err := mokku.Mock(mokku.Config{}, []byte(s))
mock, err := mokku.Mock(mokku.Config{TemplateStr: templateStr}, []byte(s))
if err != nil {
errorOut(err)
}
Expand All @@ -33,6 +56,18 @@ func main() {
}
}

// loadTemplate loads template string from filePath, if there is one.
func loadTemplate(filePath string) (string, error) {
if filePath == "" { // There may not be an external template path given
return "", nil
}
content, err := ioutil.ReadFile(filepath.Clean(filePath))
if err != nil {
return "", fmt.Errorf("failed to read file %s: %v", filePath, err)
}
return string(content), nil
}

func errorOut(err error) {
fmt.Fprintln(os.Stderr, err.Error())
flag.Usage()
Expand Down
20 changes: 13 additions & 7 deletions mokku.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,28 @@ package mokku

import "go/format"

// Config is currently ignored but is expected to contain various configuration
// options for this tool given by user-provided flags in the future.
// Config is defines all configuration options for mokku.
// In particular, this package treats additions to this struct as *non-breaking* changes.
type Config struct {
// Intentionally empty at the moment.
// Included only to avoid breaking backwards compatibility if a newer
// version of the package supports new features
// 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
}

// Mock creates the sourcecode of a mock implementation of the interface
// sourcecode defined in the given byte array.
func Mock(_ Config, src []byte) ([]byte, error) {
func Mock(config Config, src []byte) ([]byte, error) {
target, err := newParser(src).parse()
if err != nil {
return nil, err
}
mft, err := mockFromTemplate(target)
mft, err := mockFromTemplate(target, config.TemplateStr)
if err != nil {
return nil, err
}
Expand Down
15 changes: 14 additions & 1 deletion mokku_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,20 @@ import (
)

func TestIntegration(t *testing.T) {
got, err := mokku.Mock(mokku.Config{}, []byte(`
const templateStr = `
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 }}`

got, err := mokku.Mock(mokku.Config{TemplateStr: templateStr}, []byte(`
type Foo interface {
Act()
}`))
Expand Down
17 changes: 2 additions & 15 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,8 @@ import (
"text/template"
)

const tpl = `
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 }}`

func mockFromTemplate(defn *targetInterface) ([]byte, error) {
tmpl, err := template.New("mock").Parse(tpl)
func mockFromTemplate(defn *targetInterface, templateStr string) ([]byte, error) {
tmpl, err := template.New("mock").Parse(templateStr)
if err != nil {
return nil, err
}
Expand Down
15 changes: 14 additions & 1 deletion template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@ import (
)

func TestTemplate(t *testing.T) {
const templateStr = `
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 }}`

for _, tc := range []struct {
name string
in *targetInterface
Expand Down Expand Up @@ -59,7 +72,7 @@ func (m *FooBarMock) NoReturnParam( a string ) {
},
} {
t.Run(tc.name, func(t *testing.T) {
b, err := mockFromTemplate(tc.in)
b, err := mockFromTemplate(tc.in, templateStr)
if err != nil {
t.Fatalf("unexpected error: %s", err.Error())
}
Expand Down

0 comments on commit 4db948f

Please sign in to comment.