Skip to content

Commit a5830c5

Browse files
uberboboyan-soubachov
authored andcommitted
Extract method to evaluate closest match
1 parent 1962448 commit a5830c5

File tree

1 file changed

+36
-11
lines changed

1 file changed

+36
-11
lines changed

mock/mock.go

+36-11
Original file line numberDiff line numberDiff line change
@@ -297,27 +297,52 @@ func (m *Mock) findExpectedCall(method string, arguments ...interface{}) (int, *
297297
return -1, expectedCall
298298
}
299299

300+
type matchCandidate struct {
301+
call *Call
302+
mismatch string
303+
diffCount int
304+
}
305+
306+
func (c matchCandidate) isBetterMatchThan(other matchCandidate) bool {
307+
if c.call == nil {
308+
return false
309+
}
310+
if other.call == nil {
311+
return true
312+
}
313+
314+
if c.diffCount > other.diffCount {
315+
return false
316+
}
317+
if c.diffCount < other.diffCount {
318+
return true
319+
}
320+
321+
if c.call.Repeatability > other.call.Repeatability {
322+
return true
323+
}
324+
return false
325+
}
326+
300327
func (m *Mock) findClosestCall(method string, arguments ...interface{}) (*Call, string) {
301-
var diffCount int
302-
var repeatability int
303-
var closestCall *Call
304-
var err string
328+
var bestMatch matchCandidate
305329

306330
for _, call := range m.expectedCalls() {
307331
if call.Method == method {
308332

309333
errInfo, tempDiffCount := call.Arguments.Diff(arguments)
310-
if tempDiffCount < diffCount || diffCount == 0 || (tempDiffCount == diffCount && call.Repeatability > repeatability) {
311-
diffCount = tempDiffCount
312-
repeatability = call.Repeatability
313-
closestCall = call
314-
err = errInfo
334+
tempCandidate := matchCandidate{
335+
call: call,
336+
mismatch: errInfo,
337+
diffCount: tempDiffCount,
338+
}
339+
if tempCandidate.isBetterMatchThan(bestMatch) {
340+
bestMatch = tempCandidate
315341
}
316-
317342
}
318343
}
319344

320-
return closestCall, err
345+
return bestMatch.call, bestMatch.mismatch
321346
}
322347

323348
func callString(method string, arguments Arguments, includeArgumentValues bool) string {

0 commit comments

Comments
 (0)