1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-12 11:15:00 +02:00
lazygit/pkg/gui/style/style_test.go

227 lines
5.1 KiB
Go
Raw Normal View History

package style
import (
2021-08-09 21:09:52 +02:00
"bytes"
"testing"
2021-08-09 21:09:52 +02:00
"text/template"
"github.com/gookit/color"
"github.com/stretchr/testify/assert"
2021-08-01 04:54:17 +02:00
"github.com/xo/terminfo"
)
2021-11-01 00:16:45 +02:00
func init() {
// on CI we've got no color capability so we're forcing it here
color.ForceSetColorLevel(terminfo.ColorLevelMillions)
}
2021-07-31 20:48:40 +02:00
func TestMerge(t *testing.T) {
type scenario struct {
name string
2021-07-31 20:48:40 +02:00
toMerge []TextStyle
expectedStyle TextStyle
2021-08-01 04:54:17 +02:00
expectedStr string
}
2021-07-31 20:48:40 +02:00
fgRed := color.FgRed
bgRed := color.BgRed
fgBlue := color.FgBlue
rgbPinkLib := color.Rgb(0xFF, 0x00, 0xFF)
rgbPink := NewRGBColor(rgbPinkLib)
rgbYellowLib := color.Rgb(0xFF, 0xFF, 0x00)
rgbYellow := NewRGBColor(rgbYellowLib)
2021-08-01 04:54:17 +02:00
strToPrint := "foo"
scenarios := []scenario{
{
"no color",
2021-07-31 20:48:40 +02:00
nil,
2021-11-02 07:39:15 +02:00
TextStyle{Style: color.Style{}},
2021-08-01 04:54:17 +02:00
"foo",
},
{
"only fg color",
2021-07-31 20:48:40 +02:00
[]TextStyle{FgRed},
2021-11-02 07:39:15 +02:00
TextStyle{fg: &Color{basic: &fgRed}, Style: color.Style{fgRed}},
2021-08-01 04:54:17 +02:00
"\x1b[31mfoo\x1b[0m",
},
{
"only bg color",
2021-07-31 20:48:40 +02:00
[]TextStyle{BgRed},
2021-11-02 07:39:15 +02:00
TextStyle{bg: &Color{basic: &bgRed}, Style: color.Style{bgRed}},
2021-08-01 04:54:17 +02:00
"\x1b[41mfoo\x1b[0m",
},
{
"fg and bg color",
2021-07-31 20:48:40 +02:00
[]TextStyle{FgBlue, BgRed},
TextStyle{
fg: &Color{basic: &fgBlue},
bg: &Color{basic: &bgRed},
2021-11-02 07:39:15 +02:00
Style: color.Style{fgBlue, bgRed},
},
2021-08-01 04:54:17 +02:00
"\x1b[34;41mfoo\x1b[0m",
},
{
2021-07-31 20:48:40 +02:00
"single attribute",
[]TextStyle{AttrBold},
TextStyle{
decoration: Decoration{bold: true},
2021-11-02 07:39:15 +02:00
Style: color.Style{color.OpBold},
},
2021-08-01 04:54:17 +02:00
"\x1b[1mfoo\x1b[0m",
},
{
2021-07-31 20:48:40 +02:00
"multiple attributes",
[]TextStyle{AttrBold, AttrUnderline},
TextStyle{
decoration: Decoration{
bold: true,
underline: true,
},
2021-11-02 07:39:15 +02:00
Style: color.Style{color.OpBold, color.OpUnderscore},
},
2021-08-01 04:54:17 +02:00
"\x1b[1;4mfoo\x1b[0m",
},
{
2021-07-31 20:48:40 +02:00
"multiple attributes and colors",
[]TextStyle{AttrBold, FgBlue, AttrUnderline, BgRed},
TextStyle{
fg: &Color{basic: &fgBlue},
bg: &Color{basic: &bgRed},
decoration: Decoration{
bold: true,
underline: true,
},
2021-11-02 07:39:15 +02:00
Style: color.Style{fgBlue, bgRed, color.OpBold, color.OpUnderscore},
},
2021-08-01 04:54:17 +02:00
"\x1b[34;41;1;4mfoo\x1b[0m",
},
{
2021-07-31 20:48:40 +02:00
"rgb fg color",
[]TextStyle{New().SetFg(rgbPink)},
TextStyle{
fg: &rgbPink,
2021-11-02 07:39:15 +02:00
Style: color.NewRGBStyle(rgbPinkLib).SetOpts(color.Opts{}),
},
2021-08-01 04:54:17 +02:00
// '38;2' qualifies an RGB foreground color
"\x1b[38;2;255;0;255mfoo\x1b[0m",
},
{
2021-07-31 20:48:40 +02:00
"rgb fg and bg color",
[]TextStyle{New().SetFg(rgbPink).SetBg(rgbYellow)},
TextStyle{
fg: &rgbPink,
bg: &rgbYellow,
2021-11-02 07:39:15 +02:00
Style: color.NewRGBStyle(rgbPinkLib, rgbYellowLib).SetOpts(color.Opts{}),
},
2021-08-01 04:54:17 +02:00
// '48;2' qualifies an RGB background color
"\x1b[38;2;255;0;255;48;2;255;255;0mfoo\x1b[0m",
},
{
2021-07-31 20:48:40 +02:00
"rgb fg and bg color with opts",
[]TextStyle{AttrBold, New().SetFg(rgbPink).SetBg(rgbYellow), AttrUnderline},
TextStyle{
fg: &rgbPink,
bg: &rgbYellow,
decoration: Decoration{
bold: true,
underline: true,
},
2021-11-02 07:39:15 +02:00
Style: color.NewRGBStyle(rgbPinkLib, rgbYellowLib).SetOpts(color.Opts{color.OpBold, color.OpUnderscore}),
},
2021-08-01 04:54:17 +02:00
"\x1b[38;2;255;0;255;48;2;255;255;0;1;4mfoo\x1b[0m",
},
{
"mix color-16 (background) with rgb (foreground)",
2021-07-31 20:48:40 +02:00
[]TextStyle{New().SetFg(rgbYellow), BgRed},
TextStyle{
fg: &rgbYellow,
bg: &Color{basic: &bgRed},
2021-11-02 07:39:15 +02:00
Style: color.NewRGBStyle(
2021-07-31 20:48:40 +02:00
rgbYellowLib,
fgRed.RGB(), // We need to use FG here, https://github.com/gookit/color/issues/39
).SetOpts(color.Opts{}),
},
2021-08-01 04:54:17 +02:00
"\x1b[38;2;255;255;0;48;2;197;30;20mfoo\x1b[0m",
},
{
"mix color-16 (foreground) with rgb (background)",
[]TextStyle{FgRed, New().SetBg(rgbYellow)},
TextStyle{
fg: &Color{basic: &fgRed},
bg: &rgbYellow,
Style: color.NewRGBStyle(
fgRed.RGB(),
rgbYellowLib,
).SetOpts(color.Opts{}),
},
"\x1b[38;2;197;30;20;48;2;255;255;0mfoo\x1b[0m",
},
}
for _, s := range scenarios {
2021-08-01 04:54:17 +02:00
s := s
t.Run(s.name, func(t *testing.T) {
2021-07-31 20:48:40 +02:00
style := New()
for _, other := range s.toMerge {
style = style.MergeStyle(other)
}
2021-07-31 20:48:40 +02:00
assert.Equal(t, s.expectedStyle, style)
2021-08-01 04:54:17 +02:00
assert.Equal(t, s.expectedStr, style.Sprint(strToPrint))
})
}
}
2021-08-09 21:09:52 +02:00
func TestTemplateFuncMapAddColors(t *testing.T) {
type scenario struct {
name string
tmpl string
expect string
}
scenarios := []scenario{
{
"normal template",
"{{ .Foo }}",
"bar",
},
{
"colored string",
"{{ .Foo | red }}",
"\x1b[31mbar\x1b[0m",
},
{
"string with decorator",
"{{ .Foo | bold }}",
"\x1b[1mbar\x1b[0m",
},
{
"string with color and decorator",
"{{ .Foo | bold | red }}",
"\x1b[31m\x1b[1mbar\x1b[0m\x1b[0m",
},
{
2021-09-01 22:51:24 +02:00
"multiple string with different colors",
2021-08-09 21:09:52 +02:00
"{{ .Foo | red }} - {{ .Foo | blue }}",
"\x1b[31mbar\x1b[0m - \x1b[34mbar\x1b[0m",
},
}
for _, s := range scenarios {
s := s
t.Run(s.name, func(t *testing.T) {
tmpl, err := template.New("test template").Funcs(TemplateFuncMapAddColors(template.FuncMap{})).Parse(s.tmpl)
assert.NoError(t, err)
buff := bytes.NewBuffer(nil)
err = tmpl.Execute(buff, struct{ Foo string }{"bar"})
assert.NoError(t, err)
assert.Equal(t, s.expect, buff.String())
})
}
}