golang / mock

GoMock is a mocking framework for the Go programming language.
Apache License 2.0
9.3k stars 611 forks source link

Support variadic pointer parameter on caller.SetArg #174

Open rahmatismail opened 6 years ago

rahmatismail commented 6 years ago

gomock caller.SetArg recognizes variadic pointer parameter as a single param

Code example

func (p *pkg) VariadicInterface(data ...interface{}) {
    for i := range data {
        data[i] = i
    }
}
func (p *pkg) VariadicInt(data ...*int) {
    for i := range data {
        *data[i] = i
    }
}

// Usage
var a, b, c, d int
pkgReal.VariadicInterface(&a, &b)
pkgReal.VariadicInt(&c, &d)

Expect function on Test

mockPkg.EXPECT().VariadicInterface(gomock.Any(), gomock.Any()).SetArg(0, 5).SetArg(1, 5)
mockPkg.EXPECT().VariadicInt(gomock.Any(), gomock.Any()).SetArg(0, 5).SetArg(1, 5)

Output

SetArg(1, ...) called for a method with 1 args

Expected behaviour

Test passed with a, b, c, and d match given value

Please correct me if I use it wrong.

rahmatismail commented 6 years ago

Sorry, I did not notice this // TODO: This will break on variadic methods.

leoq-ardanlabs commented 2 years ago

+1 on this

bergotorino commented 2 years ago

I've needed a similar functionality and made a change that seems to match this issue