mirror of https://github.com/stretchr/testify.git
Extract method to evaluate closest match
parent
1962448488
commit
a5830c56d3
47
mock/mock.go
47
mock/mock.go
|
@ -297,27 +297,52 @@ func (m *Mock) findExpectedCall(method string, arguments ...interface{}) (int, *
|
|||
return -1, expectedCall
|
||||
}
|
||||
|
||||
type matchCandidate struct {
|
||||
call *Call
|
||||
mismatch string
|
||||
diffCount int
|
||||
}
|
||||
|
||||
func (c matchCandidate) isBetterMatchThan(other matchCandidate) bool {
|
||||
if c.call == nil {
|
||||
return false
|
||||
}
|
||||
if other.call == nil {
|
||||
return true
|
||||
}
|
||||
|
||||
if c.diffCount > other.diffCount {
|
||||
return false
|
||||
}
|
||||
if c.diffCount < other.diffCount {
|
||||
return true
|
||||
}
|
||||
|
||||
if c.call.Repeatability > other.call.Repeatability {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (m *Mock) findClosestCall(method string, arguments ...interface{}) (*Call, string) {
|
||||
var diffCount int
|
||||
var repeatability int
|
||||
var closestCall *Call
|
||||
var err string
|
||||
var bestMatch matchCandidate
|
||||
|
||||
for _, call := range m.expectedCalls() {
|
||||
if call.Method == method {
|
||||
|
||||
errInfo, tempDiffCount := call.Arguments.Diff(arguments)
|
||||
if tempDiffCount < diffCount || diffCount == 0 || (tempDiffCount == diffCount && call.Repeatability > repeatability) {
|
||||
diffCount = tempDiffCount
|
||||
repeatability = call.Repeatability
|
||||
closestCall = call
|
||||
err = errInfo
|
||||
tempCandidate := matchCandidate{
|
||||
call: call,
|
||||
mismatch: errInfo,
|
||||
diffCount: tempDiffCount,
|
||||
}
|
||||
if tempCandidate.isBetterMatchThan(bestMatch) {
|
||||
bestMatch = tempCandidate
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return closestCall, err
|
||||
return bestMatch.call, bestMatch.mismatch
|
||||
}
|
||||
|
||||
func callString(method string, arguments Arguments, includeArgumentValues bool) string {
|
||||
|
|
Loading…
Reference in New Issue