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{ "TEMPLATE": "03-var-at-begin-with-space", } outputPath := "templates/output/10-var-at-begin-with-space.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", ) }