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