@@ -948,75 +948,56 @@ func (args Arguments) Is(objects ...interface{}) bool {
948
948
return true
949
949
}
950
950
951
- type outputRenderer func () string
952
-
953
951
// Diff gets a string describing the differences between the arguments
954
952
// and the specified objects.
955
953
//
956
954
// Returns the diff string and number of differences found.
957
955
func (args Arguments ) Diff (objects []interface {}) (string , int ) {
958
956
// TODO: could return string as error and nil for No difference
959
957
960
- var outputBuilder strings. Builder
958
+ output := " \n "
961
959
var differences int
962
960
963
961
maxArgCount := len (args )
964
962
if len (objects ) > maxArgCount {
965
963
maxArgCount = len (objects )
966
964
}
967
965
968
- outputRenderers := []outputRenderer {}
969
-
970
966
for i := 0 ; i < maxArgCount ; i ++ {
971
- i := i
972
967
var actual , expected interface {}
973
- var actualFmt , expectedFmt func () string
968
+ var actualFmt , expectedFmt string
974
969
975
970
if len (objects ) <= i {
976
971
actual = "(Missing)"
977
- actualFmt = func () string {
978
- return "(Missing)"
979
- }
972
+ actualFmt = "(Missing)"
980
973
} else {
981
974
actual = objects [i ]
982
- actualFmt = func () string {
983
- return fmt .Sprintf ("(%[1]T=%[1]v)" , actual )
984
- }
975
+ actualFmt = fmt .Sprintf ("(%[1]T=%[1]v)" , actual )
985
976
}
986
977
987
978
if len (args ) <= i {
988
979
expected = "(Missing)"
989
- expectedFmt = func () string {
990
- return "(Missing)"
991
- }
980
+ expectedFmt = "(Missing)"
992
981
} else {
993
982
expected = args [i ]
994
- expectedFmt = func () string {
995
- return fmt .Sprintf ("(%[1]T=%[1]v)" , expected )
996
- }
983
+ expectedFmt = fmt .Sprintf ("(%[1]T=%[1]v)" , expected )
997
984
}
998
985
999
986
if matcher , ok := expected .(argumentMatcher ); ok {
1000
987
var matches bool
1001
988
func () {
1002
989
defer func () {
1003
990
if r := recover (); r != nil {
1004
- actualFmt = func () string {
1005
- return fmt .Sprintf ("panic in argument matcher: %v" , r )
1006
- }
991
+ actualFmt = fmt .Sprintf ("panic in argument matcher: %v" , r )
1007
992
}
1008
993
}()
1009
994
matches = matcher .Matches (actual )
1010
995
}()
1011
996
if matches {
1012
- outputRenderers = append (outputRenderers , func () string {
1013
- return fmt .Sprintf ("\t %d: PASS: %s matched by %s\n " , i , actualFmt (), matcher )
1014
- })
997
+ output = fmt .Sprintf ("%s\t %d: PASS: %s matched by %s\n " , output , i , actualFmt , matcher )
1015
998
} else {
1016
999
differences ++
1017
- outputRenderers = append (outputRenderers , func () string {
1018
- return fmt .Sprintf ("\t %d: FAIL: %s not matched by %s\n " , i , actualFmt (), matcher )
1019
- })
1000
+ output = fmt .Sprintf ("%s\t %d: FAIL: %s not matched by %s\n " , output , i , actualFmt , matcher )
1020
1001
}
1021
1002
} else {
1022
1003
switch expected := expected .(type ) {
@@ -1025,17 +1006,13 @@ func (args Arguments) Diff(objects []interface{}) (string, int) {
1025
1006
if reflect .TypeOf (actual ).Name () != string (expected ) && reflect .TypeOf (actual ).String () != string (expected ) {
1026
1007
// not match
1027
1008
differences ++
1028
- outputRenderers = append (outputRenderers , func () string {
1029
- return fmt .Sprintf ("\t %d: FAIL: type %s != type %s - %s\n " , i , expected , reflect .TypeOf (actual ).Name (), actualFmt ())
1030
- })
1009
+ output = fmt .Sprintf ("%s\t %d: FAIL: type %s != type %s - %s\n " , output , i , expected , reflect .TypeOf (actual ).Name (), actualFmt )
1031
1010
}
1032
1011
case * IsTypeArgument :
1033
1012
actualT := reflect .TypeOf (actual )
1034
1013
if actualT != expected .t {
1035
1014
differences ++
1036
- outputRenderers = append (outputRenderers , func () string {
1037
- return fmt .Sprintf ("\t %d: FAIL: type %s != type %s - %s\n " , i , expected .t .Name (), actualT .Name (), actualFmt ())
1038
- })
1015
+ output = fmt .Sprintf ("%s\t %d: FAIL: type %s != type %s - %s\n " , output , i , expected .t .Name (), actualT .Name (), actualFmt )
1039
1016
}
1040
1017
case * FunctionalOptionsArgument :
1041
1018
var name string
@@ -1046,36 +1023,26 @@ func (args Arguments) Diff(objects []interface{}) (string, int) {
1046
1023
const tName = "[]interface{}"
1047
1024
if name != reflect .TypeOf (actual ).String () && len (expected .values ) != 0 {
1048
1025
differences ++
1049
- outputRenderers = append (outputRenderers , func () string {
1050
- return fmt .Sprintf ("\t %d: FAIL: type %s != type %s - %s\n " , i , tName , reflect .TypeOf (actual ).Name (), actualFmt ())
1051
- })
1026
+ output = fmt .Sprintf ("%s\t %d: FAIL: type %s != type %s - %s\n " , output , i , tName , reflect .TypeOf (actual ).Name (), actualFmt )
1052
1027
} else {
1053
1028
if ef , af := assertOpts (expected .values , actual ); ef == "" && af == "" {
1054
1029
// match
1055
- outputRenderers = append (outputRenderers , func () string {
1056
- return fmt .Sprintf ("\t %d: PASS: %s == %s\n " , i , tName , tName )
1057
- })
1030
+ output = fmt .Sprintf ("%s\t %d: PASS: %s == %s\n " , output , i , tName , tName )
1058
1031
} else {
1059
1032
// not match
1060
1033
differences ++
1061
- outputRenderers = append (outputRenderers , func () string {
1062
- return fmt .Sprintf ("\t %d: FAIL: %s != %s\n " , i , af , ef )
1063
- })
1034
+ output = fmt .Sprintf ("%s\t %d: FAIL: %s != %s\n " , output , i , af , ef )
1064
1035
}
1065
1036
}
1066
1037
1067
1038
default :
1068
1039
if assert .ObjectsAreEqual (expected , Anything ) || assert .ObjectsAreEqual (actual , Anything ) || assert .ObjectsAreEqual (actual , expected ) {
1069
1040
// match
1070
- outputRenderers = append (outputRenderers , func () string {
1071
- return fmt .Sprintf ("\t %d: PASS: %s == %s\n " , i , actualFmt (), expectedFmt ())
1072
- })
1041
+ output = fmt .Sprintf ("%s\t %d: PASS: %s == %s\n " , output , i , actualFmt , expectedFmt )
1073
1042
} else {
1074
1043
// not match
1075
1044
differences ++
1076
- outputRenderers = append (outputRenderers , func () string {
1077
- return fmt .Sprintf ("\t %d: FAIL: %s != %s\n " , i , actualFmt (), expectedFmt ())
1078
- })
1045
+ output = fmt .Sprintf ("%s\t %d: FAIL: %s != %s\n " , output , i , actualFmt , expectedFmt )
1079
1046
}
1080
1047
}
1081
1048
}
@@ -1086,12 +1053,7 @@ func (args Arguments) Diff(objects []interface{}) (string, int) {
1086
1053
return "No differences." , differences
1087
1054
}
1088
1055
1089
- outputBuilder .WriteString ("\n " )
1090
- for _ , r := range outputRenderers {
1091
- outputBuilder .WriteString (r ())
1092
- }
1093
-
1094
- return outputBuilder .String (), differences
1056
+ return output , differences
1095
1057
}
1096
1058
1097
1059
// Assert compares the arguments with the specified objects and fails if
0 commit comments