From e58376f9f7e81ec5744d667a47fe6670e7a360a4 Mon Sep 17 00:00:00 2001 From: mjarkk Date: Mon, 9 Aug 2021 21:09:52 +0200 Subject: [PATCH] add tests for TemplateFuncMapAddColors --- pkg/gui/style/style_test.go | 52 +++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) 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()) + }) + } +}