template-nest-go/tests/02_die_on_bad_params_test.go

73 lines
1.8 KiB
Go

package tests
import (
"git.virtual.blue/tomgracey/template-nest-go"
"github.com/stretchr/testify/assert"
"testing"
)
func TestDieOnPageWithBadParams(t *testing.T) {
nest, err := templatenest.New(templatenest.Option{
TemplateDir: "templates",
DieOnBadParams: true,
})
if err != nil {
t.Fatalf("Failed to initialize TemplateNest: %+v", err)
}
page := templatenest.Hash{
"TEMPLATE": "00-simple-page",
"variable": "Simple Variable",
"simple_component": "Simple Component",
"a_bad_param": "Bad Param",
}
_, err = nest.Render(page)
if assert.NotNil(t, err) {
expected := "bad params in template hash, variable not present in template file: `a_bad_param`"
assert.Equal(t, expected, err.Error())
}
}
func TestDieOnPageWithBadParams01(t *testing.T) {
nest, err := templatenest.New(templatenest.Option{
TemplateDir: "templates",
DieOnBadParams: true,
})
if err != nil {
t.Fatalf("Failed to initialize TemplateNest: %+v", err)
}
page := templatenest.Hash{
"TEMPLATE": "00-simple-page",
"variable": "Simple Variable",
"a_bad_param": "Bad Param",
}
_, err = nest.Render(page)
if assert.NotNil(t, err) {
expected := "bad params in template hash, variable not present in template file: `a_bad_param`"
assert.Equal(t, expected, err.Error())
}
}
func TestLiveOnPageWithBadParams(t *testing.T) {
nest, err := templatenest.New(templatenest.Option{
TemplateDir: "templates",
DieOnBadParams: false,
})
if err != nil {
t.Fatalf("Failed to initialize TemplateNest: %+v", err)
}
page := templatenest.Hash{
"TEMPLATE": "00-simple-page",
"variable": "Simple Variable",
"simple_component": "Simple Component",
"a_bad_param": "Bad Param",
}
_, err = nest.Render(page)
assert.Nil(t, err)
}