template-nest-go/tests/07_token_escape_char_test.go

98 lines
2.3 KiB
Go
Raw Permalink Normal View History

2024-11-16 06:18:50 +00:00
package tests
import (
"git.virtual.blue/tomgracey/template-nest-go"
"github.com/stretchr/testify/assert"
"io/ioutil"
"strings"
"testing"
)
func TestRenderWithEscapedVariable(t *testing.T) {
nest, err := templatenest.New(templatenest.Option{
TemplateDir: "templates",
TokenEscapeChar: "\\",
})
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{
templatenest.Hash{
"TEMPLATE": "01-simple-component-token-escape",
},
},
}
outputPath := "templates/output/09-simple-page-token-escape.html"
outputContents, err := ioutil.ReadFile(outputPath)
if err != nil {
t.Fatalf("error reading file (`%s`): %+v", outputPath, err)
}
assert.Equal(
t,
strings.TrimSpace(string(outputContents)),
nest.MustRender(page),
"Rendered output does not match expected output",
)
}
func TestRenderWithEscapedVariableAtStart(t *testing.T) {
nest, err := templatenest.New(templatenest.Option{
TemplateDir: "templates",
TokenEscapeChar: "\\",
})
if err != nil {
t.Fatalf("Failed to initialize TemplateNest: %+v", err)
}
page := templatenest.Hash{
"TEMPLATE": "03-var-at-begin",
"variable": "Simple Variable",
}
outputPath := "templates/output/10-var-at-begin.html"
outputContents, err := ioutil.ReadFile(outputPath)
if err != nil {
t.Fatalf("error reading file (`%s`): %+v", outputPath, err)
}
assert.Equal(
t,
strings.TrimSpace(string(outputContents)),
nest.MustRender(page),
"Rendered output does not match expected output",
)
}
func TestRenderWithEscapedVariableAtStart01(t *testing.T) {
nest, err := templatenest.New(templatenest.Option{
TemplateDir: "templates",
TokenEscapeChar: "\\",
})
if err != nil {
t.Fatalf("Failed to initialize TemplateNest: %+v", err)
}
page := templatenest.Hash{
2024-11-18 16:55:17 +00:00
"TEMPLATE": "03-var-at-begin-with-escape",
2024-11-16 06:18:50 +00:00
}
2024-11-18 16:55:17 +00:00
outputPath := "templates/output/10-var-at-begin-with-escape.html"
2024-11-16 06:18:50 +00:00
outputContents, err := ioutil.ReadFile(outputPath)
if err != nil {
t.Fatalf("error reading file (`%s`): %+v", outputPath, err)
}
assert.Equal(
t,
strings.TrimSpace(string(outputContents)),
nest.MustRender(page),
"Rendered output does not match expected output",
)
}