forked from nathanjcochran/mock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
49 lines (44 loc) · 1.2 KB
/
template.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main
var tmpl = `package {{ .Package }}
import (
"sync/atomic"
{{- range .Imports }}
{{ . }}
{{- end }}
)
// {{ .Name }}Mock is a mock implementation of the {{ .Name }}
// interface.
type {{ .Name }}Mock{{ .TypeParams }} struct {
T *testing.T
{{- range .Methods }}
{{ .Name }}Stub func({{ .Params }}) {{ .Results }}
{{ .Name }}Called int32
{{- end }}
}
// Verify that *{{ .Name }}Mock implements {{ .Name }}.
{{- if .TypeParams }}
func _{{ .TypeParams }}() {
var _ {{ .Name }}{{ .TypeParams.Names }} = &{{ .Name }}Mock{{ .TypeParams.Names }}{}
}
{{ else }}
var _ {{ .Name }} = &{{ .Name }}Mock{}
{{ end }}
{{- range .Methods }}
// {{ .Name}} is a stub for the {{ $.Name }}.{{ .Name }}
// method that records the number of times it has been called.
func (m *{{ $.Name }}Mock{{ $.TypeParams.Names }}) {{ .Name }}({{ .Params.NamedString }}) {{ .Results }}{
atomic.AddInt32(&m.{{ .Name }}Called, 1)
if m.{{ .Name }}Stub == nil {
if m.T != nil {
m.T.Error("{{ .Name }}Stub is nil")
}
panic("{{ .Name }} unimplemented")
}
{{- if gt (len .Results) 0 }}
return m.{{ .Name }}Stub({{ .Params.ArgsString }})
{{- else }}
m.{{ .Name }}Stub({{ .Params.ArgsString }})
{{- end }}
}
{{- end -}}
`