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"
|
2016-02-07 09:15:27 -05:00
|
|
|
|
"strings"
|
2015-04-05 15:03:16 -04:00
|
|
|
|
"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)
|
|
|
|
|
|
|
|
|
|
fs := hugofs.NewMem(v)
|
|
|
|
|
|
|
|
|
|
afero.WriteFile(fs.Source, filepath.Join(workingDir, "README.txt"), []byte("Hugo Rocks!"), 0755)
|
|
|
|
|
|
|
|
|
|
d, err := deps.New(newDepsConfig(v))
|
|
|
|
|
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)
|
|
|
|
|
for i, example := range ns.Examples {
|
|
|
|
|
in, expected := example[0], example[1]
|
|
|
|
|
d.WithTemplate = func(templ tpl.TemplateHandler) error {
|
|
|
|
|
require.NoError(t, templ.AddTemplate("test", in))
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-03 13:35:04 -05:00
|
|
|
|
func TestFuncsInTemplate(t *testing.T) {
|
2017-02-04 22:20:06 -05:00
|
|
|
|
t.Parallel()
|
2016-03-03 13:35:04 -05:00
|
|
|
|
|
2016-03-21 19:28:42 -04:00
|
|
|
|
workingDir := "/home/hugo"
|
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
|
v := viper.New()
|
2016-03-21 19:28:42 -04:00
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
|
v.Set("workingDir", workingDir)
|
|
|
|
|
v.Set("multilingual", true)
|
|
|
|
|
|
|
|
|
|
fs := hugofs.NewMem(v)
|
2016-03-21 19:28:42 -04:00
|
|
|
|
|
2017-01-10 04:55:03 -05:00
|
|
|
|
afero.WriteFile(fs.Source, filepath.Join(workingDir, "README.txt"), []byte("Hugo Rocks!"), 0755)
|
2016-03-21 19:28:42 -04:00
|
|
|
|
|
2016-03-03 13:35:04 -05:00
|
|
|
|
// Add the examples from the docs: As a smoke test and to make sure the examples work.
|
|
|
|
|
// TODO(bep): docs: fix title example
|
2017-04-30 05:34:45 -04:00
|
|
|
|
// TODO(bep) namespace remove when done
|
2016-03-03 13:35:04 -05:00
|
|
|
|
in :=
|
2016-08-07 16:01:55 -04:00
|
|
|
|
`absLangURL: {{ "index.html" | absLangURL }}
|
|
|
|
|
absURL: {{ "http://gohugo.io/" | absURL }}
|
2016-04-02 19:57:22 -04:00
|
|
|
|
absURL: {{ "mystyle.css" | absURL }}
|
2016-08-12 18:31:50 -04:00
|
|
|
|
absURL: {{ 42 | absURL }}
|
2017-03-13 18:55:02 -04:00
|
|
|
|
crypto.MD5: {{ crypto.MD5 "Hello world, gophers!" }}
|
2016-03-03 13:35:04 -05:00
|
|
|
|
dateFormat: {{ dateFormat "Monday, Jan 2, 2006" "2015-01-21" }}
|
2016-04-02 19:57:22 -04:00
|
|
|
|
emojify: {{ "I :heart: Hugo" | emojify }}
|
2016-07-18 18:14:05 -04:00
|
|
|
|
htmlEscape 1: {{ htmlEscape "Cathal Garvey & The Sunshine Band <cathal@foo.bar>" | safeHTML}}
|
|
|
|
|
htmlEscape 2: {{ htmlEscape "Cathal Garvey & The Sunshine Band <cathal@foo.bar>"}}
|
|
|
|
|
htmlUnescape 1: {{htmlUnescape "Cathal Garvey & The Sunshine Band <cathal@foo.bar>" | safeHTML}}
|
|
|
|
|
htmlUnescape 2: {{"Cathal Garvey &amp; The Sunshine Band &lt;cathal@foo.bar&gt;" | htmlUnescape | htmlUnescape | safeHTML}}
|
|
|
|
|
htmlUnescape 3: {{"Cathal Garvey &amp; The Sunshine Band &lt;cathal@foo.bar&gt;" | htmlUnescape | htmlUnescape }}
|
|
|
|
|
htmlUnescape 4: {{ htmlEscape "Cathal Garvey & The Sunshine Band <cathal@foo.bar>" | htmlUnescape | safeHTML }}
|
|
|
|
|
htmlUnescape 5: {{ htmlUnescape "Cathal Garvey & The Sunshine Band <cathal@foo.bar>" | htmlEscape | safeHTML }}
|
2016-07-14 14:32:32 -04:00
|
|
|
|
humanize 1: {{ humanize "my-first-post" }}
|
|
|
|
|
humanize 2: {{ humanize "myCamelPost" }}
|
|
|
|
|
humanize 3: {{ humanize "52" }}
|
|
|
|
|
humanize 4: {{ humanize 103 }}
|
2016-03-03 13:35:04 -05:00
|
|
|
|
markdownify: {{ .Title | markdownify}}
|
2017-03-07 15:11:03 -05:00
|
|
|
|
print: {{ print "works!" }}
|
|
|
|
|
printf: {{ printf "%s!" "works" }}
|
|
|
|
|
println: {{ println "works!" -}}
|
2016-04-02 19:57:22 -04:00
|
|
|
|
plainify: {{ plainify "Hello <strong>world</strong>, gophers!" }}
|
2016-03-03 13:35:04 -05:00
|
|
|
|
pluralize: {{ "cat" | pluralize }}
|
2016-04-02 19:57:22 -04:00
|
|
|
|
readDir: {{ range (readDir ".") }}{{ .Name }}{{ end }}
|
|
|
|
|
readFile: {{ readFile "README.txt" }}
|
2016-08-07 16:01:55 -04:00
|
|
|
|
relLangURL: {{ "index.html" | relLangURL }}
|
2016-04-02 19:57:22 -04:00
|
|
|
|
relURL 1: {{ "http://gohugo.io/" | relURL }}
|
|
|
|
|
relURL 2: {{ "mystyle.css" | relURL }}
|
2016-08-12 18:36:15 -04:00
|
|
|
|
relURL 3: {{ mul 2 21 | relURL }}
|
2016-04-02 19:57:22 -04:00
|
|
|
|
safeCSS: {{ "Bat&Man" | safeCSS | safeCSS }}
|
|
|
|
|
safeHTML: {{ "Bat&Man" | safeHTML | safeHTML }}
|
|
|
|
|
safeHTML: {{ "Bat&Man" | safeHTML }}
|
|
|
|
|
safeJS: {{ "(1*2)" | safeJS | safeJS }}
|
|
|
|
|
safeURL: {{ "http://gohugo.io" | safeURL | safeURL }}
|
2016-03-03 13:35:04 -05:00
|
|
|
|
singularize: {{ "cats" | singularize }}
|
2017-03-13 18:55:02 -04:00
|
|
|
|
strings.TrimPrefix: {{ strings.TrimPrefix "Goodbye,, world!" "Goodbye," }}
|
2016-08-04 04:36:44 -04:00
|
|
|
|
time: {{ (time "2015-01-21").Year }}
|
2016-03-03 13:35:04 -05:00
|
|
|
|
urlize: {{ "Bat Man" | urlize }}
|
|
|
|
|
`
|
2016-04-02 19:57:22 -04:00
|
|
|
|
|
2016-08-07 16:01:55 -04:00
|
|
|
|
expected := `absLangURL: http://mysite.com/hugo/en/index.html
|
|
|
|
|
absURL: http://gohugo.io/
|
2016-04-02 19:57:22 -04:00
|
|
|
|
absURL: http://mysite.com/hugo/mystyle.css
|
2016-08-12 18:31:50 -04:00
|
|
|
|
absURL: http://mysite.com/hugo/42
|
2017-03-13 18:55:02 -04:00
|
|
|
|
crypto.MD5: b3029f756f98f79e7f1b7f1d1f0dd53b
|
2016-03-03 13:35:04 -05:00
|
|
|
|
dateFormat: Wednesday, Jan 21, 2015
|
2016-05-09 17:18:18 -04:00
|
|
|
|
emojify: I ❤️ Hugo
|
2016-07-18 18:14:05 -04:00
|
|
|
|
htmlEscape 1: Cathal Garvey & The Sunshine Band <cathal@foo.bar>
|
|
|
|
|
htmlEscape 2: Cathal Garvey &amp; The Sunshine Band &lt;cathal@foo.bar&gt;
|
|
|
|
|
htmlUnescape 1: Cathal Garvey & The Sunshine Band <cathal@foo.bar>
|
|
|
|
|
htmlUnescape 2: Cathal Garvey & The Sunshine Band <cathal@foo.bar>
|
|
|
|
|
htmlUnescape 3: Cathal Garvey & The Sunshine Band <cathal@foo.bar>
|
|
|
|
|
htmlUnescape 4: Cathal Garvey & The Sunshine Band <cathal@foo.bar>
|
|
|
|
|
htmlUnescape 5: Cathal Garvey & The Sunshine Band <cathal@foo.bar>
|
2016-07-14 14:32:32 -04:00
|
|
|
|
humanize 1: My first post
|
|
|
|
|
humanize 2: My camel post
|
|
|
|
|
humanize 3: 52nd
|
|
|
|
|
humanize 4: 103rd
|
2016-03-03 13:35:04 -05:00
|
|
|
|
markdownify: <strong>BatMan</strong>
|
2017-03-07 15:11:03 -05:00
|
|
|
|
print: works!
|
|
|
|
|
printf: works!
|
|
|
|
|
println: works!
|
2016-04-02 19:57:22 -04:00
|
|
|
|
plainify: Hello world, gophers!
|
2016-03-03 13:35:04 -05:00
|
|
|
|
pluralize: cats
|
2016-04-02 19:57:22 -04:00
|
|
|
|
readDir: README.txt
|
|
|
|
|
readFile: Hugo Rocks!
|
2016-08-07 16:01:55 -04:00
|
|
|
|
relLangURL: /hugo/en/index.html
|
2016-04-02 19:57:22 -04:00
|
|
|
|
relURL 1: http://gohugo.io/
|
|
|
|
|
relURL 2: /hugo/mystyle.css
|
2016-08-12 18:36:15 -04:00
|
|
|
|
relURL 3: /hugo/42
|
2016-04-02 19:57:22 -04:00
|
|
|
|
safeCSS: Bat&Man
|
|
|
|
|
safeHTML: Bat&Man
|
|
|
|
|
safeHTML: Bat&Man
|
|
|
|
|
safeJS: (1*2)
|
|
|
|
|
safeURL: http://gohugo.io
|
2016-03-03 13:35:04 -05:00
|
|
|
|
singularize: cat
|
2017-03-13 18:55:02 -04:00
|
|
|
|
strings.TrimPrefix: , world!
|
2016-08-04 04:36:44 -04:00
|
|
|
|
time: 2015
|
2016-03-03 13:35:04 -05:00
|
|
|
|
urlize: bat-man
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
2017-01-10 04:55:03 -05:00
|
|
|
|
|
2016-03-03 13:35:04 -05:00
|
|
|
|
var data struct {
|
|
|
|
|
Title string
|
|
|
|
|
Section string
|
2016-09-07 15:03:09 -04:00
|
|
|
|
Params map[string]interface{}
|
2016-03-03 13:35:04 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data.Title = "**BatMan**"
|
|
|
|
|
data.Section = "blog"
|
2016-09-07 15:03:09 -04:00
|
|
|
|
data.Params = map[string]interface{}{"langCode": "en"}
|
2016-03-03 13:35:04 -05:00
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
|
v.Set("baseURL", "http://mysite.com/hugo/")
|
|
|
|
|
v.Set("CurrentContentLanguage", helpers.NewLanguage("en", v))
|
2016-10-24 07:45:30 -04:00
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
|
config := newDepsConfig(v)
|
2017-03-27 14:43:49 -04:00
|
|
|
|
config.WithTemplate = func(templ tpl.TemplateHandler) error {
|
|
|
|
|
if err := templ.AddTemplate("test", in); err != nil {
|
2017-01-10 04:55:03 -05:00
|
|
|
|
t.Fatal("Got error on parse", err)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
config.Fs = fs
|
|
|
|
|
|
2017-03-25 09:37:04 -04:00
|
|
|
|
d, err := deps.New(config)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
|
if err := d.LoadResources(); err != nil {
|
2017-01-10 04:55:03 -05:00
|
|
|
|
t.Fatal(err)
|
2016-03-03 13:35:04 -05:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-25 09:37:04 -04:00
|
|
|
|
err = d.Tmpl.Lookup("test").Execute(&b, &data)
|
2016-03-03 13:35:04 -05:00
|
|
|
|
|
|
|
|
|
if err != nil {
|
2016-03-07 15:40:47 -05:00
|
|
|
|
t.Fatal("Got error on execute", err)
|
2016-03-03 13:35:04 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if b.String() != expected {
|
2016-08-04 04:36:44 -04:00
|
|
|
|
sl1 := strings.Split(b.String(), "\n")
|
|
|
|
|
sl2 := strings.Split(expected, "\n")
|
|
|
|
|
t.Errorf("Diff:\n%q", helpers.DiffStringSlices(sl1, sl2))
|
2016-03-03 13:35:04 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-13 18:55:02 -04:00
|
|
|
|
func TestDefault(t *testing.T) {
|
2017-02-04 22:20:06 -05:00
|
|
|
|
t.Parallel()
|
2015-04-05 15:03:16 -04:00
|
|
|
|
for i, this := range []struct {
|
2017-03-13 18:55:02 -04:00
|
|
|
|
input interface{}
|
|
|
|
|
tpl string
|
|
|
|
|
expected string
|
|
|
|
|
ok bool
|
2015-04-05 15:03:16 -04:00
|
|
|
|
}{
|
2017-03-13 18:55:02 -04:00
|
|
|
|
{map[string]string{"foo": "bar"}, `{{ index . "foo" | default "nope" }}`, `bar`, true},
|
|
|
|
|
{map[string]string{"foo": "pop"}, `{{ index . "bar" | default "nada" }}`, `nada`, true},
|
|
|
|
|
{map[string]string{"foo": "cat"}, `{{ default "nope" .foo }}`, `cat`, true},
|
|
|
|
|
{map[string]string{"foo": "dog"}, `{{ default "nope" .foo "extra" }}`, ``, false},
|
|
|
|
|
{map[string]interface{}{"images": []string{}}, `{{ default "default.jpg" (index .images 0) }}`, `default.jpg`, true},
|
2015-04-05 15:03:16 -04:00
|
|
|
|
} {
|
|
|
|
|
|
2017-03-13 18:55:02 -04:00
|
|
|
|
tmpl := newTestTemplate(t, "test", this.tpl)
|
2015-04-05 15:03:16 -04:00
|
|
|
|
|
2017-03-13 18:55:02 -04:00
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
|
err := tmpl.Execute(buf, this.input)
|
|
|
|
|
if (err == nil) != this.ok {
|
|
|
|
|
t.Errorf("[%d] execute template returned unexpected error: %s", i, err)
|
2015-04-05 15:03:16 -04:00
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-13 18:55:02 -04:00
|
|
|
|
if buf.String() != this.expected {
|
|
|
|
|
t.Errorf("[%d] execute template got %v, but expected %v", i, buf.String(), this.expected)
|
2017-03-12 18:04:12 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-03-27 14:43:49 -04:00
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
}
|
2016-10-10 18:03:30 -04:00
|
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
|
func newTestTemplate(t *testing.T, name, template string) tpl.Template {
|
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(name, template)
|
|
|
|
|
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-01-09 19:36:59 -05: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())
|
2017-01-10 04:55:03 -05:00
|
|
|
|
|
|
|
|
|
return de.Tmpl.Lookup(name)
|
2017-01-09 19:36:59 -05:00
|
|
|
|
}
|