Extract method to evaluate closest match

pull/1039/head
Bo Sunesen 2020-02-26 15:58:21 +01:00 committed by Boyan Soubachov
parent 1962448488
commit a5830c56d3
1 changed files with 36 additions and 11 deletions

View File

@ -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 {