-
Notifications
You must be signed in to change notification settings - Fork 59
/
template_test.go
118 lines (107 loc) · 2.77 KB
/
template_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package liquid
import (
"fmt"
"sync"
"testing"
"github.com/osteele/liquid/render"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestTemplate_GetRoot(t *testing.T) {
root := &render.SeqNode{}
tmpl := Template{root: root}
require.Same(t, root, tmpl.GetRoot())
}
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 := range count {
wg.Add(1)
go func(i int) {
path := fmt.Sprintf("path %d", i)
_, err := engine.ParseTemplateLocation([]byte("{{ syntax error }}"), path, i)
assert.Error(t, err)
assert.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 := range count {
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)
assert.NoError(t, err)
}(i)
}
wg.Wait()
var wg2 sync.WaitGroup
for i := range count {
wg2.Add(1)
go func(i int) {
defer wg2.Done()
_, err := ts[i].Render(Bindings{})
assert.Error(t, err)
assert.Equal(t, paths[i], err.Path())
}(i)
}
wg2.Wait()
}
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 range b.N {
_, err := tpl.Render(bindings)
require.NoError(b, err)
}
}