improved the way calls worked
This commit is contained in:
parent
09e12df472
commit
000d593524
@ -95,6 +95,7 @@ The mocked structure implements the interface, where each method calls the assoc
|
|||||||
* Keep mocked logic inside the test that is using it
|
* Keep mocked logic inside the test that is using it
|
||||||
* Only mock the fields you need
|
* Only mock the fields you need
|
||||||
* It will panic if a nil function gets called
|
* It will panic if a nil function gets called
|
||||||
|
* Name arguments in the interface for a better experience
|
||||||
* Use closured variables inside your test function to capture details about the calls to the methods
|
* Use closured variables inside your test function to capture details about the calls to the methods
|
||||||
* Use `.CallsTo.Method` to track the calls
|
* Use `.CallsTo.Method` to track the calls
|
||||||
* Use `go:generate` to invoke the `moq` command
|
* Use `go:generate` to invoke the `moq` command
|
||||||
|
@ -47,7 +47,7 @@ func New(src, packageName string) (*Mocker, error) {
|
|||||||
if len(packageName) == 0 {
|
if len(packageName) == 0 {
|
||||||
return nil, errors.New("failed to determine package name")
|
return nil, errors.New("failed to determine package name")
|
||||||
}
|
}
|
||||||
tmpl, err := template.New("moq").Parse(moqTemplate)
|
tmpl, err := template.New("moq").Funcs(templateFuncs).Parse(moqTemplate)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -214,6 +214,15 @@ func (p param) TypeString() string {
|
|||||||
return p.Type
|
return p.Type
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var templateFuncs = template.FuncMap{
|
||||||
|
"Exported": func(s string) string {
|
||||||
|
if s == "" {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return strings.ToUpper(s[0:1]) + s[1:]
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
// moqImports are the imports all moq files get.
|
// moqImports are the imports all moq files get.
|
||||||
var moqImports = []string{"sync"}
|
var moqImports = []string{"sync"}
|
||||||
|
|
||||||
@ -252,10 +261,14 @@ type {{.InterfaceName}}Mock struct {
|
|||||||
// CallsTo gets counters for each of the methods indicating
|
// CallsTo gets counters for each of the methods indicating
|
||||||
// how many times each one was called.
|
// how many times each one was called.
|
||||||
CallsTo struct {
|
CallsTo struct {
|
||||||
lock sync.Mutex
|
|
||||||
{{- range .Methods }}
|
{{- range .Methods }}
|
||||||
// {{ .Name }} holds the number of calls to the {{.Name}} method.
|
lock{{.Name}} sync.Mutex // protects {{ .Name }}
|
||||||
{{ .Name }} int
|
// {{ .Name }} holds details about calls to the {{.Name}} method.
|
||||||
|
{{ .Name }} []struct {
|
||||||
|
{{- range .Params }}
|
||||||
|
{{ .Name | Exported }} {{ .Type }}
|
||||||
|
{{- end }}
|
||||||
|
}
|
||||||
{{- end }}
|
{{- end }}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -265,9 +278,17 @@ func (mock *{{$obj.InterfaceName}}Mock) {{.Name}}({{.Arglist}}) {{.ReturnArglist
|
|||||||
if mock.{{.Name}}Func == nil {
|
if mock.{{.Name}}Func == nil {
|
||||||
panic("moq: {{$obj.InterfaceName}}Mock.{{.Name}}Func is nil but was just called")
|
panic("moq: {{$obj.InterfaceName}}Mock.{{.Name}}Func is nil but was just called")
|
||||||
}
|
}
|
||||||
mock.CallsTo.lock.Lock()
|
mock.CallsTo.lock{{.Name}}.Lock()
|
||||||
mock.CallsTo.{{.Name}}++
|
mock.CallsTo.{{.Name}} = append(mock.CallsTo.{{.Name}}, struct{
|
||||||
mock.CallsTo.lock.Unlock()
|
{{- range .Params }}
|
||||||
|
{{ .Name | Exported }} {{ .Type }}
|
||||||
|
{{- end }}
|
||||||
|
}{
|
||||||
|
{{- range .Params }}
|
||||||
|
{{ .Name | Exported }}: {{ .Name }},
|
||||||
|
{{- end }}
|
||||||
|
})
|
||||||
|
mock.CallsTo.lock{{.Name}}.Unlock()
|
||||||
{{- if .ReturnArglist }}
|
{{- if .ReturnArglist }}
|
||||||
return mock.{{.Name}}Func({{.ArgCallList}})
|
return mock.{{.Name}}Func({{.ArgCallList}})
|
||||||
{{- else }}
|
{{- else }}
|
||||||
|
@ -10,7 +10,7 @@ import (
|
|||||||
func TestMoq(t *testing.T) {
|
func TestMoq(t *testing.T) {
|
||||||
m, err := New("testdata/example", "")
|
m, err := New("testdata/example", "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("moq.New: %s", err)
|
t.Fatalf("moq.New: %s", err)
|
||||||
}
|
}
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
err = m.Mock(&buf, "PersonStore")
|
err = m.Mock(&buf, "PersonStore")
|
||||||
@ -43,7 +43,7 @@ func TestMoq(t *testing.T) {
|
|||||||
func TestMoqExplicitPackage(t *testing.T) {
|
func TestMoqExplicitPackage(t *testing.T) {
|
||||||
m, err := New("testdata/example", "different")
|
m, err := New("testdata/example", "different")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("moq.New: %s", err)
|
t.Fatalf("moq.New: %s", err)
|
||||||
}
|
}
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
err = m.Mock(&buf, "PersonStore")
|
err = m.Mock(&buf, "PersonStore")
|
||||||
@ -73,7 +73,7 @@ func TestMoqExplicitPackage(t *testing.T) {
|
|||||||
func TestVariadicArguments(t *testing.T) {
|
func TestVariadicArguments(t *testing.T) {
|
||||||
m, err := New("testdata/variadic", "")
|
m, err := New("testdata/variadic", "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("moq.New: %s", err)
|
t.Fatalf("moq.New: %s", err)
|
||||||
}
|
}
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
err = m.Mock(&buf, "Greeter")
|
err = m.Mock(&buf, "Greeter")
|
||||||
@ -98,7 +98,7 @@ func TestVariadicArguments(t *testing.T) {
|
|||||||
func TestNothingToReturn(t *testing.T) {
|
func TestNothingToReturn(t *testing.T) {
|
||||||
m, err := New("testdata/example", "")
|
m, err := New("testdata/example", "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("moq.New: %s", err)
|
t.Fatalf("moq.New: %s", err)
|
||||||
}
|
}
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
err = m.Mock(&buf, "PersonStore")
|
err = m.Mock(&buf, "PersonStore")
|
||||||
@ -123,7 +123,7 @@ func TestNothingToReturn(t *testing.T) {
|
|||||||
func TestChannelNames(t *testing.T) {
|
func TestChannelNames(t *testing.T) {
|
||||||
m, err := New("testdata/channels", "")
|
m, err := New("testdata/channels", "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("moq.New: %s", err)
|
t.Fatalf("moq.New: %s", err)
|
||||||
}
|
}
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
err = m.Mock(&buf, "Queuer")
|
err = m.Mock(&buf, "Queuer")
|
||||||
@ -144,7 +144,7 @@ func TestChannelNames(t *testing.T) {
|
|||||||
func TestImports(t *testing.T) {
|
func TestImports(t *testing.T) {
|
||||||
m, err := New("testdata/imports/two", "")
|
m, err := New("testdata/imports/two", "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("moq.New: %s", err)
|
t.Fatalf("moq.New: %s", err)
|
||||||
}
|
}
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
err = m.Mock(&buf, "DoSomething")
|
err = m.Mock(&buf, "DoSomething")
|
||||||
@ -165,3 +165,10 @@ func TestImports(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTemplateFuncs(t *testing.T) {
|
||||||
|
fn := templateFuncs["Exported"].(func(string) string)
|
||||||
|
if fn("var") != "Var" {
|
||||||
|
t.Errorf("exported didn't work: %s", fn("var"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user