nix-amer/internal/clifmt/symbols_test.go

110 lines
2.4 KiB
Go

package clifmt
import (
"fmt"
"testing"
)
var lastPrint = ""
func mockupPrinter(format string, args ...interface{}) (int, error) {
lastPrint = fmt.Sprintf("%s%s", lastPrint, fmt.Sprintf(format, args...))
return 0, nil
}
func TestSpecial(t *testing.T) {
defaultPrinter = mockupPrinter
tests := []struct {
Pre func()
Processed string
Expect string
}{
{nil, Warn(), "\033[0;31m/!\\\033[0m"},
{nil, Warn("any text"), "\033[0;31m/!\\\033[0m any text"},
{nil, Info(), "\033[0;34m(!)\033[0m"},
{nil, Info("any text"), "\033[0;34m(!)\033[0m any text"},
{func() { Title("any text") }, "", "\n\033[0;33m>>\033[0m |1| any text \033[0;33m<<\033[0m\n"},
{func() { Title("any text") }, "", "\n\033[0;33m>>\033[0m |2| any text \033[0;33m<<\033[0m\n"},
}
for i, test := range tests {
if test.Pre != nil {
lastPrint = ""
test.Pre()
test.Processed = lastPrint
}
if test.Processed != test.Expect {
t.Errorf("[%d] expected '%s', got '%s'", i, escape(test.Expect), escape(test.Processed))
}
}
}
func TestAlign(t *testing.T) {
defaultPrinter = mockupPrinter
tests := []struct {
Offset int
Text string
Expect string
}{
{12, "1234567890 ", "1234567890 "},
{12, "12345678901 ", "1234567890\033[0m… "},
{12, "123456789012", "1234567890\033[0m… "},
{12, "1234567890123", "1234567890\033[0m… "},
}
for i, test := range tests {
lastPrint = ""
alignOffset = test.Offset
Align(test.Text)
if DisplaySize(lastPrint) != alignOffset {
t.Errorf("[%d] expected output to be %d chars, got %d (%s)", i, alignOffset, DisplaySize(lastPrint), escape(lastPrint))
}
if lastPrint != test.Expect {
t.Errorf("[%d] expected '%s', got '%s'", i, escape(test.Expect), escape(lastPrint))
}
}
}
func TestDisplaySize(t *testing.T) {
tests := []struct {
Text string
Expect int
}{
{"", 0},
{"\033[32m\033[0m", 0},
{"\033[0;32m\033[0m", 0},
{"1", 1},
{"\033[32m1\033[0m", 1},
{"\033[0;32m1\033[0m", 1},
{"\033[0;32m1\033[0m\033[0;32m\033[1;31m\033[0m", 1},
{"123", 3},
{"…123", 4},
{"123…", 4},
{"…123…", 5},
{"123456789", 9},
{"1234567890", 10},
{"1234567890\033[0m… ", 12},
}
for i, test := range tests {
if DisplaySize(test.Text) != test.Expect {
t.Errorf("[%d] expected output to be %d chars, got %d (%s)", i, test.Expect, DisplaySize(test.Text), escape(lastPrint))
}
}
}