Skip to content

Commit

Permalink
more color tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseduffield committed Aug 1, 2021
1 parent 04e474a commit e798aa4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
22 changes: 22 additions & 0 deletions pkg/gui/style/style_test.go
Expand Up @@ -5,15 +5,20 @@ import (

"github.com/gookit/color"
"github.com/stretchr/testify/assert"
"github.com/xo/terminfo"
)

func TestMerge(t *testing.T) {
type scenario struct {
name string
toMerge []TextStyle
expectedStyle TextStyle
expectedStr string
}

// on CI we've got no color capability so we're forcing it here
color.ForceSetColorLevel(terminfo.ColorLevelMillions)

fgRed := color.FgRed
bgRed := color.BgRed
fgBlue := color.FgBlue
Expand All @@ -24,21 +29,26 @@ func TestMerge(t *testing.T) {
rgbYellowLib := color.Rgb(0xFF, 0xFF, 0x00)
rgbYellow := NewRGBColor(rgbYellowLib)

strToPrint := "foo"

scenarios := []scenario{
{
"no color",
nil,
TextStyle{style: color.Style{}},
"foo",
},
{
"only fg color",
[]TextStyle{FgRed},
TextStyle{fg: &Color{basic: &fgRed}, style: color.Style{fgRed}},
"\x1b[31mfoo\x1b[0m",
},
{
"only bg color",
[]TextStyle{BgRed},
TextStyle{bg: &Color{basic: &bgRed}, style: color.Style{bgRed}},
"\x1b[41mfoo\x1b[0m",
},
{
"fg and bg color",
Expand All @@ -48,6 +58,7 @@ func TestMerge(t *testing.T) {
bg: &Color{basic: &bgRed},
style: color.Style{fgBlue, bgRed},
},
"\x1b[34;41mfoo\x1b[0m",
},
{
"single attribute",
Expand All @@ -56,6 +67,7 @@ func TestMerge(t *testing.T) {
decoration: Decoration{bold: true},
style: color.Style{color.OpBold},
},
"\x1b[1mfoo\x1b[0m",
},
{
"multiple attributes",
Expand All @@ -67,6 +79,7 @@ func TestMerge(t *testing.T) {
},
style: color.Style{color.OpBold, color.OpUnderscore},
},
"\x1b[1;4mfoo\x1b[0m",
},
{
"multiple attributes and colors",
Expand All @@ -80,6 +93,7 @@ func TestMerge(t *testing.T) {
},
style: color.Style{fgBlue, bgRed, color.OpBold, color.OpUnderscore},
},
"\x1b[34;41;1;4mfoo\x1b[0m",
},
{
"rgb fg color",
Expand All @@ -88,6 +102,8 @@ func TestMerge(t *testing.T) {
fg: &rgbPink,
style: color.NewRGBStyle(rgbPinkLib).SetOpts(color.Opts{}),
},
// '38;2' qualifies an RGB foreground color
"\x1b[38;2;255;0;255mfoo\x1b[0m",
},
{
"rgb fg and bg color",
Expand All @@ -97,6 +113,8 @@ func TestMerge(t *testing.T) {
bg: &rgbYellow,
style: color.NewRGBStyle(rgbPinkLib, rgbYellowLib).SetOpts(color.Opts{}),
},
// '48;2' qualifies an RGB background color
"\x1b[38;2;255;0;255;48;2;255;255;0mfoo\x1b[0m",
},
{
"rgb fg and bg color with opts",
Expand All @@ -110,6 +128,7 @@ func TestMerge(t *testing.T) {
},
style: color.NewRGBStyle(rgbPinkLib, rgbYellowLib).SetOpts(color.Opts{color.OpBold, color.OpUnderscore}),
},
"\x1b[38;2;255;0;255;48;2;255;255;0;1;4mfoo\x1b[0m",
},
{
"mix color-16 with rgb colors",
Expand All @@ -122,16 +141,19 @@ func TestMerge(t *testing.T) {
fgRed.RGB(), // We need to use FG here, https://github.com/gookit/color/issues/39
).SetOpts(color.Opts{}),
},
"\x1b[38;2;255;255;0;48;2;197;30;20mfoo\x1b[0m",
},
}

for _, s := range scenarios {
s := s
t.Run(s.name, func(t *testing.T) {
style := New()
for _, other := range s.toMerge {
style = style.MergeStyle(other)
}
assert.Equal(t, s.expectedStyle, style)
assert.Equal(t, s.expectedStr, style.Sprint(strToPrint))
})
}
}
2 changes: 1 addition & 1 deletion pkg/gui/style/text_style.go
Expand Up @@ -7,7 +7,7 @@ import (
// A TextStyle contains a foreground color, background color, and
// decorations (bold/underline/reverse).
//
// Colors may each be either 16-bit or 256-bit RGB colors. When
// Colors may each be either 16-bit or 24-bit RGB colors. When
// we need to produce a string with a TextStyle, if either foreground or
// background color is RGB, we'll promote the other color component to RGB as well.
// We could simplify this code by forcing everything to be RGB, but we're not
Expand Down

0 comments on commit e798aa4

Please sign in to comment.