@@ -429,39 +429,7 @@ func AssertSqlEqual(t *testing.T, expected, actual string) {
429429 fmt .Printf ("%s\n " , SqlPrettyPrint ([]byte (actual )))
430430 actualLines := strings .Split (actual , "\n " )
431431 expectedLines := strings .Split (expected , "\n " )
432- pp .Println ("-- First diff: " )
433- for i , aLine := range actualLines {
434- if i >= len (expectedLines ) {
435- fmt .Println ("Actual is longer than expected" )
436- break
437- }
438- eLine := expectedLines [i ]
439- if aLine != eLine {
440- if i > 0 {
441- fmt .Println (" " , actualLines [i - 1 ])
442- }
443- fmt .Println (" actual:" , aLine )
444- if i + 1 < len (actualLines ) {
445- fmt .Println (" " , actualLines [i + 1 ])
446- }
447- fmt .Println ()
448- if i > 0 {
449- fmt .Println (" " , expectedLines [i - 1 ])
450- }
451- fmt .Println ("expected:" , eLine )
452- if i + 1 < len (expectedLines ) {
453- fmt .Println (" " , expectedLines [i + 1 ])
454- }
455-
456- for j := range min (len (aLine ), len (eLine )) {
457- if aLine [j ] != eLine [j ] {
458- fmt .Printf ("First diff in line %d at index %d (actual: %c, expected: %c)\n " , i , j , aLine [j ], eLine [j ])
459- break
460- }
461- }
462- break
463- }
464- }
432+ pp .Printf ("-- First diff: \n %s\n " , firstDiff (expectedLines , actualLines ))
465433 t .Errorf ("Expected: %s\n \n actual: %s" , expected , actual )
466434 }
467435}
@@ -855,39 +823,7 @@ func InitSqlMockWithPrettySqlAndPrint(t *testing.T, matchExpectationsInOrder boo
855823
856824 actualPretty := strings .Split (SqlPrettyPrint ([]byte (mismatch .actual )), "\n " )
857825 expectedPretty := strings .Split (SqlPrettyPrint ([]byte (mismatch .expected )), "\n " )
858-
859- for i , aLine := range actualPretty {
860- if i >= len (expectedPretty ) {
861- fmt .Println ("Actual is longer than expected" )
862- break
863- }
864- eLine := expectedPretty [i ]
865- if aLine != eLine {
866- if i > 0 {
867- fmt .Println (" " , actualPretty [i - 1 ])
868- }
869- fmt .Println (" actual:" , aLine )
870- if i + 1 < len (actualPretty ) {
871- fmt .Println (" " , actualPretty [i + 1 ])
872- }
873- fmt .Println ()
874- if i > 0 {
875- fmt .Println (" " , expectedPretty [i - 1 ])
876- }
877- fmt .Println ("expected:" , eLine )
878- if i + 1 < len (expectedPretty ) {
879- fmt .Println (" " , expectedPretty [i + 1 ])
880- }
881-
882- for j := range min (len (actualPretty ), len (expectedPretty )) {
883- if actualPretty [j ] != expectedPretty [j ] {
884- fmt .Printf ("First diff in line %d at index %d (actual: %v, expected: %v)\n " , i , j , actualPretty [j ], expectedPretty [j ])
885- break
886- }
887- }
888- break
889- }
890- }
826+ pp .Printf ("-- First diff: \n %s\n " , firstDiff (expectedPretty , actualPretty ))
891827 }
892828 }
893829 })
@@ -1075,3 +1011,41 @@ func ReadResponseBody(resp *http.Response) ([]byte, error) {
10751011 resp .Body = io .NopCloser (bytes .NewBuffer (respBody ))
10761012 return respBody , nil
10771013}
1014+
1015+ func firstDiff (expectedLines , actualLines []string ) string {
1016+ var diff strings.Builder
1017+ for i , aLine := range actualLines {
1018+ if i >= len (expectedLines ) {
1019+ fmt .Println ("Actual is longer than expected" )
1020+ break
1021+ }
1022+ eLine := expectedLines [i ]
1023+ if aLine != eLine {
1024+ if i > 0 {
1025+ diff .WriteString (fmt .Sprintf ("\t \t %s" , actualLines [i - 1 ]))
1026+ }
1027+ diff .WriteString (fmt .Sprintf (" actual: %s" , aLine ))
1028+ if i + 1 < len (actualLines ) {
1029+ diff .WriteString (fmt .Sprintf ("\t \t %s" , actualLines [i + 1 ]))
1030+ }
1031+ diff .WriteString ("\n " )
1032+ if i > 0 {
1033+ diff .WriteString (fmt .Sprintf ("\t \t %s" , expectedLines [i - 1 ]))
1034+ }
1035+ diff .WriteString (fmt .Sprintf (" expected: %s" , eLine ))
1036+ if i + 1 < len (expectedLines ) {
1037+ diff .WriteString (fmt .Sprintf ("\t \t %s" , expectedLines [i + 1 ]))
1038+ }
1039+
1040+ for j := range min (len (aLine ), len (eLine )) {
1041+ if aLine [j ] != eLine [j ] {
1042+ diff .WriteString (fmt .Sprintf ("First diff in line %d at index %d (actual: %c, expected: %c)\n " , i , j , aLine [j ], eLine [j ]))
1043+ break
1044+ }
1045+ }
1046+ break
1047+ }
1048+ }
1049+
1050+ return diff .String ()
1051+ }
0 commit comments