2017-07-04 22:48:38 +02:00
|
|
|
package liquid
|
|
|
|
|
|
|
|
import (
|
2017-07-14 16:17:34 +02:00
|
|
|
"fmt"
|
|
|
|
"sync"
|
2017-07-04 22:48:38 +02:00
|
|
|
"testing"
|
|
|
|
|
2022-01-06 18:43:17 +01:00
|
|
|
"github.com/danog/liquid/render"
|
2017-07-04 22:48:38 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2022-01-31 23:18:38 +01:00
|
|
|
func TestTemplate_GetRoot(t *testing.T) {
|
|
|
|
root := &render.SeqNode{}
|
|
|
|
tmpl := Template{root: root}
|
|
|
|
require.Same(t, root, tmpl.GetRoot())
|
|
|
|
}
|
|
|
|
|
2017-07-04 22:48:38 +02:00
|
|
|
func TestTemplate_RenderString(t *testing.T) {
|
|
|
|
engine := NewEngine()
|
|
|
|
tpl, err := engine.ParseTemplate([]byte(`{{ "hello world" | capitalize }}`))
|
|
|
|
require.NoError(t, err)
|
|
|
|
out, err := tpl.RenderString(testBindings)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "Hello world", out)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTemplate_SetSourcePath(t *testing.T) {
|
|
|
|
engine := NewEngine()
|
|
|
|
engine.RegisterTag("sourcepath", func(c render.Context) (string, error) {
|
|
|
|
return c.SourceFile(), nil
|
|
|
|
})
|
2017-07-14 16:38:30 +02:00
|
|
|
tpl, err := engine.ParseTemplateLocation([]byte(`{% sourcepath %}`), "source.md", 1)
|
2017-07-04 22:48:38 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
out, err := tpl.RenderString(testBindings)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "source.md", out)
|
2017-07-14 16:38:30 +02:00
|
|
|
|
|
|
|
src := []byte(`{{ n | undefined_filter }}`)
|
|
|
|
t1, err := engine.ParseTemplateLocation(src, "path1", 1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
t2, err := engine.ParseTemplateLocation(src, "path2", 1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
_, err = t1.Render(Bindings{})
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Equal(t, "path1", err.Path())
|
|
|
|
_, err = t2.Render(Bindings{})
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Equal(t, "path2", err.Path())
|
2017-07-04 22:48:38 +02:00
|
|
|
}
|
2017-07-14 16:17:34 +02:00
|
|
|
|
|
|
|
func TestTemplate_Parse_race(t *testing.T) {
|
|
|
|
var (
|
|
|
|
engine = NewEngine()
|
|
|
|
count = 10
|
|
|
|
wg sync.WaitGroup
|
|
|
|
)
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(i int) {
|
|
|
|
path := fmt.Sprintf("path %d", i)
|
|
|
|
_, err := engine.ParseTemplateLocation([]byte("{{ syntax error }}"), path, i)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Equal(t, path, err.Path())
|
|
|
|
wg.Done()
|
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTemplate_Render_race(t *testing.T) {
|
|
|
|
src := []byte(`{{ n | undefined_filter }}`)
|
|
|
|
engine := NewEngine()
|
|
|
|
|
|
|
|
var (
|
2017-07-14 16:38:30 +02:00
|
|
|
count = 10
|
2017-07-14 16:17:34 +02:00
|
|
|
paths = make([]string, count)
|
|
|
|
ts = make([]*Template, count)
|
|
|
|
wg sync.WaitGroup
|
|
|
|
)
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
paths[i] = fmt.Sprintf("path %d", i)
|
|
|
|
wg.Add(1)
|
|
|
|
go func(i int) {
|
|
|
|
defer wg.Done()
|
|
|
|
var err error
|
|
|
|
ts[i], err = engine.ParseTemplateLocation(src, paths[i], i)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
var wg2 sync.WaitGroup
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
wg2.Add(1)
|
|
|
|
go func(i int) {
|
|
|
|
defer wg2.Done()
|
2017-07-14 16:38:30 +02:00
|
|
|
_, err := ts[i].Render(Bindings{})
|
2017-07-14 16:17:34 +02:00
|
|
|
require.Error(t, err)
|
2017-07-14 16:38:30 +02:00
|
|
|
require.Equal(t, paths[i], err.Path())
|
2017-07-14 16:17:34 +02:00
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
wg2.Wait()
|
|
|
|
}
|
2017-07-22 14:04:09 +02:00
|
|
|
|
|
|
|
func BenchmarkTemplate_Render(b *testing.B) {
|
|
|
|
engine := NewEngine()
|
|
|
|
bindings := Bindings{"a": "string value"}
|
|
|
|
tpl, err := engine.ParseString(`{% for i in (1..1000) %}{% if i > 500 %}{{a}}{% else %}0{% endif %}{% endfor %}`)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2021-06-16 12:42:31 +02:00
|
|
|
_, err := tpl.Render(bindings)
|
|
|
|
require.NoError(b, err)
|
2017-07-22 14:04:09 +02:00
|
|
|
}
|
|
|
|
}
|