1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-11-27 10:44:39 +01:00
gojekyll/liquid_filters_test.go
2017-06-13 17:19:05 -04:00

58 lines
1.1 KiB
Go

package main
import (
"bytes"
"strings"
"testing"
"github.com/acstech/liquid"
"github.com/stretchr/testify/assert"
)
func assertTemplateRender(t *testing.T, tmpl string, data map[string]interface{}, expected string) {
template, err := liquid.ParseString(tmpl, nil)
if err != nil {
t.Fatalf(err.Error())
return
}
writer := new(bytes.Buffer)
template.Render(writer, data)
assert.Equal(t, expected, strings.TrimSpace(writer.String()))
}
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},
}
assertTemplateRender(t, tmpl, data, "34")
}
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{}{
{
"name": "A",
"flag": true,
},
{
"name": "B",
"flag": false,
},
}}
assertTemplateRender(t, tmpl, data, "A")
}