1
0
mirror of https://github.com/danog/liquid.git synced 2024-12-02 11:07:44 +01:00
liquid/render/render_test.go

130 lines
3.3 KiB
Go
Raw Permalink Normal View History

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"
"fmt"
2017-07-02 13:51:24 +02:00
"io"
"io/ioutil"
2017-06-25 17:23:20 +02:00
"testing"
2017-08-18 16:35:15 +02:00
"time"
2017-06-25 17:23:20 +02:00
2022-01-06 18:43:17 +01:00
"github.com/danog/liquid/parser"
2017-06-25 17:23:20 +02:00
"github.com/stretchr/testify/require"
)
2017-07-02 13:51:24 +02:00
var renderTests = []struct{ in, out string }{
2017-08-18 16:35:15 +02:00
// literal representations
2017-07-14 20:28:02 +02:00
{`{{ nil }}`, ""},
{`{{ true }}`, "true"},
{`{{ false }}`, "false"},
2017-06-29 18:20:16 +02:00
{`{{ 12 }}`, "12"},
2017-07-14 20:28:02 +02:00
{`{{ 12.3 }}`, "12.3"},
{`{{ date }}`, "2015-07-17 15:04:05 +0000"},
2017-08-18 16:35:15 +02:00
{`{{ "string" }}`, "string"},
2017-07-16 23:43:04 +02:00
{`{{ array }}`, "firstsecondthird"},
// variables and properties
2017-08-18 16:35:15 +02:00
{`{{ int }}`, "123"},
2017-06-29 18:20:16 +02:00
{`{{ page.title }}`, "Introduction"},
2017-07-10 18:35:53 +02:00
{`{{ array[1] }}`, "second"},
2017-07-16 23:43:04 +02:00
// whitespace control
2017-08-18 16:35:15 +02:00
{` {{ 1 }} `, " 1 "},
2017-07-16 23:43:04 +02:00
{` {{- 1 }} `, "1 "},
{` {{ 1 -}} `, " 1"},
{` {{- 1 -}} `, "1"},
{` {{- nil -}} `, ""},
{`x {{ 1 }} z`, "x 1 z"},
{`x {{- 1 }} z`, "x1 z"},
{`x {{ 1 -}} z`, "x 1z"},
{`x {{- 1 -}} z`, "x1z"},
{`x {{ nil }} z`, "x z"},
{`x {{- nil }} z`, "x z"},
{`x {{ nil -}} z`, "x z"},
{`x {{- nil -}} z`, "xz"},
{`x {% null %} z`, "x z"},
{`x {%- null %} z`, "x z"},
{`x {% null -%} z`, "x z"},
{`x {%- null -%} z`, "xz"},
{`x {% y %} z`, "x y z"},
{`x {%- y %} z`, "xy z"},
{`x {% y -%} z`, "x yz"},
{`x {%- y -%} z`, "xyz"},
2017-07-02 13:51:24 +02:00
}
var renderErrorTests = []struct{ in, out string }{
2017-07-14 20:28:02 +02:00
{`{% errblock %}{% enderrblock %}`, "errblock error"},
2017-06-26 15:33:07 +02:00
}
2017-06-30 22:46:17 +02:00
var renderTestBindings = map[string]interface{}{
2017-07-10 18:35:53 +02:00
"array": []string{"first", "second", "third"},
"date": time.Date(2015, 7, 17, 15, 4, 5, 123456789, time.UTC),
2017-08-18 16:35:15 +02:00
"int": 123,
"sort_prop": []map[string]interface{}{
{"weight": 1},
{"weight": 5},
{"weight": 3},
{"weight": nil},
2017-06-26 21:36:05 +02:00
},
2017-08-18 16:35:15 +02:00
// for examples from liquid docs
2017-06-27 00:55:12 +02:00
"animals": []string{"zebra", "octopus", "giraffe", "Sally Snake"},
2017-08-18 16:35:15 +02:00
"page": map[string]interface{}{
"title": "Introduction",
},
2017-06-27 00:55:12 +02:00
"pages": []map[string]interface{}{
{"category": "business"},
{"category": "celebrities"},
{},
{"category": "lifestyle"},
{"category": "sports"},
{},
{"category": "technology"},
},
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-10 18:35:53 +02:00
cfg := NewConfig()
addRenderTestTags(cfg)
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) {
root, err := cfg.Compile(test.in, parser.SourceLoc{})
2017-06-26 15:33:07 +02:00
require.NoErrorf(t, err, test.in)
buf := new(bytes.Buffer)
2017-07-16 23:43:04 +02:00
err = Render(root, buf, renderTestBindings, cfg)
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-10 18:35:53 +02:00
cfg := NewConfig()
addRenderTestTags(cfg)
2017-07-02 13:51:24 +02:00
for i, test := range renderErrorTests {
t.Run(fmt.Sprintf("%02d", i+1), func(t *testing.T) {
root, err := cfg.Compile(test.in, parser.SourceLoc{})
2017-07-02 13:51:24 +02:00
require.NoErrorf(t, err, test.in)
2017-07-16 23:43:04 +02:00
err = Render(root, ioutil.Discard, renderTestBindings, cfg)
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
})
}
}
2017-08-18 16:35:15 +02:00
func addRenderTestTags(cfg Config) {
cfg.AddTag("y", func(string) (func(io.Writer, Context) error, error) {
return func(w io.Writer, _ Context) error {
_, err := io.WriteString(w, "y")
return err
}, nil
})
cfg.AddTag("null", func(string) (func(io.Writer, Context) error, error) {
return func(io.Writer, Context) error { return nil }, nil
})
cfg.AddBlock("errblock").Compiler(func(c BlockNode) (func(io.Writer, Context) error, error) {
return func(w io.Writer, c Context) error {
return fmt.Errorf("errblock error")
}, nil
})
}