wip
This commit is contained in:
49
webutil/template_test.go
Normal file
49
webutil/template_test.go
Normal file
@@ -0,0 +1,49 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user