add clifmt/colors minimal test

This commit is contained in:
Adrien Marquès 2018-11-13 16:03:57 +01:00
parent d4daa435e5
commit b4d6b8d8ff
2 changed files with 54 additions and 21 deletions

View File

@ -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))
}
}
}

View File

@ -92,24 +92,3 @@ func displaySize(s string) int {
return size 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)
}