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 (
|
|
|
|
"testing"
|
|
|
|
|
2019-11-27 07:42:36 -05:00
|
|
|
template "github.com/gohugoio/hugo/tpl/internal/go_templates/htmltemplate"
|
2019-03-06 03:07:49 -05:00
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
qt "github.com/frankban/quicktest"
|
2019-11-27 07:42:36 -05:00
|
|
|
"github.com/gohugoio/hugo/tpl"
|
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 }}
|
2020-01-15 09:59:56 -05:00
|
|
|
{{ define "menu-node" }}
|
2017-02-18 03:08:00 -05:00
|
|
|
{{ 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)
|
2020-01-15 09:59:56 -05:00
|
|
|
ts := newTestTemplate(templ)
|
2017-02-18 03:08:00 -05:00
|
|
|
|
2019-11-27 07:42:36 -05:00
|
|
|
ctx := newTemplateContext(
|
2020-01-15 09:59:56 -05:00
|
|
|
ts,
|
|
|
|
newTestTemplateLookup(ts),
|
2019-11-27 07:42:36 -05:00
|
|
|
)
|
2019-08-10 15:05:17 -04:00
|
|
|
ctx.applyTransformations(templ.Tree.Root)
|
2019-03-06 03:07:49 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
func newTestTemplate(templ tpl.Template) *templateState {
|
|
|
|
return newTemplateState(
|
|
|
|
templ,
|
|
|
|
templateInfo{
|
|
|
|
name: templ.Name(),
|
|
|
|
},
|
|
|
|
)
|
2019-11-27 07:42:36 -05:00
|
|
|
}
|
|
|
|
|
2020-01-15 09:59:56 -05: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 03:07:49 -05:00
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
if ts, found := m[name]; found {
|
|
|
|
return ts
|
|
|
|
}
|
2019-03-06 03:07:49 -05:00
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
if templ, found := findTemplateIn(name, in); found {
|
|
|
|
ts := newTestTemplate(templ)
|
|
|
|
m[name] = ts
|
|
|
|
return ts
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-03-06 03:07:49 -05:00
|
|
|
}
|
2019-01-02 06:33:26 -05:00
|
|
|
|
|
|
|
func TestCollectInfo(t *testing.T) {
|
|
|
|
|
|
|
|
configStr := `{ "version": 42 }`
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
tplString string
|
2019-11-27 07:42:36 -05:00
|
|
|
expected tpl.ParseInfo
|
2019-01-02 06:33:26 -05:00
|
|
|
}{
|
2019-11-27 07:42:36 -05: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 06:33:26 -05: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 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)
|
2020-01-15 09:59:56 -05:00
|
|
|
ts := newTestTemplate(templ)
|
|
|
|
ts.typ = templateShortcode
|
2019-11-27 07:42:36 -05:00
|
|
|
ctx := newTemplateContext(
|
2020-01-15 09:59:56 -05:00
|
|
|
ts,
|
|
|
|
newTestTemplateLookup(ts),
|
|
|
|
)
|
2019-08-10 15:05:17 -04:00
|
|
|
ctx.applyTransformations(templ.Tree.Root)
|
2020-01-15 09:59:56 -05:00
|
|
|
c.Assert(ctx.t.parseInfo, qt.DeepEquals, 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)
|
2020-01-15 09:59:56 -05:00
|
|
|
ts := newTestTemplate(templ)
|
|
|
|
ctx := newTemplateContext(
|
|
|
|
ts,
|
|
|
|
newTestTemplateLookup(ts),
|
|
|
|
)
|
2019-04-02 04:30:24 -04:00
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
_, err = ctx.applyTransformations(templ.Tree.Root)
|
2019-04-02 04:30:24 -04:00
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|