...
1 package termenv
2
3 import (
4 "bytes"
5 "fmt"
6 "io/ioutil"
7 "testing"
8 "text/template"
9 )
10
11 func TestTemplateFuncs(t *testing.T) {
12 tests := []struct {
13 name string
14 profile Profile
15 }{
16 {"ascii", Ascii},
17 {"ansi", ANSI},
18 {"ansi256", ANSI256},
19 {"truecolor", TrueColor},
20 }
21 const templateFile = "./testdata/templatehelper.tpl"
22 for _, test := range tests {
23 t.Run(test.name, func(t *testing.T) {
24 tpl, err := template.New("templatehelper.tpl").Funcs(TemplateFuncs(test.profile)).ParseFiles(templateFile)
25 if err != nil {
26 t.Fatalf("unexpected error parsing template: %v", err)
27 }
28 var buf bytes.Buffer
29 if err = tpl.Execute(&buf, nil); err != nil {
30 t.Fatalf("unexpected error executing template: %v", err)
31 }
32 actual := buf.Bytes()
33 filename := fmt.Sprintf("./testdata/templatehelper_%s.txt", test.name)
34 expected, err := ioutil.ReadFile(filename)
35 if err != nil {
36 t.Fatalf("unexpected error reading golden file %q: %v", filename, err)
37 }
38 if !bytes.Equal(buf.Bytes(), expected) {
39 t.Fatalf("template output does not match golden file.\n--- Expected ---\n%s\n--- Actual ---\n%s\n", string(expected), string(actual))
40 }
41 })
42 }
43 }
44
View as plain text