nix-amer/internal/clifmt/colors_test.go

57 lines
1.2 KiB
Go
Raw Permalink Normal View History

2018-11-13 15:03:57 +00:00
package clifmt
import (
"testing"
)
func escape(in string) string {
out := make([]rune, 0)
for _, char := range in {
2018-11-13 15:22:57 +00:00
if char == '\\' {
out = append(out, []rune("\\\\")...)
} else if char == '\n' {
2018-11-13 15:03:57 +00:00
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))
}
}
}