2019-01-02 11:33:26 +00:00
|
|
|
// Copyright 2019 The Hugo Authors. All rights reserved.
|
2016-10-24 20:29:48 +00: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 12:30:50 +00:00
|
|
|
package tplimpl
|
2016-10-24 20:29:48 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2019-11-27 12:42:36 +00:00
|
|
|
template "github.com/gohugoio/hugo/tpl/internal/go_templates/htmltemplate"
|
2019-03-06 08:07:49 +00:00
|
|
|
|
2019-08-10 19:05:17 +00:00
|
|
|
qt "github.com/frankban/quicktest"
|
2019-11-27 12:42:36 +00:00
|
|
|
"github.com/gohugoio/hugo/tpl"
|
2016-10-24 20:29:48 +00:00
|
|
|
)
|
|
|
|
|
2017-02-18 08:08:00 +00:00
|
|
|
// Issue #2927
|
|
|
|
func TestTransformRecursiveTemplate(t *testing.T) {
|
2019-08-10 19:05:17 +00:00
|
|
|
c := qt.New(t)
|
2017-02-18 08:08:00 +00:00
|
|
|
|
|
|
|
recursive := `
|
|
|
|
{{ define "menu-nodes" }}
|
|
|
|
{{ template "menu-node" }}
|
|
|
|
{{ end }}
|
2020-01-15 14:59:56 +00:00
|
|
|
{{ define "menu-node" }}
|
2017-02-18 08:08:00 +00:00
|
|
|
{{ template "menu-node" }}
|
|
|
|
{{ end }}
|
|
|
|
{{ template "menu-nodes" }}
|
|
|
|
`
|
|
|
|
|
|
|
|
templ, err := template.New("foo").Parse(recursive)
|
2019-08-10 19:05:17 +00:00
|
|
|
c.Assert(err, qt.IsNil)
|
2020-01-15 14:59:56 +00:00
|
|
|
ts := newTestTemplate(templ)
|
2017-02-18 08:08:00 +00:00
|
|
|
|
2019-11-27 12:42:36 +00:00
|
|
|
ctx := newTemplateContext(
|
2020-01-15 14:59:56 +00:00
|
|
|
ts,
|
|
|
|
newTestTemplateLookup(ts),
|
2019-11-27 12:42:36 +00:00
|
|
|
)
|
2019-08-10 19:05:17 +00:00
|
|
|
ctx.applyTransformations(templ.Tree.Root)
|
2019-03-06 08:07:49 +00:00
|
|
|
}
|
|
|
|
|
2020-01-15 14:59:56 +00:00
|
|
|
func newTestTemplate(templ tpl.Template) *templateState {
|
|
|
|
return newTemplateState(
|
|
|
|
templ,
|
|
|
|
templateInfo{
|
|
|
|
name: templ.Name(),
|
|
|
|
},
|
|
|
|
)
|
2019-11-27 12:42:36 +00:00
|
|
|
}
|
|
|
|
|
2020-01-15 14:59:56 +00:00
|
|
|
func newTestTemplateLookup(in *templateState) func(name string) *templateState {
|
|
|
|
m := make(map[string]*templateState)
|
|
|
|
return func(name string) *templateState {
|
|
|
|
if in.Name() == name {
|
|
|
|
return in
|
|
|
|
}
|
2019-03-06 08:07:49 +00:00
|
|
|
|
2020-01-15 14:59:56 +00:00
|
|
|
if ts, found := m[name]; found {
|
|
|
|
return ts
|
|
|
|
}
|
2019-03-06 08:07:49 +00:00
|
|
|
|
2020-01-15 14:59:56 +00:00
|
|
|
if templ, found := findTemplateIn(name, in); found {
|
|
|
|
ts := newTestTemplate(templ)
|
|
|
|
m[name] = ts
|
|
|
|
return ts
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-03-06 08:07:49 +00:00
|
|
|
}
|
2019-01-02 11:33:26 +00:00
|
|
|
|
|
|
|
func TestCollectInfo(t *testing.T) {
|
|
|
|
configStr := `{ "version": 42 }`
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
tplString string
|
2019-11-27 12:42:36 +00:00
|
|
|
expected tpl.ParseInfo
|
2019-01-02 11:33:26 +00:00
|
|
|
}{
|
2019-11-27 12:42:36 +00:00
|
|
|
{"Basic Inner", `{{ .Inner }}`, tpl.ParseInfo{IsInner: true, Config: tpl.DefaultParseConfig}},
|
|
|
|
{"Basic config map", "{{ $_hugo_config := `" + configStr + "` }}", tpl.ParseInfo{Config: tpl.ParseConfig{Version: 42}}},
|
2019-01-02 11:33:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 19:05:17 +00:00
|
|
|
c := qt.New(t)
|
2019-01-02 11:33:26 +00:00
|
|
|
|
|
|
|
templ, err := template.New("foo").Funcs(funcs).Parse(test.tplString)
|
2019-08-10 19:05:17 +00:00
|
|
|
c.Assert(err, qt.IsNil)
|
2020-01-15 14:59:56 +00:00
|
|
|
ts := newTestTemplate(templ)
|
|
|
|
ts.typ = templateShortcode
|
2019-11-27 12:42:36 +00:00
|
|
|
ctx := newTemplateContext(
|
2020-01-15 14:59:56 +00:00
|
|
|
ts,
|
|
|
|
newTestTemplateLookup(ts),
|
|
|
|
)
|
2019-08-10 19:05:17 +00:00
|
|
|
ctx.applyTransformations(templ.Tree.Root)
|
2020-01-15 14:59:56 +00:00
|
|
|
c.Assert(ctx.t.parseInfo, qt.DeepEquals, test.expected)
|
2019-01-02 11:33:26 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-04-02 08:30:24 +00: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 19:05:17 +00:00
|
|
|
c := qt.New(t)
|
2019-04-02 08:30:24 +00:00
|
|
|
|
|
|
|
templ, err := template.New("foo").Funcs(funcs).Parse(test.tplString)
|
2019-08-10 19:05:17 +00:00
|
|
|
c.Assert(err, qt.IsNil)
|
2020-01-15 14:59:56 +00:00
|
|
|
ts := newTestTemplate(templ)
|
|
|
|
ctx := newTemplateContext(
|
|
|
|
ts,
|
|
|
|
newTestTemplateLookup(ts),
|
|
|
|
)
|
2019-04-02 08:30:24 +00:00
|
|
|
|
2020-01-15 14:59:56 +00:00
|
|
|
_, err = ctx.applyTransformations(templ.Tree.Root)
|
2019-04-02 08:30:24 +00:00
|
|
|
|
|
|
|
// Just check that it doesn't fail in this test. We have functional tests
|
|
|
|
// in hugoblib.
|
2019-08-10 19:05:17 +00:00
|
|
|
c.Assert(err, qt.IsNil)
|
2019-04-02 08:30:24 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|