From 24883c20a6ff09bf1822a8c9f8c5e29a8554de9a Mon Sep 17 00:00:00 2001 From: Suhas Karanth Date: Sun, 16 Aug 2020 12:16:52 +0530 Subject: [PATCH] Use fallback name for parameter when name is _ (#131) --- pkg/moq/moq.go | 2 +- pkg/moq/moq_test.go | 26 ++++++- pkg/moq/testpackages/blankid/swallower.go | 5 ++ .../testpackages/blankid/swallower.golden.go | 73 +++++++++++++++++++ .../variadic/{testdata => }/echoer.golden.go | 0 5 files changed, 103 insertions(+), 3 deletions(-) create mode 100644 pkg/moq/testpackages/blankid/swallower.go create mode 100644 pkg/moq/testpackages/blankid/swallower.golden.go rename pkg/moq/testpackages/variadic/{testdata => }/echoer.golden.go (100%) diff --git a/pkg/moq/moq.go b/pkg/moq/moq.go index d7ffe1e..0a10202 100644 --- a/pkg/moq/moq.go +++ b/pkg/moq/moq.go @@ -201,7 +201,7 @@ func (m *Mocker) extractArgs(sig *types.Signature) (params, results []*param) { func (m *Mocker) buildParam(v *types.Var, fallbackName string) *param { name := v.Name() - if name == "" { + if name == "" || name == "_" { name = fallbackName } typ := types.TypeString(v.Type(), m.packageQualifier) diff --git a/pkg/moq/moq_test.go b/pkg/moq/moq_test.go index 79eaa6c..2178e62 100644 --- a/pkg/moq/moq_test.go +++ b/pkg/moq/moq_test.go @@ -233,7 +233,26 @@ func TestSliceResult(t *testing.T) { t.Errorf("m.Mock: %s", err) } - golden := filepath.Join("testpackages/variadic/testdata", "echoer.golden.go") + golden := filepath.Join("testpackages/variadic", "echoer.golden.go") + if err := matchGoldenFile(golden, buf.Bytes()); err != nil { + t.Errorf("check golden file: %s", err) + } +} + +// TestBlankID tests generation of mock where a method on the interface +// uses a blank identifier. +// See https://github.com/matryer/moq/issues/70 +func TestBlankID(t *testing.T) { + m, err := New(Config{SrcDir: "testpackages/blankid"}) + if err != nil { + t.Fatalf("moq.New: %s", err) + } + + var buf bytes.Buffer + if err = m.Mock(&buf, "Swallower"); err != nil { + t.Errorf("m.Mock: %s", err) + } + golden := filepath.Join("testpackages/blankid", "swallower.golden.go") if err := matchGoldenFile(golden, buf.Bytes()); err != nil { t.Errorf("check golden file: %s", err) } @@ -342,8 +361,11 @@ func matchGoldenFile(goldenFile string, actual []byte) error { // To update golden files, run the following: // go test -v -run '^$' github.com/matryer/moq/pkg/moq -update if *update { + if err := os.MkdirAll(filepath.Dir(goldenFile), 0755); err != nil { + return fmt.Errorf("create dir: %s", err) + } if err := ioutil.WriteFile(goldenFile, actual, 0644); err != nil { - return fmt.Errorf("write: %s: %s", goldenFile, err) + return fmt.Errorf("write: %s", err) } return nil diff --git a/pkg/moq/testpackages/blankid/swallower.go b/pkg/moq/testpackages/blankid/swallower.go new file mode 100644 index 0000000..7d73d7b --- /dev/null +++ b/pkg/moq/testpackages/blankid/swallower.go @@ -0,0 +1,5 @@ +package blankid + +type Swallower interface { + Swallow(_ string) +} diff --git a/pkg/moq/testpackages/blankid/swallower.golden.go b/pkg/moq/testpackages/blankid/swallower.golden.go new file mode 100644 index 0000000..ecdcfa6 --- /dev/null +++ b/pkg/moq/testpackages/blankid/swallower.golden.go @@ -0,0 +1,73 @@ +// Code generated by moq; DO NOT EDIT. +// github.com/matryer/moq + +package blankid + +import ( + "sync" +) + +// Ensure, that SwallowerMock does implement Swallower. +// If this is not the case, regenerate this file with moq. +var _ Swallower = &SwallowerMock{} + +// SwallowerMock is a mock implementation of Swallower. +// +// func TestSomethingThatUsesSwallower(t *testing.T) { +// +// // make and configure a mocked Swallower +// mockedSwallower := &SwallowerMock{ +// SwallowFunc: func(in1 string) { +// panic("mock out the Swallow method") +// }, +// } +// +// // use mockedSwallower in code that requires Swallower +// // and then make assertions. +// +// } +type SwallowerMock struct { + // SwallowFunc mocks the Swallow method. + SwallowFunc func(in1 string) + + // calls tracks calls to the methods. + calls struct { + // Swallow holds details about calls to the Swallow method. + Swallow []struct { + // In1 is the in1 argument value. + In1 string + } + } + lockSwallow sync.RWMutex +} + +// Swallow calls SwallowFunc. +func (mock *SwallowerMock) Swallow(in1 string) { + if mock.SwallowFunc == nil { + panic("SwallowerMock.SwallowFunc: method is nil but Swallower.Swallow was just called") + } + callInfo := struct { + In1 string + }{ + In1: in1, + } + mock.lockSwallow.Lock() + mock.calls.Swallow = append(mock.calls.Swallow, callInfo) + mock.lockSwallow.Unlock() + mock.SwallowFunc(in1) +} + +// SwallowCalls gets all the calls that were made to Swallow. +// Check the length with: +// len(mockedSwallower.SwallowCalls()) +func (mock *SwallowerMock) SwallowCalls() []struct { + In1 string +} { + var calls []struct { + In1 string + } + mock.lockSwallow.RLock() + calls = mock.calls.Swallow + mock.lockSwallow.RUnlock() + return calls +} diff --git a/pkg/moq/testpackages/variadic/testdata/echoer.golden.go b/pkg/moq/testpackages/variadic/echoer.golden.go similarity index 100% rename from pkg/moq/testpackages/variadic/testdata/echoer.golden.go rename to pkg/moq/testpackages/variadic/echoer.golden.go