Use fallback name for parameter when name is _ (#131)
This commit is contained in:
parent
70fed06ee3
commit
24883c20a6
@ -201,7 +201,7 @@ func (m *Mocker) extractArgs(sig *types.Signature) (params, results []*param) {
|
|||||||
|
|
||||||
func (m *Mocker) buildParam(v *types.Var, fallbackName string) *param {
|
func (m *Mocker) buildParam(v *types.Var, fallbackName string) *param {
|
||||||
name := v.Name()
|
name := v.Name()
|
||||||
if name == "" {
|
if name == "" || name == "_" {
|
||||||
name = fallbackName
|
name = fallbackName
|
||||||
}
|
}
|
||||||
typ := types.TypeString(v.Type(), m.packageQualifier)
|
typ := types.TypeString(v.Type(), m.packageQualifier)
|
||||||
|
@ -233,7 +233,26 @@ func TestSliceResult(t *testing.T) {
|
|||||||
t.Errorf("m.Mock: %s", err)
|
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 {
|
if err := matchGoldenFile(golden, buf.Bytes()); err != nil {
|
||||||
t.Errorf("check golden file: %s", err)
|
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:
|
// To update golden files, run the following:
|
||||||
// go test -v -run '^<Test-Name>$' github.com/matryer/moq/pkg/moq -update
|
// go test -v -run '^<Test-Name>$' github.com/matryer/moq/pkg/moq -update
|
||||||
if *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 {
|
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
|
return nil
|
||||||
|
5
pkg/moq/testpackages/blankid/swallower.go
Normal file
5
pkg/moq/testpackages/blankid/swallower.go
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package blankid
|
||||||
|
|
||||||
|
type Swallower interface {
|
||||||
|
Swallow(_ string)
|
||||||
|
}
|
73
pkg/moq/testpackages/blankid/swallower.golden.go
Normal file
73
pkg/moq/testpackages/blankid/swallower.golden.go
Normal file
@ -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
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user