When `len(actual) > len(expected)`, this for loop would cause a panic.
Alternative to this implementation, there could be a check for
`if len(actual) != len(expected)`, but generating a meaningful message
describing that is already provided by `diff()`. So we defer to diff in
every case.
Fixes#393
Pre-diff:
```
panic: runtime error: index out of range
```
Post-diff:
```
panic:
mock: Unexpected Method Call
-----------------------------
MyFunction(string,string,string)
0: string,
1: string,
2: string
The closest call I have is:
MyFunction(string,string)
0: "mock.Anything"
1: "mock.Anything"
Provided 3 arguments, mocked for 2 arguments
```
Add an argumentMatcher type and MatchedBy() helper to the mock
package, enabling custom fine-grained argument matching behaviors.
This is particularly helpful when building mocks over complex argument
types where deep equality is cumbersome or can't be used. Ex, a matcher
might match over just the the URL of an argument *http.Request.
Originally the expctations required that the return values be specified
immediately after the method name and arguments, otherwise the call
setup will either panic (best case) or silently modify the *previous*
call specification (worst case).
This change moves the Return(), Run(), Once(), etc methods onto the Call
struct, and changes the chaining behaviour so that they modify the Call
data directly rather than referencing the last item in the ExpectedCalls
array.
There is race condition caused when a method being tested calls a mocked
method within a go routine. For example, caching might be done a go
routine and that caching might be mocked.
There is already a mutex protecting the lists on the Mock object -
however Return() appends to ExpectedCalls and findExpectedCall could
run at the same time.
We cannot compare two Func arguments to check if they are equal using
the reflect package. This leads to the following misleading panic:
```go
handler := func(http.ResponseWriter, *http.Request) {}
muxMock.On("HandleFunc", "/", handler)
muxMock.HandleFunc("/", handler)
// panics arguing handler != handler
```
Since we cannot fix this behaviour using reflection, this patch
provides a better panic with a meaningful workaround.
```go
handler := func(http.ResponseWriter, *http.Request) {}
muxMock.On("HandleFunc", "/", handler)
// panics recommending defining the expectatin with:
// AnythingOfType("func(http.ResponseWriter, *http.Request)")
```
Solution following the panic's instructions:
```go
handler := func(http.ResponseWriter, *http.Request) {}
muxMock.On("HandleFunc", "/",
mock.AnythingOfType("func(http.ResponseWriter, *http.Request)"))
muxMock.HandleFunc("/", handler)
```