2016-03-21 19:28:42 -04:00
|
|
|
// Copyright 2016 The Hugo Authors. All rights reserved.
|
2015-12-10 17:19:38 -05: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
|
2015-04-05 15:03:16 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2016-03-21 19:28:42 -04:00
|
|
|
"path/filepath"
|
2015-04-05 15:03:16 -04:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
2016-08-04 04:36:44 -04:00
|
|
|
|
2017-01-03 11:28:51 -05:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
|
2016-04-11 16:58:27 -04:00
|
|
|
"github.com/spf13/afero"
|
2017-02-04 22:20:06 -05:00
|
|
|
"github.com/spf13/hugo/config"
|
2017-03-13 18:55:02 -04:00
|
|
|
"github.com/spf13/hugo/deps"
|
|
|
|
"github.com/spf13/hugo/helpers"
|
2016-04-11 16:58:27 -04:00
|
|
|
"github.com/spf13/hugo/hugofs"
|
2017-02-04 22:20:06 -05:00
|
|
|
"github.com/spf13/hugo/i18n"
|
2017-03-13 18:55:02 -04:00
|
|
|
"github.com/spf13/hugo/tpl"
|
2017-04-30 11:45:56 -04:00
|
|
|
"github.com/spf13/hugo/tpl/internal"
|
2017-01-03 11:28:51 -05:00
|
|
|
jww "github.com/spf13/jwalterweatherman"
|
2016-04-11 16:58:27 -04:00
|
|
|
"github.com/spf13/viper"
|
2017-01-10 04:55:03 -05:00
|
|
|
"github.com/stretchr/testify/require"
|
2015-04-05 15:03:16 -04:00
|
|
|
)
|
|
|
|
|
2017-01-10 04:55:03 -05:00
|
|
|
var (
|
2017-02-15 04:00:34 -05:00
|
|
|
logger = jww.NewNotepad(jww.LevelFatal, jww.LevelFatal, os.Stdout, ioutil.Discard, "", log.Ldate|log.Ltime)
|
|
|
|
)
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
func newDepsConfig(cfg config.Provider) deps.DepsCfg {
|
|
|
|
l := helpers.NewLanguage("en", cfg)
|
|
|
|
l.Set("i18nDir", "i18n")
|
2017-02-15 04:00:34 -05:00
|
|
|
return deps.DepsCfg{
|
2017-02-04 22:20:06 -05:00
|
|
|
Language: l,
|
|
|
|
Cfg: cfg,
|
|
|
|
Fs: hugofs.NewMem(l),
|
|
|
|
Logger: logger,
|
|
|
|
TemplateProvider: DefaultTemplateProvider,
|
|
|
|
TranslationProvider: i18n.NewTranslationProvider(),
|
2017-01-10 04:55:03 -05:00
|
|
|
}
|
2017-02-15 04:00:34 -05:00
|
|
|
}
|
2017-01-03 11:28:51 -05:00
|
|
|
|
2017-04-30 11:45:56 -04:00
|
|
|
func TestTemplateFuncsExamples(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
workingDir := "/home/hugo"
|
|
|
|
|
|
|
|
v := viper.New()
|
|
|
|
|
|
|
|
v.Set("workingDir", workingDir)
|
|
|
|
v.Set("multilingual", true)
|
2017-04-30 17:33:14 -04:00
|
|
|
v.Set("baseURL", "http://mysite.com/hugo/")
|
|
|
|
v.Set("CurrentContentLanguage", helpers.NewLanguage("en", v))
|
2017-04-30 11:45:56 -04:00
|
|
|
|
|
|
|
fs := hugofs.NewMem(v)
|
|
|
|
|
|
|
|
afero.WriteFile(fs.Source, filepath.Join(workingDir, "README.txt"), []byte("Hugo Rocks!"), 0755)
|
|
|
|
|
2017-04-30 16:52:47 -04:00
|
|
|
depsCfg := newDepsConfig(v)
|
|
|
|
depsCfg.Fs = fs
|
|
|
|
d, err := deps.New(depsCfg)
|
2017-04-30 11:45:56 -04:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var data struct {
|
|
|
|
Title string
|
|
|
|
Section string
|
|
|
|
Params map[string]interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
data.Title = "**BatMan**"
|
|
|
|
data.Section = "blog"
|
|
|
|
data.Params = map[string]interface{}{"langCode": "en"}
|
|
|
|
|
|
|
|
for _, nsf := range internal.TemplateFuncsNamespaceRegistry {
|
|
|
|
ns := nsf(d)
|
2017-05-01 12:40:34 -04:00
|
|
|
for _, mm := range ns.MethodMappings {
|
|
|
|
for i, example := range mm.Examples {
|
|
|
|
in, expected := example[0], example[1]
|
|
|
|
d.WithTemplate = func(templ tpl.TemplateHandler) error {
|
|
|
|
require.NoError(t, templ.AddTemplate("test", in))
|
|
|
|
require.NoError(t, templ.AddTemplate("partials/header.html", "<title>Hugo Rocks!</title>"))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
require.NoError(t, d.LoadResources())
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
require.NoError(t, d.Tmpl.Lookup("test").Execute(&b, &data))
|
|
|
|
if b.String() != expected {
|
|
|
|
t.Fatalf("%s[%d]: got %q expected %q", ns.Name, i, b.String(), expected)
|
|
|
|
}
|
2017-04-30 11:45:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-05-01 03:06:42 -04:00
|
|
|
// TODO(bep) it would be dandy to put this one into the partials package, but
|
|
|
|
// we have some package cycle issues to solve first.
|
2016-10-10 18:03:30 -04:00
|
|
|
func TestPartialCached(t *testing.T) {
|
2017-02-04 22:20:06 -05:00
|
|
|
t.Parallel()
|
2016-10-10 18:03:30 -04:00
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
partial string
|
|
|
|
tmpl string
|
|
|
|
variant string
|
|
|
|
}{
|
|
|
|
// name and partial should match between test cases.
|
|
|
|
{"test1", "{{ .Title }} seq: {{ shuffle (seq 1 20) }}", `{{ partialCached "test1" . }}`, ""},
|
|
|
|
{"test1", "{{ .Title }} seq: {{ shuffle (seq 1 20) }}", `{{ partialCached "test1" . "%s" }}`, "header"},
|
|
|
|
{"test1", "{{ .Title }} seq: {{ shuffle (seq 1 20) }}", `{{ partialCached "test1" . "%s" }}`, "footer"},
|
|
|
|
{"test1", "{{ .Title }} seq: {{ shuffle (seq 1 20) }}", `{{ partialCached "test1" . "%s" }}`, "header"},
|
|
|
|
}
|
|
|
|
|
|
|
|
var data struct {
|
|
|
|
Title string
|
|
|
|
Section string
|
|
|
|
Params map[string]interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
data.Title = "**BatMan**"
|
|
|
|
data.Section = "blog"
|
|
|
|
data.Params = map[string]interface{}{"langCode": "en"}
|
|
|
|
|
|
|
|
for i, tc := range testCases {
|
|
|
|
var tmp string
|
|
|
|
if tc.variant != "" {
|
|
|
|
tmp = fmt.Sprintf(tc.tmpl, tc.variant)
|
|
|
|
} else {
|
|
|
|
tmp = tc.tmpl
|
|
|
|
}
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
config := newDepsConfig(viper.New())
|
2017-02-15 04:00:34 -05:00
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
config.WithTemplate = func(templ tpl.TemplateHandler) error {
|
2017-01-10 04:55:03 -05:00
|
|
|
err := templ.AddTemplate("testroot", tmp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = templ.AddTemplate("partials/"+tc.name, tc.partial)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-10-10 18:03:30 -04:00
|
|
|
|
2017-01-10 04:55:03 -05:00
|
|
|
return nil
|
2016-10-10 18:03:30 -04:00
|
|
|
}
|
|
|
|
|
2017-03-25 09:37:04 -04:00
|
|
|
de, err := deps.New(config)
|
|
|
|
require.NoError(t, err)
|
2017-02-04 22:20:06 -05:00
|
|
|
require.NoError(t, de.LoadResources())
|
2016-10-10 18:03:30 -04:00
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
2017-01-10 04:55:03 -05:00
|
|
|
templ := de.Tmpl.Lookup("testroot")
|
2017-03-25 09:37:04 -04:00
|
|
|
err = templ.Execute(buf, &data)
|
2016-10-10 18:03:30 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("[%d] error executing template: %s", i, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for j := 0; j < 10; j++ {
|
|
|
|
buf2 := new(bytes.Buffer)
|
2017-01-10 04:55:03 -05:00
|
|
|
err := templ.Execute(buf2, nil)
|
2016-10-10 18:03:30 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("[%d] error executing template 2nd time: %s", i, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(buf, buf2) {
|
|
|
|
t.Fatalf("[%d] cached results do not match:\nResult 1:\n%q\nResult 2:\n%q", i, buf, buf2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkPartial(b *testing.B) {
|
2017-02-04 22:20:06 -05:00
|
|
|
config := newDepsConfig(viper.New())
|
2017-03-27 14:43:49 -04:00
|
|
|
config.WithTemplate = func(templ tpl.TemplateHandler) error {
|
2017-01-10 04:55:03 -05:00
|
|
|
err := templ.AddTemplate("testroot", `{{ partial "bench1" . }}`)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = templ.AddTemplate("partials/bench1", `{{ shuffle (seq 1 10) }}`)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2016-10-10 18:03:30 -04:00
|
|
|
}
|
|
|
|
|
2017-03-25 09:37:04 -04:00
|
|
|
de, err := deps.New(config)
|
|
|
|
require.NoError(b, err)
|
2017-02-04 22:20:06 -05:00
|
|
|
require.NoError(b, de.LoadResources())
|
2017-01-10 04:55:03 -05:00
|
|
|
|
2016-10-10 18:03:30 -04:00
|
|
|
buf := new(bytes.Buffer)
|
2017-01-10 04:55:03 -05:00
|
|
|
tmpl := de.Tmpl.Lookup("testroot")
|
2016-10-10 18:03:30 -04:00
|
|
|
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2017-01-10 04:55:03 -05:00
|
|
|
if err := tmpl.Execute(buf, nil); err != nil {
|
2016-10-10 18:03:30 -04:00
|
|
|
b.Fatalf("error executing template: %s", err)
|
|
|
|
}
|
|
|
|
buf.Reset()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkPartialCached(b *testing.B) {
|
2017-02-04 22:20:06 -05:00
|
|
|
config := newDepsConfig(viper.New())
|
2017-03-27 14:43:49 -04:00
|
|
|
config.WithTemplate = func(templ tpl.TemplateHandler) error {
|
2017-01-10 04:55:03 -05:00
|
|
|
err := templ.AddTemplate("testroot", `{{ partialCached "bench1" . }}`)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = templ.AddTemplate("partials/bench1", `{{ shuffle (seq 1 10) }}`)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2016-10-10 18:03:30 -04:00
|
|
|
}
|
|
|
|
|
2017-03-25 09:37:04 -04:00
|
|
|
de, err := deps.New(config)
|
|
|
|
require.NoError(b, err)
|
2017-02-04 22:20:06 -05:00
|
|
|
require.NoError(b, de.LoadResources())
|
2017-01-10 04:55:03 -05:00
|
|
|
|
2016-10-10 18:03:30 -04:00
|
|
|
buf := new(bytes.Buffer)
|
2017-01-10 04:55:03 -05:00
|
|
|
tmpl := de.Tmpl.Lookup("testroot")
|
2016-10-10 18:03:30 -04:00
|
|
|
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2017-01-10 04:55:03 -05:00
|
|
|
if err := tmpl.Execute(buf, nil); err != nil {
|
2016-10-10 18:03:30 -04:00
|
|
|
b.Fatalf("error executing template: %s", err)
|
|
|
|
}
|
|
|
|
buf.Reset()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-10 04:55:03 -05:00
|
|
|
func newTestFuncster() *templateFuncster {
|
2017-02-04 22:20:06 -05:00
|
|
|
return newTestFuncsterWithViper(viper.New())
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTestFuncsterWithViper(v *viper.Viper) *templateFuncster {
|
|
|
|
config := newDepsConfig(v)
|
2017-03-25 09:37:04 -04:00
|
|
|
d, err := deps.New(config)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2017-02-04 22:20:06 -05:00
|
|
|
|
|
|
|
if err := d.LoadResources(); err != nil {
|
2017-01-10 04:55:03 -05:00
|
|
|
panic(err)
|
2016-10-10 18:03:30 -04:00
|
|
|
}
|
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
return d.Tmpl.(*templateHandler).html.funcster
|
2017-01-10 04:55:03 -05:00
|
|
|
}
|