diff --git a/templates/components/role_test.go b/templates/components/role_test.go index 779a0fd..4126689 100644 --- a/templates/components/role_test.go +++ b/templates/components/role_test.go @@ -1,31 +1,20 @@ package components_test import ( - "bytes" "fmt" - "html/template" "testing" "git.sapphic.engineer/roleypoly/v4/presentation" - "git.sapphic.engineer/roleypoly/v4/templates" + "git.sapphic.engineer/roleypoly/v4/templates/templatetesting" "git.sapphic.engineer/roleypoly/v4/types" "git.sapphic.engineer/roleypoly/v4/types/fixtures" "git.sapphic.engineer/roleypoly/v4/utils" "github.com/stretchr/testify/assert" ) -var ( - Templates = template.Must(template.ParseFS(templates.FS, "components/*.html")) -) - func renderRole(t *testing.T, c *types.Category, r *types.Role, s bool) string { data := presentation.Role(c, r, s) - buf := bytes.Buffer{} - err := Templates.ExecuteTemplate(&buf, "role.html", data) - if err != nil { - t.Fatal("template failed to render", err) - } - return buf.String() + return templatetesting.Template(t, "components/role", data) } func TestRole(t *testing.T) { diff --git a/templates/templatetesting/templates.go b/templates/templatetesting/templates.go new file mode 100644 index 0000000..d9f0ac6 --- /dev/null +++ b/templates/templatetesting/templates.go @@ -0,0 +1,66 @@ +// Package testing provides test helpers that support fiber templates +package templatetesting + +import ( + "bytes" + "html/template" + "io/fs" + "strings" + "testing" + + "git.sapphic.engineer/roleypoly/v4/templates" +) + +var ( + funcMap = template.FuncMap{ + "embed": func() string { + return "%%EMBED%%" + }, + } + Templates *template.Template = template.New("index").Funcs(funcMap) +) + +func init() { + fs.WalkDir(templates.FS, ".", func(path string, d fs.DirEntry, err error) error { + if err != nil { + return err + } + + if d.IsDir() { + return nil + } + + fiberName := strings.Replace(path, ".html", "", 1) + f, err := templates.FS.ReadFile(path) + if err != nil { + return err + } + + if fiberName == "index" { + Templates, err = Templates.Parse(string(f)) + } else { + _, err = Templates.New(fiberName).Parse(string(f)) + } + + return err + }, + ) + +} + +func Template(t *testing.T, name string, data interface{}) string { + buf := bytes.Buffer{} + err := Templates.ExecuteTemplate(&buf, name, data) + if err != nil { + debugTemplates(t) + t.Fatal("failed to render: ", err) + } + + return buf.String() +} + +func debugTemplates(t *testing.T) { + for i, tmpl := range Templates.Templates() { + t.Logf("template %d: name=%s", i, tmpl.Name()) + } +} diff --git a/templates/templatetesting/templates_test.go b/templates/templatetesting/templates_test.go new file mode 100644 index 0000000..295b65d --- /dev/null +++ b/templates/templatetesting/templates_test.go @@ -0,0 +1,16 @@ +package templatetesting_test + +import ( + "testing" + + "git.sapphic.engineer/roleypoly/v4/templates/templatetesting" + "github.com/stretchr/testify/assert" +) + +func TestRender(t *testing.T) { + for _, template := range templatetesting.Templates.Templates() { + assert.NotPanicsf(t, func() { + templatetesting.Template(t, template.Name(), nil) + }, "rendering %s", template.Name()) + } +} diff --git a/templates/testing/templates.go b/templates/testing/templates.go deleted file mode 100644 index 823d4a7..0000000 --- a/templates/testing/templates.go +++ /dev/null @@ -1,14 +0,0 @@ -// Package testing provides test helpers that support fiber templates -package testing - -import ( - "html/template" - - "git.sapphic.engineer/roleypoly/v4/templates" -) - -var ( - Templates = template.Must(template.ParseFS(templates.FS, "*.html")).Funcs(template.FuncMap{ - "embed": func() {}, - }) -) diff --git a/templates/testing/templates_test.go b/templates/testing/templates_test.go deleted file mode 100644 index 4568c0f..0000000 --- a/templates/testing/templates_test.go +++ /dev/null @@ -1,7 +0,0 @@ -package testing_test - -// func Test(t *testing.T) { - -// } - -// TODO: test the template tester