1
0
mirror of https://github.com/danog/liquid.git synced 2024-11-27 06:24:39 +01:00
liquid/engine_test.go
2017-07-21 11:35:11 -04:00

66 lines
1.6 KiB
Go

package liquid
import (
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
var emptyBindings = map[string]interface{}{}
// There's a lot more tests in the filters and tags sub-packages.
// This collects a minimal set for testing end-to-end.
var liquidTests = []struct{ in, expected string }{
{`{{ page.title }}`, "Introduction"},
{`{% if x %}true{% endif %}`, "true"},
{`{{ "upper" | upcase }}`, "UPPER"},
}
var testBindings = map[string]interface{}{
"x": 123,
"ar": []string{"first", "second", "third"},
"page": map[string]interface{}{
"title": "Introduction",
},
}
func TestEngine_ParseAndRenderString(t *testing.T) {
engine := NewEngine()
for i, test := range liquidTests {
t.Run(fmt.Sprint(i+1), func(t *testing.T) {
out, err := engine.ParseAndRenderString(test.in, testBindings)
require.NoErrorf(t, err, test.in)
require.Equalf(t, test.expected, out, test.in)
})
}
}
func TestEngine_ParseAndRenderString_ptr_to_hash(t *testing.T) {
params := map[string]interface{}{
"message": &map[string]interface{}{
"Text": "hello",
},
}
engine := NewEngine()
template := "{{ message.Text }}"
str, err := engine.ParseAndRenderString(template, params)
require.NoError(t, err)
require.Equal(t, "hello", str)
}
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)
}