2019-01-02 06:33:26 -05:00
|
|
|
// Copyright 2019 The Hugo Authors. All rights reserved.
|
2016-10-24 16:29:48 -04:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2017-02-17 07:30:50 -05:00
|
|
|
package tplimpl
|
2016-10-24 16:29:48 -04:00
|
|
|
|
|
|
|
import (
|
2019-12-10 13:56:44 -05:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
template "github.com/gohugoio/hugo/tpl/internal/go_templates/htmltemplate"
|
2019-12-10 02:02:15 -05:00
|
|
|
|
2016-10-24 16:29:48 -04:00
|
|
|
"testing"
|
2019-03-06 03:07:49 -05:00
|
|
|
"time"
|
2016-10-24 16:29:48 -04:00
|
|
|
|
2019-03-06 03:07:49 -05:00
|
|
|
"github.com/gohugoio/hugo/tpl"
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
qt "github.com/frankban/quicktest"
|
2016-10-24 16:29:48 -04:00
|
|
|
)
|
|
|
|
|
2017-02-18 03:08:00 -05:00
|
|
|
// Issue #2927
|
|
|
|
func TestTransformRecursiveTemplate(t *testing.T) {
|
2019-08-10 15:05:17 -04:00
|
|
|
c := qt.New(t)
|
2017-02-18 03:08:00 -05:00
|
|
|
|
|
|
|
recursive := `
|
|
|
|
{{ define "menu-nodes" }}
|
|
|
|
{{ template "menu-node" }}
|
|
|
|
{{ end }}
|
|
|
|
{{ define "menu-node" }}
|
|
|
|
{{ template "menu-node" }}
|
|
|
|
{{ end }}
|
|
|
|
{{ template "menu-nodes" }}
|
|
|
|
`
|
|
|
|
|
|
|
|
templ, err := template.New("foo").Parse(recursive)
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
2017-02-18 03:08:00 -05:00
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
ctx := newTemplateContext(createParseTreeLookup(templ))
|
|
|
|
ctx.applyTransformations(templ.Tree.Root)
|
2019-03-06 03:07:49 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
type I interface {
|
|
|
|
Method0()
|
|
|
|
}
|
|
|
|
|
|
|
|
type T struct {
|
|
|
|
NonEmptyInterfaceTypedNil I
|
|
|
|
}
|
|
|
|
|
|
|
|
func (T) Method0() {
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInsertIsZeroFunc(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
c := qt.New(t)
|
2019-03-06 03:07:49 -05:00
|
|
|
|
|
|
|
var (
|
|
|
|
ctx = map[string]interface{}{
|
|
|
|
"True": true,
|
|
|
|
"Now": time.Now(),
|
|
|
|
"TimeZero": time.Time{},
|
|
|
|
"T": &T{NonEmptyInterfaceTypedNil: (*T)(nil)},
|
|
|
|
}
|
|
|
|
|
2019-04-16 03:13:55 -04:00
|
|
|
templ1 = `
|
2019-03-06 03:07:49 -05:00
|
|
|
{{ if .True }}.True: TRUE{{ else }}.True: FALSE{{ end }}
|
|
|
|
{{ if .TimeZero }}.TimeZero1: TRUE{{ else }}.TimeZero1: FALSE{{ end }}
|
|
|
|
{{ if (.TimeZero) }}.TimeZero2: TRUE{{ else }}.TimeZero2: FALSE{{ end }}
|
|
|
|
{{ if not .TimeZero }}.TimeZero3: TRUE{{ else }}.TimeZero3: FALSE{{ end }}
|
|
|
|
{{ if .Now }}.Now: TRUE{{ else }}.Now: FALSE{{ end }}
|
|
|
|
{{ with .TimeZero }}.TimeZero1 with: {{ . }}{{ else }}.TimeZero1 with: FALSE{{ end }}
|
|
|
|
{{ template "mytemplate" . }}
|
|
|
|
{{ if .T.NonEmptyInterfaceTypedNil }}.NonEmptyInterfaceTypedNil: TRUE{{ else }}.NonEmptyInterfaceTypedNil: FALSE{{ end }}
|
|
|
|
|
2019-04-16 03:13:55 -04:00
|
|
|
{{ template "other-file-template" . }}
|
2019-03-06 03:07:49 -05:00
|
|
|
|
|
|
|
{{ define "mytemplate" }}
|
|
|
|
{{ if .TimeZero }}.TimeZero1: mytemplate: TRUE{{ else }}.TimeZero1: mytemplate: FALSE{{ end }}
|
|
|
|
{{ end }}
|
|
|
|
|
2019-04-16 03:13:55 -04:00
|
|
|
`
|
|
|
|
|
|
|
|
// 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 }}
|
2019-03-06 03:07:49 -05:00
|
|
|
`
|
|
|
|
)
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
d := newD(c)
|
2019-12-10 13:56:44 -05:00
|
|
|
h := d.Tmpl.(tpl.TemplateManager)
|
2019-04-16 03:13:55 -04:00
|
|
|
|
|
|
|
// HTML templates
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(h.AddTemplate("mytemplate.html", templ1), qt.IsNil)
|
|
|
|
c.Assert(h.AddTemplate("othertemplate.html", templ2), qt.IsNil)
|
2019-04-16 03:13:55 -04:00
|
|
|
|
|
|
|
// Text templates
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(h.AddTemplate("_text/mytexttemplate.txt", templ1), qt.IsNil)
|
|
|
|
c.Assert(h.AddTemplate("_text/myothertexttemplate.txt", templ2), qt.IsNil)
|
2019-04-16 03:13:55 -04:00
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(h.MarkReady(), qt.IsNil)
|
2019-04-16 03:13:55 -04:00
|
|
|
|
|
|
|
for _, name := range []string{"mytemplate.html", "mytexttemplate.txt"} {
|
|
|
|
tt, _ := d.Tmpl.Lookup(name)
|
2019-12-10 13:56:44 -05:00
|
|
|
sb := &strings.Builder{}
|
|
|
|
|
|
|
|
err := d.Tmpl.Execute(tt, sb, ctx)
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
result := sb.String()
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(result, qt.Contains, ".True: TRUE")
|
|
|
|
c.Assert(result, qt.Contains, ".TimeZero1: FALSE")
|
|
|
|
c.Assert(result, qt.Contains, ".TimeZero2: FALSE")
|
|
|
|
c.Assert(result, qt.Contains, ".TimeZero3: TRUE")
|
|
|
|
c.Assert(result, qt.Contains, ".Now: TRUE")
|
|
|
|
c.Assert(result, qt.Contains, "TimeZero1 with: FALSE")
|
|
|
|
c.Assert(result, qt.Contains, ".TimeZero1: mytemplate: FALSE")
|
|
|
|
c.Assert(result, qt.Contains, ".TimeZero1: other-file-template: FALSE")
|
|
|
|
c.Assert(result, qt.Contains, ".NonEmptyInterfaceTypedNil: FALSE")
|
2019-04-16 03:13:55 -04:00
|
|
|
}
|
2017-02-18 03:08:00 -05:00
|
|
|
|
|
|
|
}
|
2019-01-02 06:33:26 -05:00
|
|
|
|
|
|
|
func TestCollectInfo(t *testing.T) {
|
|
|
|
|
|
|
|
configStr := `{ "version": 42 }`
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
tplString string
|
|
|
|
expected tpl.Info
|
|
|
|
}{
|
|
|
|
{"Basic Inner", `{{ .Inner }}`, tpl.Info{IsInner: true, Config: tpl.DefaultConfig}},
|
|
|
|
{"Basic config map", "{{ $_hugo_config := `" + configStr + "` }}", tpl.Info{
|
|
|
|
Config: tpl.Config{
|
|
|
|
Version: 42,
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
echo := func(in interface{}) interface{} {
|
|
|
|
return in
|
|
|
|
}
|
|
|
|
|
|
|
|
funcs := template.FuncMap{
|
|
|
|
"highlight": echo,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2019-08-10 15:05:17 -04:00
|
|
|
c := qt.New(t)
|
2019-01-02 06:33:26 -05:00
|
|
|
|
|
|
|
templ, err := template.New("foo").Funcs(funcs).Parse(test.tplString)
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
2019-01-02 06:33:26 -05:00
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
ctx := newTemplateContext(createParseTreeLookup(templ))
|
|
|
|
ctx.typ = templateShortcode
|
|
|
|
ctx.applyTransformations(templ.Tree.Root)
|
2019-01-02 06:33:26 -05:00
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(ctx.Info, qt.Equals, test.expected)
|
2019-01-02 06:33:26 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-04-02 04:30:24 -04:00
|
|
|
|
|
|
|
func TestPartialReturn(t *testing.T) {
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
tplString string
|
|
|
|
expected bool
|
|
|
|
}{
|
|
|
|
{"Basic", `
|
|
|
|
{{ $a := "Hugo Rocks!" }}
|
|
|
|
{{ return $a }}
|
|
|
|
`, true},
|
|
|
|
{"Expression", `
|
|
|
|
{{ return add 32 }}
|
|
|
|
`, true},
|
|
|
|
}
|
|
|
|
|
|
|
|
echo := func(in interface{}) interface{} {
|
|
|
|
return in
|
|
|
|
}
|
|
|
|
|
|
|
|
funcs := template.FuncMap{
|
|
|
|
"return": echo,
|
|
|
|
"add": echo,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2019-08-10 15:05:17 -04:00
|
|
|
c := qt.New(t)
|
2019-04-02 04:30:24 -04:00
|
|
|
|
|
|
|
templ, err := template.New("foo").Funcs(funcs).Parse(test.tplString)
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
2019-04-02 04:30:24 -04:00
|
|
|
|
|
|
|
_, err = applyTemplateTransformers(templatePartial, templ.Tree, createParseTreeLookup(templ))
|
|
|
|
|
|
|
|
// Just check that it doesn't fail in this test. We have functional tests
|
|
|
|
// in hugoblib.
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
2019-04-02 04:30:24 -04:00
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|