1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-11-26 23:34:47 +01:00
gojekyll/liquid/filters_test.go

113 lines
3.1 KiB
Go
Raw Normal View History

package liquid
2017-06-11 20:57:25 +02:00
import (
2017-06-28 20:42:04 +02:00
"fmt"
2017-06-11 20:57:25 +02:00
"strings"
"testing"
2017-06-16 04:30:53 +02:00
"time"
2017-06-11 20:57:25 +02:00
"github.com/stretchr/testify/require"
2017-06-11 20:57:25 +02:00
)
2017-06-26 19:17:33 +02:00
var filterTests = []struct{ in, expected string }{
2017-06-28 22:55:15 +02:00
// dates
// TODO date_to_xmlschema use local timezone
{`{{time | date_to_xmlschema}}`, "2008-11-07T13:07:54+00:00"},
{`{{time | date_to_rfc822 }}`, "07 Nov 08 13:07 UTC"},
{`{{time | date_to_string }}`, "07 Nov 2008"},
{`{{time | date_to_long_string }}`, "07 November 2008"},
// arrays
// TODO sort where group_by group_by_exp sample push pop shift
// site.members | where:"graduation_year","2014"
2017-06-28 20:42:04 +02:00
{`{{ar | array_to_sentence_string }}`, "first, second, and third"},
2017-06-28 22:55:15 +02:00
// strings
// TODO xml_escape cgi_escape uri_escape number_of_words
// TODO scssify smartify slugify normalize_whitespace to_integer
{`{{ "/assets/style.css" | relative_url }}`, "/my-baseurl/assets/style.css"},
{`{{ "/assets/style.css" | absolute_url }}`, "http://example.com/my-baseurl/assets/style.css"},
{`{{"Markdown with _emphasis_ and *bold*." | markdownify}}`, "<p>Markdown with <em>emphasis</em> and <em>bold</em>.</p>"},
{`{{obj | jsonify }}`, `{"a":[1,2,3,4]}`},
2017-06-28 20:42:04 +02:00
{`{{pages | map: "name" | join}}`, "a, b, c, d"},
{`{{pages | filter: "weight" | map: "name" | join}}`, "a, c, d"},
2017-06-26 19:17:33 +02:00
}
var filterTestScope = map[string]interface{}{
"ar": []string{"first", "second", "third"},
"obj": map[string]interface{}{
"a": []int{1, 2, 3, 4},
},
2017-06-28 20:42:04 +02:00
"pages": []map[string]interface{}{
{"name": "a", "weight": 10},
{"name": "b"},
{"name": "c", "weight": 50},
{"name": "d", "weight": 30},
},
2017-06-28 22:55:15 +02:00
"time": timeMustParse("2008-11-07T13:07:54Z"),
2017-06-26 19:17:33 +02:00
}
func timeMustParse(s string) time.Time {
2017-06-28 04:59:53 +02:00
t, err := time.Parse(time.RFC3339, s)
2017-06-26 19:17:33 +02:00
if err != nil {
panic(err)
}
return t
}
func TestFilters(t *testing.T) {
2017-06-28 20:42:04 +02:00
for i, test := range filterTests {
t.Run(fmt.Sprintf("%02d", i+1), func(t *testing.T) {
requireTemplateRender(t, test.in, filterTestScope, test.expected)
})
2017-06-26 19:17:33 +02:00
}
}
func requireTemplateRender(t *testing.T, tmpl string, scope map[string]interface{}, expected string) {
2017-06-29 13:27:43 +02:00
engine := NewEngine()
2017-06-28 22:55:15 +02:00
engine.BaseURL = "/my-baseurl"
engine.AbsoluteURL = "http://example.com"
data, err := engine.ParseAndRender([]byte(tmpl), scope)
2017-06-28 20:42:04 +02:00
require.NoErrorf(t, err, tmpl)
require.Equalf(t, expected, strings.TrimSpace(string(data)), tmpl)
2017-06-11 20:57:25 +02:00
}
2017-06-16 04:30:53 +02:00
// func TestXMLEscapeFilter(t *testing.T) {
// data := map[string]interface{}{
2017-06-16 04:30:53 +02:00
// "obj": map[string]interface{}{
// "a": []int{1, 2, 3, 4},
// },
// }
// requireTemplateRender(t, `{{obj | xml_escape }}`, data, `{"ak":[1,2,3,4]}`)
2017-06-16 04:30:53 +02:00
// }
2017-06-15 16:17:30 +02:00
func TestWhereExpFilter(t *testing.T) {
2017-06-11 20:57:25 +02:00
var tmpl = `
{% assign filtered = array | where_exp: "n", "n > 2" %}
{% for item in filtered %}{{item}}{% endfor %}
`
data := map[string]interface{}{
2017-06-11 20:57:25 +02:00
"array": []int{1, 2, 3, 4},
}
requireTemplateRender(t, tmpl, data, "34")
2017-06-11 20:57:25 +02:00
}
2017-06-15 16:17:30 +02:00
func TestWhereExpFilterObjects(t *testing.T) {
2017-06-11 20:57:25 +02:00
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,
},
}}
requireTemplateRender(t, tmpl, data, "A")
2017-06-11 20:57:25 +02:00
}