diff options
author | mjarkk <mkopenga@gmail.com> | 2021-08-09 21:09:52 +0200 |
---|---|---|
committer | mjarkk <mkopenga@gmail.com> | 2021-08-09 21:09:52 +0200 |
commit | e58376f9f7e81ec5744d667a47fe6670e7a360a4 (patch) | |
tree | f4cdfc1905a05096439f44b29868635e9131d080 /pkg/gui/style | |
parent | e8e4fa59572915f669c5daf272bb5721f53de2d7 (diff) |
add tests for TemplateFuncMapAddColors
Diffstat (limited to 'pkg/gui/style')
-rw-r--r-- | pkg/gui/style/style_test.go | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/pkg/gui/style/style_test.go b/pkg/gui/style/style_test.go index 298dae843..8298076a5 100644 --- a/pkg/gui/style/style_test.go +++ b/pkg/gui/style/style_test.go @@ -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()) + }) + } +} |