Add extension, delimiter tests & various fixes
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"git.virtual.blue/tomgracey/template-nest-go"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRenderWithJsExtension(t *testing.T) {
|
||||
ext := "js"
|
||||
nest, err := templatenest.New(templatenest.Option{
|
||||
TemplateDir: "templates",
|
||||
TemplateExtension: &ext,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to initialize TemplateNest: %+v", err)
|
||||
}
|
||||
|
||||
page := map[string]interface{}{
|
||||
"TEMPLATE": "30-main",
|
||||
"var": "Simple Variable",
|
||||
}
|
||||
|
||||
render := nest.MustRender(page)
|
||||
|
||||
outputPath := "templates/output/06-main-template-extension.js"
|
||||
outputContents, err := ioutil.ReadFile(outputPath)
|
||||
if err != nil {
|
||||
t.Fatalf("error reading file (`%s`): %+v", outputPath, err)
|
||||
}
|
||||
|
||||
assert.Equal(
|
||||
t,
|
||||
strings.TrimSpace(string(outputContents)),
|
||||
render,
|
||||
"Rendered output does not match expected output",
|
||||
)
|
||||
}
|
||||
|
||||
func TestRenderWithNoExtension(t *testing.T) {
|
||||
ext := ""
|
||||
nest, err := templatenest.New(templatenest.Option{
|
||||
TemplateDir: "templates",
|
||||
TemplateExtension: &ext,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to initialize TemplateNest: %+v", err)
|
||||
}
|
||||
|
||||
page := map[string]interface{}{
|
||||
"TEMPLATE": "00-simple-page.html",
|
||||
"variable": "Simple Variable",
|
||||
"simple_component": []interface{}{
|
||||
map[string]interface{}{
|
||||
"TEMPLATE": "01-simple-component.html",
|
||||
"variable": "Simple Variable in Simple Component",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
render := nest.MustRender(page)
|
||||
|
||||
outputPath := "templates/output/01-simple-page.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)),
|
||||
render,
|
||||
"Rendered output does not match expected output",
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"git.virtual.blue/tomgracey/template-nest-go"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRenderWithAltDelim(t *testing.T) {
|
||||
nest, err := templatenest.New(templatenest.Option{
|
||||
TemplateDir: "templates",
|
||||
Delimiters: [2]string{"<%", "%>"},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to initialize TemplateNest: %+v", err)
|
||||
}
|
||||
|
||||
page := map[string]interface{}{
|
||||
"TEMPLATE": "00-simple-page-alt-delim",
|
||||
"variable": "Simple Variable",
|
||||
"simple_component": map[string]interface{}{
|
||||
"TEMPLATE": "01-simple-component-alt-delim",
|
||||
"variable": "Simple Variable in Simple Component",
|
||||
},
|
||||
}
|
||||
|
||||
render := nest.MustRender(page)
|
||||
|
||||
outputPath := "templates/output/01-simple-page.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)),
|
||||
render,
|
||||
"Rendered output does not match expected output",
|
||||
)
|
||||
}
|
||||
|
||||
func TestRenderWithAltDelimAndFixedIndent(t *testing.T) {
|
||||
nest, err := templatenest.New(templatenest.Option{
|
||||
TemplateDir: "templates",
|
||||
Delimiters: [2]string{"<%", "%>"},
|
||||
FixedIndent: true,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to initialize TemplateNest: %+v", err)
|
||||
}
|
||||
|
||||
page := map[string]interface{}{
|
||||
"TEMPLATE": "00-simple-page-alt-delim",
|
||||
"variable": "Simple Variable",
|
||||
"simple_component": map[string]interface{}{
|
||||
"TEMPLATE": "02-simple-component-multi-line-alt-delim",
|
||||
},
|
||||
}
|
||||
|
||||
render := nest.MustRender(page)
|
||||
|
||||
outputPath := "templates/output/07-simple-page-fixed-indent.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)),
|
||||
render,
|
||||
"Rendered output does not match expected output",
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user