91 lines
2.6 KiB
Go
91 lines
2.6 KiB
Go
package moq
|
|
|
|
// moqImports are the imports all moq files get.
|
|
var moqImports = []string{"sync"}
|
|
|
|
// moqTemplate is the template for mocked code.
|
|
var moqTemplate = `package {{.PackageName}}
|
|
|
|
// AUTOGENERATED BY MOQ - DO NOT EDIT
|
|
// github.com/matryer/moq
|
|
|
|
import (
|
|
{{- range .Imports }}
|
|
"{{.}}"
|
|
{{- end }}
|
|
)
|
|
|
|
{{ range $i, $obj := .Objects -}}
|
|
// These locks allow you to use the mocks in a safe way
|
|
// in concurrent code.
|
|
var (
|
|
{{- range .Methods }}
|
|
lock{{$obj.InterfaceName}}Mock{{.Name}} sync.Mutex
|
|
{{- end }}
|
|
)
|
|
|
|
// {{.InterfaceName}}Mock is a mock implementation of {{.InterfaceName}}.
|
|
//
|
|
// func TestSomethingThatUses{{.InterfaceName}}(t *testing.T) {
|
|
//
|
|
// // make and configure a mocked {{.InterfaceName}}
|
|
// mocked{{.InterfaceName}} := &{{.InterfaceName}}Mock{ {{ range .Methods }}
|
|
// {{.Name}}Func: func({{ .Arglist }}) {{.ReturnArglist}} {
|
|
// panic("TODO: mock out the {{.Name}} method")
|
|
// },{{- end }}
|
|
// }
|
|
//
|
|
// // TODO: use mocked{{.InterfaceName}} in code that requires {{.InterfaceName}}
|
|
// // and then make assertions.
|
|
// //
|
|
// // Use the CallsTo structure to access details about what calls were made:
|
|
// //
|
|
// // if len(mocked{{.InterfaceName}}.CallsTo.MethodFunc) != 1 {
|
|
// // t.Errorf("expected 1 call there were %d", len(mocked{{.InterfaceName}}.CallsTo.MethodFunc))
|
|
// // }
|
|
//
|
|
// }
|
|
type {{.InterfaceName}}Mock struct {
|
|
{{- range .Methods }}
|
|
// {{.Name}}Func mocks the {{.Name}} method.
|
|
{{.Name}}Func func({{ .Arglist }}) {{.ReturnArglist}}
|
|
{{ end }}
|
|
// CallsTo tracks calls to the methods.
|
|
CallsTo struct {
|
|
{{- range .Methods }}
|
|
// {{ .Name }} holds details about calls to the {{.Name}} method.
|
|
{{ .Name }} []struct {
|
|
{{- range .Params }}
|
|
// {{ .Name | Exported }} is the {{ .Name }} argument value.
|
|
{{ .Name | Exported }} {{ .Type }}
|
|
{{- end }}
|
|
}
|
|
{{- end }}
|
|
}
|
|
}
|
|
{{ range .Methods }}
|
|
// {{.Name}} calls {{.Name}}Func.
|
|
func (mock *{{$obj.InterfaceName}}Mock) {{.Name}}({{.Arglist}}) {{.ReturnArglist}} {
|
|
if mock.{{.Name}}Func == nil {
|
|
panic("moq: {{$obj.InterfaceName}}Mock.{{.Name}}Func is nil but was just called")
|
|
}
|
|
lock{{$obj.InterfaceName}}Mock{{.Name}}.Lock()
|
|
mock.CallsTo.{{.Name}} = append(mock.CallsTo.{{.Name}}, struct{
|
|
{{- range .Params }}
|
|
{{ .Name | Exported }} {{ .Type }}
|
|
{{- end }}
|
|
}{
|
|
{{- range .Params }}
|
|
{{ .Name | Exported }}: {{ .Name }},
|
|
{{- end }}
|
|
})
|
|
lock{{$obj.InterfaceName}}Mock{{.Name}}.Unlock()
|
|
{{- if .ReturnArglist }}
|
|
return mock.{{.Name}}Func({{.ArgCallList}})
|
|
{{- else }}
|
|
mock.{{.Name}}Func({{.ArgCallList}})
|
|
{{- end }}
|
|
}
|
|
{{ end -}}
|
|
{{ end -}}`
|