Support ShowLabels, add tests

This commit is contained in:
Andinus 2024-11-16 10:40:45 +05:30
parent 83fa60f797
commit 5b7e923bd8
Signed by: andinus
GPG Key ID: B67D55D482A799FD
2 changed files with 91 additions and 6 deletions

@ -15,10 +15,12 @@ type Hash map[string]interface{}
// Option holds configuration for TemplateNest
type Option struct {
Delimiters [2]string
NameLabel string
TemplateDir string
TemplateExtension string
DieOnBadParams bool
NameLabel string // Identify the template to be used
TemplateDir string // directory where templates are located
TemplateExtension string // appended on the label to idenfity the template
DieOnBadParams bool // attempt to populate a variable that doesn't exist should result in an error
ShowLabels bool // Prepend & Append a string to every template, helpful in debugging
CommentDelimiters [2]string // Used in conjunction with ShowLabels, if HTML then use '<!--', '-->'
}
type TemplateNest struct {
@ -60,6 +62,9 @@ func New(opts Option) (*TemplateNest, error) {
if opts.Delimiters == [2]string{} {
opts.Delimiters = [2]string{"<!--%", "%-->"}
}
if opts.CommentDelimiters == [2]string{} {
opts.CommentDelimiters = [2]string{"<!--", "-->"}
}
if opts.NameLabel == "" {
opts.NameLabel = "TEMPLATE"
}
@ -271,6 +276,18 @@ func (nest *TemplateNest) Render(toRender interface{}) (string, error) {
rendered = rendered[:variable.StartPosition] + replacement + rendered[variable.EndPosition:]
}
if nest.option.ShowLabels {
labelStart := fmt.Sprintf(
"%s BEGIN %s %s\n",
nest.option.CommentDelimiters[0], tName, nest.option.CommentDelimiters[1],
)
labelEnd := fmt.Sprintf(
"%s END %s %s\n",
nest.option.CommentDelimiters[0], tName, nest.option.CommentDelimiters[1],
)
rendered = labelStart + rendered + labelEnd
}
return strings.TrimSpace(rendered), nil
default:

@ -0,0 +1,68 @@
package tests
import (
"git.virtual.blue/tomgracey/template-nest-go"
"github.com/stretchr/testify/assert"
"testing"
)
func TestRenderWithShowLabels(t *testing.T) {
nest, err := templatenest.New(templatenest.Option{
TemplateDir: "templates",
ShowLabels: true,
})
if err != nil {
t.Fatalf("Failed to initialize TemplateNest: %+v", err)
}
nestNoLabels, err := templatenest.New(templatenest.Option{TemplateDir: "templates"})
if err != nil {
t.Fatalf("Failed to initialize TemplateNest: %+v", err)
}
page := templatenest.Hash{
"TEMPLATE": "00-simple-page",
"variable": "Simple Variable",
"simple_component": templatenest.Hash{
"TEMPLATE": "01-simple-component",
"variable": "Simple Variable in Simple Component",
},
}
outputPage := templatenest.Hash{"TEMPLATE": "output/04-simple-page-with-labels"}
render := nest.MustRender(page)
outputRender := nestNoLabels.MustRender(outputPage)
assert.Equal(t, outputRender, render, "Rendered output does not match expected output")
}
func TestRenderWithShowLabelsAltDelimiters(t *testing.T) {
nest, err := templatenest.New(templatenest.Option{
TemplateDir: "templates",
ShowLabels: true,
CommentDelimiters: [2]string{"<!--!", "!-->"},
})
if err != nil {
t.Fatalf("Failed to initialize TemplateNest: %+v", err)
}
nestNoLabels, err := templatenest.New(templatenest.Option{TemplateDir: "templates"})
if err != nil {
t.Fatalf("Failed to initialize TemplateNest: %+v", err)
}
page := templatenest.Hash{
"TEMPLATE": "00-simple-page",
"variable": "Simple Variable",
"simple_component": templatenest.Hash{
"TEMPLATE": "01-simple-component",
"variable": "Simple Variable in Simple Component",
},
}
outputPage := templatenest.Hash{"TEMPLATE": "output/05-simple-page-with-labels-alt-delims"}
render := nest.MustRender(page)
outputRender := nestNoLabels.MustRender(outputPage)
assert.Equal(t, outputRender, render, "Rendered output does not match expected output")
}