go/webutil/template_test.go
2024-11-11 06:36:55 +01:00

50 lines
795 B
Go

package webutil
import (
"bytes"
"embed"
"html/template"
"strings"
"testing"
)
//go:embed all:test-templates
var testFS embed.FS
func TestParseTemplateSet(t *testing.T) {
funcs := template.FuncMap{"join": strings.Join}
m := ParseTemplateSet(funcs, testFS)
type TestCase struct {
Key string
Data any
Out string
}
cases := []TestCase{
{
Key: "/home.html",
Data: "DATA",
Out: "<p>HOME!</p>",
}, {
Key: "/about.html",
Data: "DATA",
Out: "<p><b>DATA</b></p>",
}, {
Key: "/contact.html",
Data: []string{"a", "b", "c"},
Out: "<p>a,b,c</p>",
},
}
for _, tc := range cases {
b := &bytes.Buffer{}
m[tc.Key].Execute(b, tc.Data)
out := strings.TrimSpace(b.String())
if out != tc.Out {
t.Fatalf("%s != %s", out, tc.Out)
}
}
}