Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions render/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,36 @@
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
}

Check warning on line 75 in render/html.go

View check run for this annotation

Codecov / codecov/patch

render/html.go#L74-L75

Added lines #L74 - L75 were not covered by tests
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
}

Check warning on line 82 in render/html.go

View check run for this annotation

Codecov / codecov/patch

render/html.go#L81-L82

Added lines #L81 - L82 were not covered by tests
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
}

Check warning on line 95 in render/html.go

View check run for this annotation

Codecov / codecov/patch

render/html.go#L94-L95

Added lines #L94 - L95 were not covered by tests
if r.Name == "" {
return r.Template.Execute(w, r.Data)
}
Expand Down
65 changes: 65 additions & 0 deletions render/html_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
5 changes: 4 additions & 1 deletion render/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down