diff --git a/internal/clifmt/colors_test.go b/internal/clifmt/colors_test.go new file mode 100644 index 0000000..db12e88 --- /dev/null +++ b/internal/clifmt/colors_test.go @@ -0,0 +1,54 @@ +package clifmt + +import ( + "testing" +) + +func escape(in string) string { + + out := make([]rune, 0) + + for _, char := range in { + if char == '\n' { + out = append(out, []rune("\\n")...) + } else if char == '\r' { + out = append(out, []rune("\\r")...) + } else if char == '\t' { + out = append(out, []rune("\\t")...) + } else if char == '\033' { + out = append(out, []rune("\\033")...) + } else { + out = append(out, char) + } + } + + return string(out) +} + +func TestColoring(t *testing.T) { + + tests := []struct { + Text string + Color byte + Bold bool + Expect string + }{ + {"any text", 0, false, "\033[0;0many text\033[0m"}, + {"any text", 1, false, "\033[0;1many text\033[0m"}, + {"any text", 32, false, "\033[0;32many text\033[0m"}, + + {"any text", 0, true, "\033[1;0many text\033[0m"}, + {"any text", 1, true, "\033[1;1many text\033[0m"}, + {"any text", 32, true, "\033[1;32many text\033[0m"}, + } + + for i, test := range tests { + + colored := Color(test.Color, test.Text, test.Bold) + if colored != test.Expect { + t.Errorf("[%d] expected '%s', got '%s'", i, escape(test.Expect), escape(colored)) + } + + } + +} diff --git a/internal/clifmt/symbols.go b/internal/clifmt/symbols.go index 475d6aa..8bd429b 100644 --- a/internal/clifmt/symbols.go +++ b/internal/clifmt/symbols.go @@ -92,24 +92,3 @@ func displaySize(s string) int { return size } - -func escape(in string) string { - - out := make([]rune, 0) - - for _, char := range in { - if char == '\n' { - out = append(out, []rune("\\n")...) - } else if char == '\r' { - out = append(out, []rune("\\r")...) - } else if char == '\t' { - out = append(out, []rune("\\t")...) - } else if char == '\033' { - out = append(out, []rune("\\033")...) - } else { - out = append(out, char) - } - } - - return string(out) -}