nswrap/util/test.go

41 lines
934 B
Go

package util
import (
"fmt"
"math"
"strconv"
"strings"
)
// ShowDiff will print two strings vertically next to each other so that line
// differences are easier to read.
func ShowDiff(a, b string) string {
aLines := strings.Split(a, "\n")
bLines := strings.Split(b, "\n")
maxLines := int(math.Max(float64(len(aLines)), float64(len(bLines))))
out := "\n"
for lineNumber := 0; lineNumber < maxLines; lineNumber++ {
aLine := ""
bLine := ""
// Replace NULL characters with a dot. Otherwise the strings will look
// exactly the same but have different length (and therfore not be
// equal).
if lineNumber < len(aLines) {
aLine = strconv.Quote(aLines[lineNumber])
}
if lineNumber < len(bLines) {
bLine = strconv.Quote(bLines[lineNumber])
}
diffFlag := " "
if aLine != bLine {
diffFlag = "*"
}
out += fmt.Sprintf("%s %3d %-40s%-40s\n", diffFlag, lineNumber+1, aLine, bLine)
}
return out
}