1
0
mirror of https://github.com/danog/liquid.git synced 2024-11-30 08:18:59 +01:00
liquid/template_test.go
Oliver Steele 92a4f2d500 Source location is an initialization parameter
Fixes a race condition
2017-07-14 10:38:30 -04:00

98 lines
2.3 KiB
Go

package liquid
import (
"fmt"
"sync"
"testing"
"github.com/osteele/liquid/render"
"github.com/stretchr/testify/require"
)
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
})
tpl, err := engine.ParseTemplateLocation([]byte(`{% sourcepath %}`), "source.md", 1)
require.NoError(t, err)
out, err := tpl.RenderString(testBindings)
require.NoError(t, err)
require.Equal(t, "source.md", out)
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())
}
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 (
count = 10
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()
_, err := ts[i].Render(Bindings{})
require.Error(t, err)
require.Equal(t, paths[i], err.Path())
}(i)
}
wg2.Wait()
}