1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-04-11 11:42:12 +02:00

add tests for TemplateFuncMapAddColors

This commit is contained in:
mjarkk 2021-08-09 21:09:52 +02:00
parent e8e4fa5957
commit e58376f9f7

@ -1,7 +1,9 @@
package style
import (
"bytes"
"testing"
"text/template"
"github.com/gookit/color"
"github.com/stretchr/testify/assert"
@ -157,3 +159,53 @@ func TestMerge(t *testing.T) {
})
}
}
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",
},
{
"multiple string with diffrent colors",
"{{ .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())
})
}
}