Merge a4006e2db5dbdb7cff4d00ff56719b84c855dd1f into 49e9137c68e6dfaa529a2d0c9fe64d9e69a8554e

This commit is contained in:
ljluestc 2025-04-11 16:01:03 +00:00 committed by GitHub
commit be16d6346e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 85 additions and 4 deletions

View File

@ -63,23 +63,36 @@ func (r HTMLDebug) Instance(name string, data any) Render {
Data: data,
}
}
func (r HTMLDebug) loadTemplate() *template.Template {
if r.FuncMap == nil {
r.FuncMap = template.FuncMap{}
}
if len(r.Files) > 0 {
return template.Must(template.New("").Delims(r.Delims.Left, r.Delims.Right).Funcs(r.FuncMap).ParseFiles(r.Files...))
tmpl, err := template.New("").Delims(r.Delims.Left, r.Delims.Right).Funcs(r.FuncMap).ParseFiles(r.Files...)
if err != nil {
return nil // Return nil instead of panicking
}
return tmpl
}
if r.Glob != "" {
return template.Must(template.New("").Delims(r.Delims.Left, r.Delims.Right).Funcs(r.FuncMap).ParseGlob(r.Glob))
tmpl, err := template.New("").Delims(r.Delims.Left, r.Delims.Right).Funcs(r.FuncMap).ParseGlob(r.Glob)
if err != nil {
return nil // Return nil instead of panicking
}
return tmpl
}
panic("the HTML debug render was created without files or glob pattern")
// Return nil instead of panicking when no files or glob are provided
return nil
}
// Render (HTML) executes template and writes its result with custom ContentType for response.
func (r HTML) Render(w http.ResponseWriter) error {
r.WriteContentType(w)
if r.Template == nil {
return nil // Handle nil template gracefully
}
if r.Name == "" {
return r.Template.Execute(w, r.Data)
}

65
render/html_test.go Normal file
View File

@ -0,0 +1,65 @@
// render/html_test.go
package render
import (
"html/template"
"testing"
"github.com/stretchr/testify/assert"
)
// TestHTMLProductionInstanceSuccess tests a successful template instance in production
func TestHTMLProductionInstanceSuccess(t *testing.T) {
tmpl := template.Must(template.New("test").Parse("Hello {{.}}"))
renderer := HTMLProduction{Template: tmpl}
instance := renderer.Instance("test", "World")
html, ok := instance.(HTML)
assert.True(t, ok, "Instance should be of type HTML")
assert.Equal(t, "test", html.Name)
assert.Equal(t, "World", html.Data)
}
// TestHTMLProductionInstanceNilTemplate tests when template is not initialized
func TestHTMLProductionInstanceNilTemplate(t *testing.T) {
renderer := HTMLProduction{Template: nil}
instance := renderer.Instance("test", "World")
html, ok := instance.(HTML)
assert.True(t, ok, "Instance should be of type HTML")
assert.Nil(t, html.Template, "Template should be nil when not initialized")
}
// TestHTMLDebugInstanceSuccess tests a successful template instance in debug mode
func TestHTMLDebugInstanceSuccess(t *testing.T) {
renderer := HTMLDebug{
Files: nil, // No files provided
Delims: Delims{Left: "{{", Right: "}}"},
FuncMap: template.FuncMap{},
}
// Since no files or glob are provided, Template will be nil
instance := renderer.Instance("inline", "World")
html, ok := instance.(HTML)
assert.True(t, ok, "Instance should be of type HTML")
assert.Equal(t, "inline", html.Name)
assert.Equal(t, "World", html.Data)
assert.Nil(t, html.Template, "Template should be nil since no files or glob provided")
}
// TestHTMLDebugInstanceNoFilesOrGlob tests behavior when no files or glob are provided
func TestHTMLDebugInstanceNoFilesOrGlob(t *testing.T) {
renderer := HTMLDebug{
Files: nil,
Glob: "",
Delims: Delims{Left: "{{", Right: "}}"},
FuncMap: template.FuncMap{},
}
instance := renderer.Instance("test", "World")
html, ok := instance.(HTML)
assert.True(t, ok, "Instance should be of type HTML")
assert.Equal(t, "test", html.Name)
assert.Equal(t, "World", html.Data)
assert.Nil(t, html.Template, "Template should be nil when no files or glob provided")
}

View File

@ -531,7 +531,10 @@ func TestRenderHTMLDebugPanics(t *testing.T) {
Delims: Delims{"{{", "}}"},
FuncMap: nil,
}
assert.Panics(t, func() { htmlRender.Instance("", nil) })
instance := htmlRender.Instance("", nil)
html, ok := instance.(HTML)
assert.True(t, ok, "Instance should be of type HTML")
assert.Nil(t, html.Template, "Template should be nil when no files or glob provided")
}
func TestRenderReader(t *testing.T) {