2017-07-04 17:03:18 +02:00
|
|
|
package render
|
2017-06-25 17:23:20 +02:00
|
|
|
|
|
|
|
import (
|
2017-06-25 23:00:00 +02:00
|
|
|
"bytes"
|
2017-06-26 02:47:31 +02:00
|
|
|
"fmt"
|
2017-07-02 13:51:24 +02:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2017-06-25 17:23:20 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2017-07-04 17:08:57 +02:00
|
|
|
func addRenderTestTags(s Config) {
|
2017-07-03 18:00:43 +02:00
|
|
|
s.AddBlock("parse").Parser(func(c ASTBlock) (func(io.Writer, RenderContext) error, error) {
|
2017-07-02 13:51:24 +02:00
|
|
|
a := c.Args
|
|
|
|
return func(w io.Writer, c RenderContext) error {
|
|
|
|
_, err := w.Write([]byte(a))
|
|
|
|
return err
|
|
|
|
}, nil
|
|
|
|
})
|
2017-07-02 19:31:34 +02:00
|
|
|
s.AddBlock("eval").Renderer(func(w io.Writer, c RenderContext) error {
|
2017-07-02 14:35:06 +02:00
|
|
|
v, err := c.EvaluateString(c.TagArgs())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s := fmt.Sprint(v)
|
|
|
|
_, err = w.Write([]byte(s))
|
|
|
|
return err
|
|
|
|
})
|
2017-07-03 18:00:43 +02:00
|
|
|
s.AddBlock("err2").Parser(func(c ASTBlock) (func(io.Writer, RenderContext) error, error) {
|
2017-07-02 13:51:24 +02:00
|
|
|
return func(w io.Writer, c RenderContext) error {
|
|
|
|
return fmt.Errorf("stage 2 error")
|
|
|
|
}, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
var renderTests = []struct{ in, out string }{
|
2017-06-29 18:20:16 +02:00
|
|
|
{`{{ 12 }}`, "12"},
|
|
|
|
{`{{ x }}`, "123"},
|
|
|
|
{`{{ page.title }}`, "Introduction"},
|
|
|
|
{`{{ ar[1] }}`, "second"},
|
2017-07-02 13:51:24 +02:00
|
|
|
{`{% parse args %}{% endparse %}`, "args"},
|
2017-07-02 14:35:06 +02:00
|
|
|
{`{% eval x %}{% endeval %}`, "123"},
|
2017-07-02 13:51:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var renderErrorTests = []struct{ in, out string }{
|
|
|
|
// {"{%if syntax error%}{%endif%}", "parse error"},
|
|
|
|
{`{% err2 %}{% enderr2 %}`, "stage 2 error"},
|
2017-06-26 15:33:07 +02:00
|
|
|
}
|
|
|
|
|
2017-06-30 22:46:17 +02:00
|
|
|
var renderTestBindings = 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"},
|
|
|
|
},
|
2017-06-27 14:46:06 +02:00
|
|
|
"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",
|
|
|
|
},
|
2017-06-30 22:46:17 +02:00
|
|
|
}
|
2017-06-26 15:33:07 +02:00
|
|
|
|
2017-06-27 00:55:12 +02:00
|
|
|
func TestRender(t *testing.T) {
|
2017-07-04 17:08:57 +02:00
|
|
|
settings := NewConfig()
|
2017-07-02 13:51:24 +02:00
|
|
|
addRenderTestTags(settings)
|
2017-06-30 22:46:17 +02:00
|
|
|
context := NewContext(renderTestBindings, settings)
|
2017-06-27 00:55:12 +02:00
|
|
|
for i, test := range renderTests {
|
2017-06-29 18:20:16 +02:00
|
|
|
t.Run(fmt.Sprintf("%02d", i+1), func(t *testing.T) {
|
2017-06-30 22:51:39 +02:00
|
|
|
ast, err := settings.Parse(test.in)
|
2017-06-26 15:33:07 +02:00
|
|
|
require.NoErrorf(t, err, test.in)
|
|
|
|
buf := new(bytes.Buffer)
|
2017-07-04 16:25:29 +02:00
|
|
|
err = renderNode(ast, buf, context)
|
2017-06-27 00:55:12 +02:00
|
|
|
require.NoErrorf(t, err, test.in)
|
2017-07-02 13:51:24 +02:00
|
|
|
require.Equalf(t, test.out, buf.String(), test.in)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRenderErrors(t *testing.T) {
|
2017-07-04 17:08:57 +02:00
|
|
|
settings := NewConfig()
|
2017-07-02 13:51:24 +02:00
|
|
|
addRenderTestTags(settings)
|
|
|
|
context := NewContext(renderTestBindings, settings)
|
|
|
|
for i, test := range renderErrorTests {
|
|
|
|
t.Run(fmt.Sprintf("%02d", i+1), func(t *testing.T) {
|
|
|
|
ast, err := settings.Parse(test.in)
|
|
|
|
require.NoErrorf(t, err, test.in)
|
2017-07-04 16:25:29 +02:00
|
|
|
err = renderNode(ast, ioutil.Discard, context)
|
2017-07-02 13:51:24 +02:00
|
|
|
require.Errorf(t, err, test.in)
|
|
|
|
require.Containsf(t, err.Error(), test.out, test.in)
|
2017-06-27 00:55:12 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|