Skip to content

Commit 5d0bea5

Browse files
avoid rendering output
1 parent a61d79b commit 5d0bea5

File tree

1 file changed

+38
-15
lines changed

1 file changed

+38
-15
lines changed

mock/mock.go

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ func (c *Call) Unset() *Call {
229229
var index int // write index
230230
for _, call := range c.Parent.ExpectedCalls {
231231
if call.Method == c.Method {
232-
_, diffCount := call.Arguments.Diff(c.Arguments)
232+
diffCount := call.Arguments.countDiff(c.Arguments)
233233
if diffCount == 0 {
234234
foundMatchingCall = true
235235
// Remove from ExpectedCalls - just skip it
@@ -380,7 +380,7 @@ func (m *Mock) findExpectedCall(method string, arguments ...interface{}) (int, *
380380

381381
for i, call := range m.ExpectedCalls {
382382
if call.Method == method {
383-
_, diffCount := call.Arguments.Diff(arguments)
383+
diffCount := call.Arguments.countDiff(arguments)
384384
if diffCount == 0 {
385385
expectedCall = call
386386
if call.Repeatability > -1 {
@@ -745,7 +745,7 @@ func (m *Mock) methodWasCalled(methodName string, expected []interface{}) bool {
745745
for _, call := range m.calls() {
746746
if call.Method == methodName {
747747

748-
_, differences := Arguments(expected).Diff(call.Arguments)
748+
differences := Arguments(expected).countDiff(call.Arguments)
749749

750750
if differences == 0 {
751751
// found the expected call
@@ -943,11 +943,34 @@ func (args Arguments) Is(objects ...interface{}) bool {
943943
//
944944
// Returns the diff string and number of differences found.
945945
func (args Arguments) Diff(objects []interface{}) (string, int) {
946+
return args.diff(objects, true)
947+
}
948+
949+
// countDiff gets the number of differences between the arguments
950+
// and the specified objects.
951+
//
952+
// Returns the diff number of differences found.
953+
func (args Arguments) countDiff(objects []interface{}) int {
954+
_, count := args.diff(objects, false)
955+
return count
956+
}
957+
958+
func noOpSprintf(format string, args ...interface{}) string {
959+
return ""
960+
}
961+
962+
// diff allows for the diffing of arguments and objects.
963+
func (args Arguments) diff(objects []interface{}, includeOutput bool) (string, int) {
946964
// TODO: could return string as error and nil for No difference
947965

948966
output := "\n"
949967
var differences int
950968

969+
optSprintf := fmt.Sprintf
970+
if !includeOutput {
971+
optSprintf = noOpSprintf
972+
}
973+
951974
maxArgCount := len(args)
952975
if len(objects) > maxArgCount {
953976
maxArgCount = len(objects)
@@ -962,32 +985,32 @@ func (args Arguments) Diff(objects []interface{}) (string, int) {
962985
actualFmt = "(Missing)"
963986
} else {
964987
actual = objects[i]
965-
actualFmt = fmt.Sprintf("(%[1]T=%[1]v)", actual)
988+
actualFmt = optSprintf("(%[1]T=%[1]v)", actual)
966989
}
967990

968991
if len(args) <= i {
969992
expected = "(Missing)"
970993
expectedFmt = "(Missing)"
971994
} else {
972995
expected = args[i]
973-
expectedFmt = fmt.Sprintf("(%[1]T=%[1]v)", expected)
996+
expectedFmt = optSprintf("(%[1]T=%[1]v)", expected)
974997
}
975998

976999
if matcher, ok := expected.(argumentMatcher); ok {
9771000
var matches bool
9781001
func() {
9791002
defer func() {
9801003
if r := recover(); r != nil {
981-
actualFmt = fmt.Sprintf("panic in argument matcher: %v", r)
1004+
actualFmt = optSprintf("panic in argument matcher: %v", r)
9821005
}
9831006
}()
9841007
matches = matcher.Matches(actual)
9851008
}()
9861009
if matches {
987-
output = fmt.Sprintf("%s\t%d: PASS: %s matched by %s\n", output, i, actualFmt, matcher)
1010+
output = optSprintf("%s\t%d: PASS: %s matched by %s\n", output, i, actualFmt, matcher)
9881011
} else {
9891012
differences++
990-
output = fmt.Sprintf("%s\t%d: FAIL: %s not matched by %s\n", output, i, actualFmt, matcher)
1013+
output = optSprintf("%s\t%d: FAIL: %s not matched by %s\n", output, i, actualFmt, matcher)
9911014
}
9921015
} else {
9931016
switch expected := expected.(type) {
@@ -996,13 +1019,13 @@ func (args Arguments) Diff(objects []interface{}) (string, int) {
9961019
if reflect.TypeOf(actual).Name() != string(expected) && reflect.TypeOf(actual).String() != string(expected) {
9971020
// not match
9981021
differences++
999-
output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, expected, reflect.TypeOf(actual).Name(), actualFmt)
1022+
output = optSprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, expected, reflect.TypeOf(actual).Name(), actualFmt)
10001023
}
10011024
case *IsTypeArgument:
10021025
actualT := reflect.TypeOf(actual)
10031026
if actualT != expected.t {
10041027
differences++
1005-
output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, expected.t.Name(), actualT.Name(), actualFmt)
1028+
output = optSprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, expected.t.Name(), actualT.Name(), actualFmt)
10061029
}
10071030
case *FunctionalOptionsArgument:
10081031
var name string
@@ -1013,26 +1036,26 @@ func (args Arguments) Diff(objects []interface{}) (string, int) {
10131036
const tName = "[]interface{}"
10141037
if name != reflect.TypeOf(actual).String() && len(expected.values) != 0 {
10151038
differences++
1016-
output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, tName, reflect.TypeOf(actual).Name(), actualFmt)
1039+
output = optSprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, tName, reflect.TypeOf(actual).Name(), actualFmt)
10171040
} else {
10181041
if ef, af := assertOpts(expected.values, actual); ef == "" && af == "" {
10191042
// match
1020-
output = fmt.Sprintf("%s\t%d: PASS: %s == %s\n", output, i, tName, tName)
1043+
output = optSprintf("%s\t%d: PASS: %s == %s\n", output, i, tName, tName)
10211044
} else {
10221045
// not match
10231046
differences++
1024-
output = fmt.Sprintf("%s\t%d: FAIL: %s != %s\n", output, i, af, ef)
1047+
output = optSprintf("%s\t%d: FAIL: %s != %s\n", output, i, af, ef)
10251048
}
10261049
}
10271050

10281051
default:
10291052
if assert.ObjectsAreEqual(expected, Anything) || assert.ObjectsAreEqual(actual, Anything) || assert.ObjectsAreEqual(actual, expected) {
10301053
// match
1031-
output = fmt.Sprintf("%s\t%d: PASS: %s == %s\n", output, i, actualFmt, expectedFmt)
1054+
output = optSprintf("%s\t%d: PASS: %s == %s\n", output, i, actualFmt, expectedFmt)
10321055
} else {
10331056
// not match
10341057
differences++
1035-
output = fmt.Sprintf("%s\t%d: FAIL: %s != %s\n", output, i, actualFmt, expectedFmt)
1058+
output = optSprintf("%s\t%d: FAIL: %s != %s\n", output, i, actualFmt, expectedFmt)
10361059
}
10371060
}
10381061
}

0 commit comments

Comments
 (0)