diff --git a/README.md b/README.md index efab77e..b0f06af 100644 --- a/README.md +++ b/README.md @@ -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 * Only mock the fields you need * 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 `.CallsTo.Method` to track the calls * Use `go:generate` to invoke the `moq` command diff --git a/package/moq/moq.go b/package/moq/moq.go index cb74e6e..ca1092a 100644 --- a/package/moq/moq.go +++ b/package/moq/moq.go @@ -47,7 +47,7 @@ func New(src, packageName string) (*Mocker, error) { if len(packageName) == 0 { 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 { return nil, err } @@ -214,6 +214,15 @@ func (p param) TypeString() string { 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. var moqImports = []string{"sync"} @@ -252,10 +261,14 @@ type {{.InterfaceName}}Mock struct { // CallsTo gets counters for each of the methods indicating // how many times each one was called. CallsTo struct { - lock sync.Mutex {{- range .Methods }} - // {{ .Name }} holds the number of calls to the {{.Name}} method. - {{ .Name }} int + lock{{.Name}} sync.Mutex // protects {{ .Name }} + // {{ .Name }} holds details about calls to the {{.Name}} method. + {{ .Name }} []struct { + {{- range .Params }} + {{ .Name | Exported }} {{ .Type }} + {{- end }} + } {{- end }} } } @@ -265,9 +278,17 @@ 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") } - mock.CallsTo.lock.Lock() - mock.CallsTo.{{.Name}}++ - mock.CallsTo.lock.Unlock() + mock.CallsTo.lock{{.Name}}.Lock() + mock.CallsTo.{{.Name}} = append(mock.CallsTo.{{.Name}}, struct{ + {{- range .Params }} + {{ .Name | Exported }} {{ .Type }} + {{- end }} + }{ + {{- range .Params }} + {{ .Name | Exported }}: {{ .Name }}, + {{- end }} + }) + mock.CallsTo.lock{{.Name}}.Unlock() {{- if .ReturnArglist }} return mock.{{.Name}}Func({{.ArgCallList}}) {{- else }} diff --git a/package/moq/moq_test.go b/package/moq/moq_test.go index b53b689..fc435b3 100644 --- a/package/moq/moq_test.go +++ b/package/moq/moq_test.go @@ -10,7 +10,7 @@ import ( func TestMoq(t *testing.T) { m, err := New("testdata/example", "") if err != nil { - t.Errorf("moq.New: %s", err) + t.Fatalf("moq.New: %s", err) } var buf bytes.Buffer err = m.Mock(&buf, "PersonStore") @@ -43,7 +43,7 @@ func TestMoq(t *testing.T) { func TestMoqExplicitPackage(t *testing.T) { m, err := New("testdata/example", "different") if err != nil { - t.Errorf("moq.New: %s", err) + t.Fatalf("moq.New: %s", err) } var buf bytes.Buffer err = m.Mock(&buf, "PersonStore") @@ -73,7 +73,7 @@ func TestMoqExplicitPackage(t *testing.T) { func TestVariadicArguments(t *testing.T) { m, err := New("testdata/variadic", "") if err != nil { - t.Errorf("moq.New: %s", err) + t.Fatalf("moq.New: %s", err) } var buf bytes.Buffer err = m.Mock(&buf, "Greeter") @@ -98,7 +98,7 @@ func TestVariadicArguments(t *testing.T) { func TestNothingToReturn(t *testing.T) { m, err := New("testdata/example", "") if err != nil { - t.Errorf("moq.New: %s", err) + t.Fatalf("moq.New: %s", err) } var buf bytes.Buffer err = m.Mock(&buf, "PersonStore") @@ -123,7 +123,7 @@ func TestNothingToReturn(t *testing.T) { func TestChannelNames(t *testing.T) { m, err := New("testdata/channels", "") if err != nil { - t.Errorf("moq.New: %s", err) + t.Fatalf("moq.New: %s", err) } var buf bytes.Buffer err = m.Mock(&buf, "Queuer") @@ -144,7 +144,7 @@ func TestChannelNames(t *testing.T) { func TestImports(t *testing.T) { m, err := New("testdata/imports/two", "") if err != nil { - t.Errorf("moq.New: %s", err) + t.Fatalf("moq.New: %s", err) } var buf bytes.Buffer 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")) + } +}