Skip to content

Commit

Permalink
Extract method to evaluate closest match
Browse files Browse the repository at this point in the history
  • Loading branch information
uberbo authored and boyan-soubachov committed Jan 13, 2021
1 parent 1962448 commit a5830c5
Showing 1 changed file with 36 additions and 11 deletions.
47 changes: 36 additions & 11 deletions mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit a5830c5

Please sign in to comment.