mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-14 12:12:12 +08:00
Update HTMLDebug to return nil instead of panicking and adjust tests
This commit is contained in:
parent
8763f33c65
commit
a4006e2db5
@ -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
65
render/html_test.go
Normal 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")
|
||||
}
|
@ -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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user