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 (
|
2019-11-27 12:42:36 +00:00
|
|
|
"github.com/gohugoio/hugo/hugofs/files"
|
2019-12-10 07:02:15 +00:00
|
|
|
|
2016-10-24 20:29:48 +00:00
|
|
|
"testing"
|
|
|
|
|
2019-11-27 12:42:36 +00:00
|
|
|
template "github.com/gohugoio/hugo/tpl/internal/go_templates/htmltemplate"
|
|
|
|
"github.com/gohugoio/hugo/tpl/internal/go_templates/texttemplate/parse"
|
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/identity"
|
|
|
|
"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 }}
|
2019-11-27 12:42:36 +00:00
|
|
|
{{ define "menu-nßode" }}
|
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)
|
2019-11-27 12:42:36 +00:00
|
|
|
parseInfo := tpl.DefaultParseInfo
|
2017-02-18 08:08:00 +00:00
|
|
|
|
2019-11-27 12:42:36 +00:00
|
|
|
ctx := newTemplateContext(
|
|
|
|
newTemplateInfo("test").(identity.Manager),
|
|
|
|
&parseInfo,
|
|
|
|
createGetTemplateInfoTree(templ.Tree),
|
|
|
|
)
|
2019-08-10 19:05:17 +00:00
|
|
|
ctx.applyTransformations(templ.Tree.Root)
|
2019-03-06 08:07:49 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-11-27 12:42:36 +00:00
|
|
|
func createGetTemplateInfoTree(tree *parse.Tree) func(name string) *templateInfoTree {
|
|
|
|
return func(name string) *templateInfoTree {
|
|
|
|
return &templateInfoTree{
|
|
|
|
tree: tree,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-06 08:07:49 +00:00
|
|
|
type I interface {
|
|
|
|
Method0()
|
|
|
|
}
|
|
|
|
|
|
|
|
type T struct {
|
|
|
|
NonEmptyInterfaceTypedNil I
|
|
|
|
}
|
|
|
|
|
|
|
|
func (T) Method0() {
|
|
|
|
}
|
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)
|
2019-11-27 12:42:36 +00:00
|
|
|
parseInfo := tpl.DefaultParseInfo
|
2019-01-02 11:33:26 +00:00
|
|
|
|
2019-11-27 12:42:36 +00:00
|
|
|
ctx := newTemplateContext(
|
|
|
|
newTemplateInfo("test").(identity.Manager), &parseInfo, createGetTemplateInfoTree(templ.Tree))
|
2019-08-10 19:05:17 +00:00
|
|
|
ctx.typ = templateShortcode
|
|
|
|
ctx.applyTransformations(templ.Tree.Root)
|
2019-11-27 12:42:36 +00:00
|
|
|
c.Assert(ctx.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)
|
2019-04-02 08:30:24 +00:00
|
|
|
|
2019-11-27 12:42:36 +00:00
|
|
|
_, err = applyTemplateTransformers(
|
|
|
|
templatePartial,
|
|
|
|
&templateInfoTree{tree: templ.Tree, info: tpl.DefaultParseInfo},
|
|
|
|
createGetTemplateInfoTree(templ.Tree))
|
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
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-11-27 12:42:36 +00:00
|
|
|
|
|
|
|
func newTemplateInfo(name string) tpl.Info {
|
|
|
|
return tpl.NewInfo(
|
|
|
|
identity.NewManager(identity.NewPathIdentity(files.ComponentFolderLayouts, name)),
|
|
|
|
tpl.DefaultParseInfo,
|
|
|
|
)
|
|
|
|
}
|