counterfeiter icon indicating copy to clipboard operation
counterfeiter copied to clipboard

feature: export ArgsForCall structs to enable comparing the args as a struct

Open dnephin opened this issue 6 years ago • 1 comments

I have been using counterfeiter with gotest.tools/assert which uses go-cmp to compare structs. I often find myself using fake.FooArgsForCall(i) and writing an assertion for each of the args.

I would really like to write something like:

expected := myfake.FooArgsForCall{...}
assert.DeepEqual(t, myfake.FooArgsForCall(i), expected)

To make this happen I believe the "argsForCall" struct would need to be exported (currently it is an inline []struct{...}), and we'd need to come up with some new name for FooArgsForCall that returns the struct instead of separate args. I assume the existing FooArgsForCall will need to remain for backwards compatibility.

If there is interest in this change I would be happy to work on it.

dnephin avatar Dec 12 '18 23:12 dnephin

Looking into this I see that Fake.Invocations() can kind of be used for this purpose. It requires using the function name to index into the map, and use untyped []interface{} for the arg list. Exporting the type would remove the need to write a bunch of wrappers around this interface to make the tests readable. Something like:


func expectedCalls(funcCalls ...fakeCall) map[string][][]interface{} {
	expected := make(map[string][][]interface{}, len(funcCalls))
	for _, fake := range funcCalls {
		expected[fake.name] = append(expected[fake.name], fake.args)
	}
	return expected
}

type fakeCall struct {
	name string
	args []interface{}
}

func call(name string, args ...interface{}) fakeCall {
	return fakeCall{name: name, args: args}
}

dnephin avatar Dec 26 '18 17:12 dnephin