1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-11-26 23:14:40 +01:00
gojekyll/liquid_filters_test.go

58 lines
1.1 KiB
Go
Raw Normal View History

2017-06-11 20:57:25 +02:00
package main
import (
"bytes"
"strings"
"testing"
"github.com/acstech/liquid"
2017-06-11 22:00:03 +02:00
"github.com/stretchr/testify/assert"
2017-06-11 20:57:25 +02:00
)
2017-06-11 22:00:03 +02:00
func assertTemplateRender(t *testing.T, tmpl string, data map[string]interface{}, expected string) {
2017-06-11 20:57:25 +02:00
template, err := liquid.ParseString(tmpl, nil)
if err != nil {
t.Fatalf(err.Error())
return
}
writer := new(bytes.Buffer)
template.Render(writer, data)
2017-06-11 22:00:03 +02:00
assert.Equal(t, expected, strings.TrimSpace(writer.String()))
2017-06-11 20:57:25 +02:00
}
func TestWhereExp(t *testing.T) {
var tmpl = `
{% assign filtered = array | where_exp: "n", "n > 2" %}
{% for item in filtered %}{{item}}{% endfor %}
`
data := map[string]interface{}{
"array": []int{1, 2, 3, 4},
}
2017-06-11 22:00:03 +02:00
assertTemplateRender(t, tmpl, data, "34")
2017-06-11 20:57:25 +02:00
}
func TestWhereExpObjects(t *testing.T) {
var tmpl = `
{% assign filtered = array | where_exp: "item", "item.flag == true" %}
{% for item in filtered %}{{item.name}}{% endfor %}
`
data := map[string]interface{}{
"array": []map[string]interface{}{
2017-06-13 23:19:05 +02:00
{
2017-06-11 20:57:25 +02:00
"name": "A",
"flag": true,
},
2017-06-13 23:19:05 +02:00
{
2017-06-11 20:57:25 +02:00
"name": "B",
"flag": false,
},
}}
2017-06-11 22:00:03 +02:00
assertTemplateRender(t, tmpl, data, "A")
2017-06-11 20:57:25 +02:00
}