Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

Support variadic pointer parameter on caller.SetArg #174

Open
rahmatismail opened this issue Apr 15, 2018 · 3 comments
Open

Support variadic pointer parameter on caller.SetArg #174

rahmatismail opened this issue Apr 15, 2018 · 3 comments

Comments

@rahmatismail
Copy link

rahmatismail commented Apr 15, 2018

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
Copy link
Author

rahmatismail commented Apr 15, 2018

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

@leoq-ardanlabs
Copy link

+1 on this

bergotorino added a commit to bergotorino/mock that referenced this issue Oct 15, 2021
The gomock's Call interface allows for setting a value to a specified
argument, making it convinient to work with functions that return data
through pointer arguments. However, this is not possible for variadic
functions, the error is:

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

The root cause is that the variadic argument in a form of
...interface{} is threated as single value instead of being treated as
slice.

This commit changes this situation by making sure that the variadic
arguments that are a slice are treated as slices. As a consequence it is
now possible to set argument value for variadic functions.

See: golang#174
@bergotorino
Copy link

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants