go/webutil/template_test.go

50 lines
795 B
Go
Raw Permalink Normal View History

2024-11-11 05:36:55 +00:00
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)
}
}
}