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