testcommon.LogColumns has a configurable width

This commit is contained in:
Sasha Koshka 2024-02-26 14:18:09 -05:00
parent 2edbd6c8b3
commit dc47a4c8c0
1 changed files with 9 additions and 7 deletions

View File

@ -3,16 +3,17 @@ package testcommon
import "testing"
import "strings"
import "strconv"
// LogColumns logs columns of text side by side, each column 80 characters wide.
func LogColumns (test *testing.T, columns ...any) {
LogColumnsStyle(test, "0m", columns...)
func LogColumns (test *testing.T, width int, columns ...any) {
LogColumnsStyle(test, width, "0m", columns...)
}
// LogColumnsStyle is like LogColumns, but accepts an ANSI escape code as an
// argument (without the \033[). For example, passing 31m causes the text to be
// red.
func LogColumnsStyle (test *testing.T, style string, columns ...any) {
func LogColumnsStyle (test *testing.T, width int, style string, columns ...any) {
formatString := ""
for index := range columns {
if index > 0 { formatString += " | " }
@ -20,7 +21,7 @@ func LogColumnsStyle (test *testing.T, style string, columns ...any) {
if index == len(columns) - 1 {
formatString += "%v"
} else {
formatString += "%-80v"
formatString += "%-" + strconv.Itoa(width) + "v"
}
}
test.Logf("\033[" + style + formatString + "\033[0m", columns...)
@ -37,7 +38,8 @@ func Compare (test *testing.T, correct, got string) {
length := len(gotLines)
if len(correctLines) > len(gotLines) { length = len(correctLines) }
LogColumns(test, "CORRECT:", "GOT:")
width := 80
LogColumns(test, width, "CORRECT:", "GOT:")
test.Log()
for index := 0; index < length; index ++ {
left := ""
@ -46,9 +48,9 @@ func Compare (test *testing.T, correct, got string) {
if index < len(gotLines) { right = gotLines[index] }
if left != right {
LogColumnsStyle(test, "31m", left, right)
LogColumnsStyle(test, width, "31m", left, right)
} else {
LogColumns(test, left, right)
LogColumns(test, width, left, right)
}
}