1
0
mirror of https://github.com/danog/liquid.git synced 2024-12-12 21:07:21 +01:00
liquid/chunks/render_test.go

79 lines
1.8 KiB
Go
Raw Normal View History

2017-06-26 15:36:52 +02:00
package chunks
2017-06-25 17:23:20 +02:00
import (
2017-06-25 23:00:00 +02:00
"bytes"
"fmt"
2017-06-25 17:23:20 +02:00
"testing"
"github.com/stretchr/testify/require"
)
var parseErrorTests = []struct{ in, expected string }{
{"{%unknown_tag%}", "unknown tag"},
2017-06-27 17:39:32 +02:00
// {"{%if syntax error%}", "unterminated if tag"},
2017-06-27 17:12:58 +02:00
// {"{%if syntax error%}{%endif%}", "parse error"},
}
2017-06-27 00:55:12 +02:00
var renderTests = []struct{ in, expected string }{
2017-06-27 17:12:58 +02:00
// {"{%if syntax error%}{%endif%}", "parse error"},
2017-06-25 23:00:00 +02:00
{"{{12}}", "12"},
{"{{x}}", "123"},
2017-06-26 04:59:33 +02:00
{"{{page.title}}", "Introduction"},
2017-06-26 13:50:53 +02:00
{"{{ar[1]}}", "second"},
2017-06-26 15:33:07 +02:00
}
2017-06-27 00:55:12 +02:00
var renderTestContext = Context{map[string]interface{}{
2017-06-26 21:36:05 +02:00
"x": 123,
"obj": map[string]interface{}{
"a": 1,
},
2017-06-27 00:55:12 +02:00
"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},
},
2017-06-26 15:33:07 +02:00
"ar": []string{"first", "second", "third"},
"page": map[string]interface{}{
"title": "Introduction",
},
},
}
func TestParseErrors(t *testing.T) {
for i, test := range parseErrorTests {
t.Run(fmt.Sprintf("%02d", i), func(t *testing.T) {
tokens := Scan(test.in, "")
ast, err := Parse(tokens)
require.Nilf(t, ast, test.in)
require.Errorf(t, err, test.in)
require.Containsf(t, err.Error(), test.expected, test.in)
})
}
}
2017-06-27 00:55:12 +02:00
func TestRender(t *testing.T) {
for i, test := range renderTests {
t.Run(fmt.Sprintf("%02d", i), func(t *testing.T) {
tokens := Scan(test.in, "")
2017-06-26 15:33:07 +02:00
// fmt.Println(tokens)
ast, err := Parse(tokens)
require.NoErrorf(t, err, test.in)
// fmt.Println(MustYAML(ast))
buf := new(bytes.Buffer)
2017-06-27 00:55:12 +02:00
err = ast.Render(buf, renderTestContext)
require.NoErrorf(t, err, test.in)
require.Equalf(t, test.expected, buf.String(), test.in)
})
}
}