2017-06-26 16:15:01 +02:00
|
|
|
package liquid
|
|
|
|
|
|
|
|
import (
|
2017-07-22 14:04:09 +02:00
|
|
|
"bytes"
|
2019-12-20 12:51:52 +01:00
|
|
|
"encoding/json"
|
2017-06-26 16:15:01 +02:00
|
|
|
"fmt"
|
2017-07-22 14:04:09 +02:00
|
|
|
"io"
|
2017-06-26 16:15:01 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2017-07-04 22:48:38 +02:00
|
|
|
var emptyBindings = map[string]interface{}{}
|
|
|
|
|
2017-06-30 14:45:22 +02:00
|
|
|
// There's a lot more tests in the filters and tags sub-packages.
|
|
|
|
// This collects a minimal set for testing end-to-end.
|
2017-06-26 16:15:01 +02:00
|
|
|
var liquidTests = []struct{ in, expected string }{
|
2017-06-30 14:45:22 +02:00
|
|
|
{`{{ page.title }}`, "Introduction"},
|
2017-06-30 22:13:18 +02:00
|
|
|
{`{% if x %}true{% endif %}`, "true"},
|
|
|
|
{`{{ "upper" | upcase }}`, "UPPER"},
|
2017-06-26 16:15:01 +02:00
|
|
|
}
|
|
|
|
|
2017-07-03 03:17:04 +02:00
|
|
|
var testBindings = map[string]interface{}{
|
2017-06-26 16:15:01 +02:00
|
|
|
"x": 123,
|
|
|
|
"ar": []string{"first", "second", "third"},
|
|
|
|
"page": map[string]interface{}{
|
|
|
|
"title": "Introduction",
|
|
|
|
},
|
2017-07-03 03:17:04 +02:00
|
|
|
}
|
2017-06-26 16:15:01 +02:00
|
|
|
|
2017-07-04 22:48:38 +02:00
|
|
|
func TestEngine_ParseAndRenderString(t *testing.T) {
|
2017-06-26 16:15:01 +02:00
|
|
|
engine := NewEngine()
|
|
|
|
for i, test := range liquidTests {
|
2017-06-30 22:13:18 +02:00
|
|
|
t.Run(fmt.Sprint(i+1), func(t *testing.T) {
|
2017-07-03 03:17:04 +02:00
|
|
|
out, err := engine.ParseAndRenderString(test.in, testBindings)
|
2017-06-26 16:15:01 +02:00
|
|
|
require.NoErrorf(t, err, test.in)
|
2017-06-26 16:36:53 +02:00
|
|
|
require.Equalf(t, test.expected, out, test.in)
|
2017-06-26 16:15:01 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2017-07-21 17:35:11 +02:00
|
|
|
|
|
|
|
func TestEngine_ParseAndRenderString_ptr_to_hash(t *testing.T) {
|
|
|
|
params := map[string]interface{}{
|
|
|
|
"message": &map[string]interface{}{
|
2021-06-26 09:24:21 +02:00
|
|
|
"Text": "hello",
|
|
|
|
"jsonNumber": json.Number("123"),
|
2017-07-21 17:35:11 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
engine := NewEngine()
|
2019-12-20 12:51:52 +01:00
|
|
|
template := "{{ message.Text }} {{message.jsonNumber}}"
|
2017-07-21 17:35:11 +02:00
|
|
|
str, err := engine.ParseAndRenderString(template, params)
|
|
|
|
require.NoError(t, err)
|
2019-12-20 12:51:52 +01:00
|
|
|
require.Equal(t, "hello 123", str)
|
2017-07-21 17:35:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type testStruct struct{ Text string }
|
|
|
|
|
|
|
|
func TestEngine_ParseAndRenderString_struct(t *testing.T) {
|
|
|
|
params := map[string]interface{}{
|
|
|
|
"message": testStruct{
|
|
|
|
Text: "hello",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
engine := NewEngine()
|
|
|
|
template := "{{ message.Text }}"
|
|
|
|
str, err := engine.ParseAndRenderString(template, params)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "hello", str)
|
|
|
|
}
|
2017-07-22 14:04:09 +02:00
|
|
|
|
2017-07-25 14:16:30 +02:00
|
|
|
func TestEngine_ParseAndRender_errors(t *testing.T) {
|
|
|
|
_, err := NewEngine().ParseAndRenderString("{{ syntax error }}", emptyBindings)
|
|
|
|
require.Error(t, err)
|
|
|
|
_, err = NewEngine().ParseAndRenderString("{% if %}", emptyBindings)
|
|
|
|
require.Error(t, err)
|
|
|
|
_, err = NewEngine().ParseAndRenderString("{% undefined_tag %}", emptyBindings)
|
|
|
|
require.Error(t, err)
|
|
|
|
_, err = NewEngine().ParseAndRenderString("{% a | undefined_filter %}", emptyBindings)
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2017-07-22 14:04:09 +02:00
|
|
|
func BenchmarkEngine_Parse(b *testing.B) {
|
|
|
|
engine := NewEngine()
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
for i := 0; i < 1000; i++ {
|
2021-06-16 12:42:31 +02:00
|
|
|
_, err := io.WriteString(buf, `if{% if true %}true{% elsif %}elsif{% else %}else{% endif %}`)
|
|
|
|
require.NoError(b, err)
|
|
|
|
_, err = io.WriteString(buf, `loop{% for item in array %}loop{% break %}{% endfor %}`)
|
|
|
|
require.NoError(b, err)
|
|
|
|
_, err = io.WriteString(buf, `case{% case value %}{% when a %}{% when b %{% endcase %}`)
|
|
|
|
require.NoError(b, err)
|
|
|
|
_, err = io.WriteString(buf, `expr{{ a and b }}{{ a add: b }}`)
|
|
|
|
require.NoError(b, err)
|
2017-07-22 14:04:09 +02:00
|
|
|
}
|
|
|
|
s := buf.Bytes()
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2021-06-16 12:42:31 +02:00
|
|
|
_, err := engine.ParseTemplate(s)
|
|
|
|
require.NoError(b, err)
|
2017-07-22 14:04:09 +02:00
|
|
|
}
|
|
|
|
}
|
2020-11-03 01:36:02 +01:00
|
|
|
|
|
|
|
func TestEngine_ParseTemplateAndCache(t *testing.T) {
|
|
|
|
// Given two templates...
|
|
|
|
templateA := []byte("Foo")
|
|
|
|
templateB := []byte(`{% include "template_a.html" %}, Bar`)
|
|
|
|
|
|
|
|
// Cache the first
|
|
|
|
eng := NewEngine()
|
|
|
|
_, err := eng.ParseTemplateAndCache(templateA, "template_a.html", 1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// ...and execute the second.
|
|
|
|
result, err := eng.ParseAndRender(templateB, Bindings{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, string(result), "Foo, Bar")
|
|
|
|
}
|