template testing helpers
This commit is contained in:
parent
df33164b08
commit
1c533926ca
5 changed files with 84 additions and 34 deletions
|
@ -1,31 +1,20 @@
|
||||||
package components_test
|
package components_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"git.sapphic.engineer/roleypoly/v4/presentation"
|
"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"
|
||||||
"git.sapphic.engineer/roleypoly/v4/types/fixtures"
|
"git.sapphic.engineer/roleypoly/v4/types/fixtures"
|
||||||
"git.sapphic.engineer/roleypoly/v4/utils"
|
"git.sapphic.engineer/roleypoly/v4/utils"
|
||||||
"github.com/stretchr/testify/assert"
|
"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 {
|
func renderRole(t *testing.T, c *types.Category, r *types.Role, s bool) string {
|
||||||
data := presentation.Role(c, r, s)
|
data := presentation.Role(c, r, s)
|
||||||
buf := bytes.Buffer{}
|
return templatetesting.Template(t, "components/role", data)
|
||||||
err := Templates.ExecuteTemplate(&buf, "role.html", data)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal("template failed to render", err)
|
|
||||||
}
|
|
||||||
return buf.String()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRole(t *testing.T) {
|
func TestRole(t *testing.T) {
|
||||||
|
|
66
templates/templatetesting/templates.go
Normal file
66
templates/templatetesting/templates.go
Normal file
|
@ -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())
|
||||||
|
}
|
||||||
|
}
|
16
templates/templatetesting/templates_test.go
Normal file
16
templates/templatetesting/templates_test.go
Normal file
|
@ -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())
|
||||||
|
}
|
||||||
|
}
|
|
@ -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() {},
|
|
||||||
})
|
|
||||||
)
|
|
|
@ -1,7 +0,0 @@
|
||||||
package testing_test
|
|
||||||
|
|
||||||
// func Test(t *testing.T) {
|
|
||||||
|
|
||||||
// }
|
|
||||||
|
|
||||||
// TODO: test the template tester
|
|
Loading…
Add table
Add a link
Reference in a new issue