From dc47a4c8c047130cbd510eb7299d38e513535949 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Mon, 26 Feb 2024 14:18:09 -0500 Subject: [PATCH] testcommon.LogColumns has a configurable width --- testcommon/common.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/testcommon/common.go b/testcommon/common.go index a1b4120..591d853 100644 --- a/testcommon/common.go +++ b/testcommon/common.go @@ -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) } }