@@ -229,7 +229,7 @@ func (c *Call) Unset() *Call {
229
229
var index int // write index
230
230
for _ , call := range c .Parent .ExpectedCalls {
231
231
if call .Method == c .Method {
232
- _ , diffCount := call .Arguments .Diff (c .Arguments )
232
+ diffCount := call .Arguments .countDiff (c .Arguments )
233
233
if diffCount == 0 {
234
234
foundMatchingCall = true
235
235
// Remove from ExpectedCalls - just skip it
@@ -380,7 +380,7 @@ func (m *Mock) findExpectedCall(method string, arguments ...interface{}) (int, *
380
380
381
381
for i , call := range m .ExpectedCalls {
382
382
if call .Method == method {
383
- _ , diffCount := call .Arguments .Diff (arguments )
383
+ diffCount := call .Arguments .countDiff (arguments )
384
384
if diffCount == 0 {
385
385
expectedCall = call
386
386
if call .Repeatability > - 1 {
@@ -745,7 +745,7 @@ func (m *Mock) methodWasCalled(methodName string, expected []interface{}) bool {
745
745
for _ , call := range m .calls () {
746
746
if call .Method == methodName {
747
747
748
- _ , differences := Arguments (expected ).Diff (call .Arguments )
748
+ differences := Arguments (expected ).countDiff (call .Arguments )
749
749
750
750
if differences == 0 {
751
751
// found the expected call
@@ -943,11 +943,34 @@ func (args Arguments) Is(objects ...interface{}) bool {
943
943
//
944
944
// Returns the diff string and number of differences found.
945
945
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 ) {
946
964
// TODO: could return string as error and nil for No difference
947
965
948
966
output := "\n "
949
967
var differences int
950
968
969
+ optSprintf := fmt .Sprintf
970
+ if ! includeOutput {
971
+ optSprintf = noOpSprintf
972
+ }
973
+
951
974
maxArgCount := len (args )
952
975
if len (objects ) > maxArgCount {
953
976
maxArgCount = len (objects )
@@ -962,32 +985,32 @@ func (args Arguments) Diff(objects []interface{}) (string, int) {
962
985
actualFmt = "(Missing)"
963
986
} else {
964
987
actual = objects [i ]
965
- actualFmt = fmt . Sprintf ("(%[1]T=%[1]v)" , actual )
988
+ actualFmt = optSprintf ("(%[1]T=%[1]v)" , actual )
966
989
}
967
990
968
991
if len (args ) <= i {
969
992
expected = "(Missing)"
970
993
expectedFmt = "(Missing)"
971
994
} else {
972
995
expected = args [i ]
973
- expectedFmt = fmt . Sprintf ("(%[1]T=%[1]v)" , expected )
996
+ expectedFmt = optSprintf ("(%[1]T=%[1]v)" , expected )
974
997
}
975
998
976
999
if matcher , ok := expected .(argumentMatcher ); ok {
977
1000
var matches bool
978
1001
func () {
979
1002
defer func () {
980
1003
if r := recover (); r != nil {
981
- actualFmt = fmt . Sprintf ("panic in argument matcher: %v" , r )
1004
+ actualFmt = optSprintf ("panic in argument matcher: %v" , r )
982
1005
}
983
1006
}()
984
1007
matches = matcher .Matches (actual )
985
1008
}()
986
1009
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 )
988
1011
} else {
989
1012
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 )
991
1014
}
992
1015
} else {
993
1016
switch expected := expected .(type ) {
@@ -996,13 +1019,13 @@ func (args Arguments) Diff(objects []interface{}) (string, int) {
996
1019
if reflect .TypeOf (actual ).Name () != string (expected ) && reflect .TypeOf (actual ).String () != string (expected ) {
997
1020
// not match
998
1021
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 )
1000
1023
}
1001
1024
case * IsTypeArgument :
1002
1025
actualT := reflect .TypeOf (actual )
1003
1026
if actualT != expected .t {
1004
1027
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 )
1006
1029
}
1007
1030
case * FunctionalOptionsArgument :
1008
1031
var name string
@@ -1013,26 +1036,26 @@ func (args Arguments) Diff(objects []interface{}) (string, int) {
1013
1036
const tName = "[]interface{}"
1014
1037
if name != reflect .TypeOf (actual ).String () && len (expected .values ) != 0 {
1015
1038
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 )
1017
1040
} else {
1018
1041
if ef , af := assertOpts (expected .values , actual ); ef == "" && af == "" {
1019
1042
// 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 )
1021
1044
} else {
1022
1045
// not match
1023
1046
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 )
1025
1048
}
1026
1049
}
1027
1050
1028
1051
default :
1029
1052
if assert .ObjectsAreEqual (expected , Anything ) || assert .ObjectsAreEqual (actual , Anything ) || assert .ObjectsAreEqual (actual , expected ) {
1030
1053
// 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 )
1032
1055
} else {
1033
1056
// not match
1034
1057
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 )
1036
1059
}
1037
1060
}
1038
1061
}
0 commit comments