1
0
mirror of https://github.com/danog/liquid.git synced 2024-11-27 06:34:45 +01:00
liquid/chunks/render_test.go
2017-06-29 12:20:16 -04:00

62 lines
1.3 KiB
Go

package chunks
import (
"bytes"
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
var renderTests = []struct{ in, expected string }{
// {"{%if syntax error%}{%endif%}", "parse error"},
{`{{ 12 }}`, "12"},
{`{{ x }}`, "123"},
{`{{ page.title }}`, "Introduction"},
{`{{ ar[1] }}`, "second"},
}
var renderTestContext = Context{map[string]interface{}{
"x": 123,
"obj": map[string]interface{}{
"a": 1,
},
"animals": []string{"zebra", "octopus", "giraffe", "Sally Snake"},
"pages": []map[string]interface{}{
{"category": "business"},
{"category": "celebrities"},
{},
{"category": "lifestyle"},
{"category": "sports"},
{},
{"category": "technology"},
},
"sort_prop": []map[string]interface{}{
{"weight": 1},
{"weight": 5},
{"weight": 3},
{"weight": nil},
},
"ar": []string{"first", "second", "third"},
"page": map[string]interface{}{
"title": "Introduction",
},
},
}
func TestRender(t *testing.T) {
for i, test := range renderTests {
t.Run(fmt.Sprintf("%02d", i+1), func(t *testing.T) {
tokens := Scan(test.in, "")
// fmt.Println(tokens)
ast, err := Parse(tokens)
require.NoErrorf(t, err, test.in)
// fmt.Println(MustYAML(ast))
buf := new(bytes.Buffer)
err = ast.Render(buf, renderTestContext)
require.NoErrorf(t, err, test.in)
require.Equalf(t, test.expected, buf.String(), test.in)
})
}
}