2017-07-10 18:35:53 +02:00
|
|
|
package render
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"testing"
|
|
|
|
|
2017-07-14 16:38:30 +02:00
|
|
|
"github.com/osteele/liquid/parser"
|
2017-07-10 18:35:53 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func addContextTestTags(s Config) {
|
|
|
|
s.AddBlock("eval").Renderer(func(w io.Writer, c Context) error {
|
|
|
|
v, err := c.EvaluateString(c.TagArgs())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-07-16 03:13:21 +02:00
|
|
|
_, err = fmt.Fprint(w, v)
|
2017-07-10 18:35:53 +02:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
s.AddBlock("parse").Compiler(func(c BlockNode) (func(io.Writer, Context) error, error) {
|
|
|
|
a := c.Args
|
|
|
|
return func(w io.Writer, c Context) error {
|
2017-07-16 03:13:21 +02:00
|
|
|
_, err := io.WriteString(w, a)
|
2017-07-10 18:35:53 +02:00
|
|
|
return err
|
|
|
|
}, nil
|
|
|
|
})
|
|
|
|
s.AddTag("tag_name", func(string) (func(io.Writer, Context) error, error) {
|
|
|
|
return func(w io.Writer, c Context) error {
|
2017-07-16 03:13:21 +02:00
|
|
|
_, err := io.WriteString(w, c.TagName())
|
2017-07-10 18:35:53 +02:00
|
|
|
return err
|
|
|
|
}, nil
|
|
|
|
})
|
|
|
|
s.AddTag("expand_arg", func(string) (func(w io.Writer, c Context) error, error) {
|
|
|
|
return func(w io.Writer, c Context) error {
|
|
|
|
s, err := c.ExpandTagArg()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-07-16 03:13:21 +02:00
|
|
|
_, err = io.WriteString(w, s)
|
2017-07-10 18:35:53 +02:00
|
|
|
return err
|
|
|
|
}, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
var contextTests = []struct{ in, out string }{
|
|
|
|
{`{% parse args %}{% endparse %}`, "args"},
|
|
|
|
{`{% eval x %}{% endeval %}`, "123"},
|
|
|
|
{`{% expand_arg x %}`, "x"},
|
|
|
|
{`{% expand_arg {{x}} %}`, "123"},
|
|
|
|
{`{% tag_name %}`, "tag_name"},
|
|
|
|
}
|
|
|
|
|
|
|
|
var contextTestBindings = map[string]interface{}{
|
|
|
|
"x": 123,
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext(t *testing.T) {
|
|
|
|
cfg := NewConfig()
|
|
|
|
addContextTestTags(cfg)
|
|
|
|
for i, test := range contextTests {
|
|
|
|
t.Run(fmt.Sprintf("%02d", i+1), func(t *testing.T) {
|
2017-07-14 19:01:04 +02:00
|
|
|
root, err := cfg.Compile(test.in, parser.SourceLoc{})
|
2017-07-10 18:35:53 +02:00
|
|
|
require.NoErrorf(t, err, test.in)
|
|
|
|
buf := new(bytes.Buffer)
|
2017-07-16 23:43:04 +02:00
|
|
|
err = Render(root, buf, contextTestBindings, cfg)
|
2017-07-10 18:35:53 +02:00
|
|
|
require.NoErrorf(t, err, test.in)
|
|
|
|
require.Equalf(t, test.out, buf.String(), test.in)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|