summaryrefslogtreecommitdiffstats
path: root/tpl/tplimpl/template_ast_transformers_test.go
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2019-04-16 09:13:55 +0200
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2019-04-16 21:24:09 +0200
commit2957795f5276cc9bc8d438da2d7d9b61defea225 (patch)
treecdd3e3acda4e5603339f7b18737d41f1c9866c5c /tpl/tplimpl/template_ast_transformers_test.go
parent56550d1e449f45ebee398ac8a9e3b9818b3ee60e (diff)
tpl/tplimpl: Handle late transformation of templates
Fixes #5865
Diffstat (limited to 'tpl/tplimpl/template_ast_transformers_test.go')
-rw-r--r--tpl/tplimpl/template_ast_transformers_test.go56
1 files changed, 35 insertions, 21 deletions
diff --git a/tpl/tplimpl/template_ast_transformers_test.go b/tpl/tplimpl/template_ast_transformers_test.go
index 9ed29d27f..be1efc522 100644
--- a/tpl/tplimpl/template_ast_transformers_test.go
+++ b/tpl/tplimpl/template_ast_transformers_test.go
@@ -26,10 +26,6 @@ import (
"github.com/stretchr/testify/require"
)
-type handler interface {
- addTemplate(name, tpl string) error
-}
-
var (
testFuncs = map[string]interface{}{
"getif": func(v interface{}) interface{} { return v },
@@ -413,7 +409,7 @@ func TestInsertIsZeroFunc(t *testing.T) {
"T": &T{NonEmptyInterfaceTypedNil: (*T)(nil)},
}
- templ = `
+ templ1 = `
{{ if .True }}.True: TRUE{{ else }}.True: FALSE{{ end }}
{{ if .TimeZero }}.TimeZero1: TRUE{{ else }}.TimeZero1: FALSE{{ end }}
{{ if (.TimeZero) }}.TimeZero2: TRUE{{ else }}.TimeZero2: FALSE{{ end }}
@@ -423,31 +419,49 @@ func TestInsertIsZeroFunc(t *testing.T) {
{{ template "mytemplate" . }}
{{ if .T.NonEmptyInterfaceTypedNil }}.NonEmptyInterfaceTypedNil: TRUE{{ else }}.NonEmptyInterfaceTypedNil: FALSE{{ end }}
+{{ template "other-file-template" . }}
{{ define "mytemplate" }}
{{ if .TimeZero }}.TimeZero1: mytemplate: TRUE{{ else }}.TimeZero1: mytemplate: FALSE{{ end }}
{{ end }}
`
+
+ // https://github.com/gohugoio/hugo/issues/5865
+ templ2 = `{{ define "other-file-template" }}
+{{ if .TimeZero }}.TimeZero1: other-file-template: TRUE{{ else }}.TimeZero1: other-file-template: FALSE{{ end }}
+{{ end }}
+`
)
d := newD(assert)
- h := d.Tmpl.(handler)
-
- assert.NoError(h.addTemplate("mytemplate.html", templ))
-
- tt, _ := d.Tmpl.Lookup("mytemplate.html")
- result, err := tt.(tpl.TemplateExecutor).ExecuteToString(ctx)
- assert.NoError(err)
-
- assert.Contains(result, ".True: TRUE")
- assert.Contains(result, ".TimeZero1: FALSE")
- assert.Contains(result, ".TimeZero2: FALSE")
- assert.Contains(result, ".TimeZero3: TRUE")
- assert.Contains(result, ".Now: TRUE")
- assert.Contains(result, "TimeZero1 with: FALSE")
- assert.Contains(result, ".TimeZero1: mytemplate: FALSE")
- assert.Contains(result, ".NonEmptyInterfaceTypedNil: FALSE")
+ h := d.Tmpl.(tpl.TemplateHandler)
+
+ // HTML templates
+ assert.NoError(h.AddTemplate("mytemplate.html", templ1))
+ assert.NoError(h.AddTemplate("othertemplate.html", templ2))
+
+ // Text templates
+ assert.NoError(h.AddTemplate("_text/mytexttemplate.txt", templ1))
+ assert.NoError(h.AddTemplate("_text/myothertexttemplate.txt", templ2))
+
+ assert.NoError(h.MarkReady())
+
+ for _, name := range []string{"mytemplate.html", "mytexttemplate.txt"} {
+ tt, _ := d.Tmpl.Lookup(name)
+ result, err := tt.(tpl.TemplateExecutor).ExecuteToString(ctx)
+ assert.NoError(err)
+
+ assert.Contains(result, ".True: TRUE")
+ assert.Contains(result, ".TimeZero1: FALSE")
+ assert.Contains(result, ".TimeZero2: FALSE")
+ assert.Contains(result, ".TimeZero3: TRUE")
+ assert.Contains(result, ".Now: TRUE")
+ assert.Contains(result, "TimeZero1 with: FALSE")
+ assert.Contains(result, ".TimeZero1: mytemplate: FALSE")
+ assert.Contains(result, ".TimeZero1: other-file-template: FALSE")
+ assert.Contains(result, ".NonEmptyInterfaceTypedNil: FALSE")
+ }
}