test: simplify & improve output

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
This commit is contained in:
Carlos A Becker 2022-06-01 10:38:18 -03:00 committed by Christian Muehlhaeuser
parent 681d473622
commit 065368aa11
3 changed files with 43 additions and 33 deletions

View File

@ -8,46 +8,47 @@ import (
)
func TestSetColorProfile(t *testing.T) {
input := "hello"
tt := []struct {
name string
profile termenv.Profile
input string
style Style
expected string
}{
{
"ascii",
termenv.Ascii,
"hello",
NewStyle().Foreground(Color("#5A56E0")),
"hello",
},
{
"ansi",
termenv.ANSI,
"hello",
NewStyle().Foreground(Color("#5A56E0")),
"\x1b[94mhello\x1b[0m",
},
{
"ansi256",
termenv.ANSI256,
"hello",
NewStyle().Foreground(Color("#5A56E0")),
"\x1b[38;5;62mhello\x1b[0m",
},
{
"truecolor",
termenv.TrueColor,
"hello",
NewStyle().Foreground(Color("#5A56E0")),
"\x1b[38;2;89;86;224mhello\x1b[0m",
},
}
for i, tc := range tt {
SetColorProfile(tc.profile)
res := tc.style.Render(tc.input)
if res != tc.expected {
t.Errorf("Test %d, expected:\n\n`%s`\n`%s`\n\nActual output:\n\n`%s`\n`%s`\n\n",
i, tc.expected, formatEscapes(tc.expected),
res, formatEscapes(res))
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
SetColorProfile(tc.profile)
style := NewStyle().Foreground(Color("#5A56E0"))
res := style.Render(input)
if res != tc.expected {
t.Errorf("Expected:\n\n`%s`\n`%s`\n\nActual output:\n\n`%s`\n`%s`\n\n",
tc.expected, formatEscapes(tc.expected),
res, formatEscapes(res))
}
})
}
}

View File

@ -4,18 +4,21 @@ import "testing"
func TestJoinVertical(t *testing.T) {
type test struct {
name string
result string
expected string
}
tests := []test{
{JoinVertical(0, "A", "BBBB"), "A \nBBBB"},
{JoinVertical(1, "A", "BBBB"), " A\nBBBB"},
{JoinVertical(0.25, "A", "BBBB"), " A \nBBBB"},
{"por0", JoinVertical(0, "A", "BBBB"), "A \nBBBB"},
{"pos1", JoinVertical(1, "A", "BBBB"), " A\nBBBB"},
{"pos0.25", JoinVertical(0.25, "A", "BBBB"), " A \nBBBB"},
}
for _, test := range tests {
if test.result != test.expected {
t.Errorf("Got \n%s\n, expected \n%s\n", test.result, test.expected)
}
t.Run(test.name, func(t *testing.T) {
if test.result != test.expected {
t.Errorf("Got \n%s\n, expected \n%s\n", test.result, test.expected)
}
})
}
}

View File

@ -6,37 +6,41 @@ import (
)
func TestStyleRunes(t *testing.T) {
t.Parallel()
matchedStyle := NewStyle().Reverse(true)
unmatchedStyle := NewStyle()
tt := []struct {
name string
input string
indices []int
expected string
}{
{
"hello 0",
"hello",
[]int{0},
"\x1b[7mh\x1b[0mello",
},
{
"你好 1",
"你好",
[]int{1},
"你\x1b[7m好\x1b[0m",
},
{
"hello 你好 6,7",
"hello 你好",
[]int{6, 7},
"hello \x1b[7m你好\x1b[0m",
},
{
"hello 1,3",
"hello",
[]int{1, 3},
"h\x1b[7me\x1b[0ml\x1b[7ml\x1b[0mo",
},
{
"你好 0,1",
"你好",
[]int{0, 1},
"\x1b[7m你好\x1b[0m",
@ -47,13 +51,15 @@ func TestStyleRunes(t *testing.T) {
return StyleRunes(str, indices, matchedStyle, unmatchedStyle)
}
for i, tc := range tt {
res := fn(tc.input, tc.indices)
if fn(tc.input, tc.indices) != tc.expected {
t.Errorf("Test %d, expected:\n\n`%s`\n`%s`\n\nActual Output:\n\n`%s`\n`%s`\n\n",
i, tc.expected, formatEscapes(tc.expected),
res, formatEscapes(res))
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
res := fn(tc.input, tc.indices)
if res != tc.expected {
t.Errorf("Expected:\n\n`%s`\n`%s`\n\nActual Output:\n\n`%s`\n`%s`\n\n",
tc.expected, formatEscapes(tc.expected),
res, formatEscapes(res))
}
})
}
}