matryer / moq

Interface mocking tool for go generate
http://bit.ly/meetmoq
MIT License
1.96k stars 126 forks source link

Mock method parameter shadows unexported types #216

Open bitflipp opened 5 months ago

bitflipp commented 5 months ago

Please consider the following snippet:

package main

type s struct{}

//go:generate moq -out i_test.go . i
type i interface {
    m(s s)
}

moq generates:

// m calls mFunc.
func (mock *iMock) m(s s) {            // Type s is shadowed.
    if mock.mFunc == nil {
        panic("iMock.mFunc: method is nil but i.m was just called")
    }
    callInfo := struct {
        S s                    // Compiler error: s (variable of type s) is not a type.
    }{
        S: s,
    }
    mock.lockm.Lock()
    mock.calls.m = append(mock.calls.m, callInfo)
    mock.lockm.Unlock()
    mock.mFunc(s)
}

This may (and arguably should) be remedied by changing the interface method to m(somethingDescriptive s), yet it's an invalid output generated from a valid input.