1
0
mirror of https://github.com/danog/liquid.git synced 2024-11-27 03:24:39 +01:00
liquid/chunks/render_test.go

151 lines
4.4 KiB
Go
Raw Normal View History

2017-06-26 15:36:52 +02:00
package chunks
2017-06-25 17:23:20 +02:00
import (
2017-06-25 23:00:00 +02:00
"bytes"
"fmt"
2017-06-25 17:23:20 +02:00
"testing"
"github.com/stretchr/testify/require"
)
2017-06-27 00:55:12 +02:00
var renderTests = []struct{ in, expected string }{
2017-06-25 23:00:00 +02:00
{"{{12}}", "12"},
{"{{x}}", "123"},
2017-06-26 04:59:33 +02:00
{"{{page.title}}", "Introduction"},
2017-06-26 13:50:53 +02:00
{"{{ar[1]}}", "second"},
2017-06-26 21:36:05 +02:00
2017-06-26 02:20:58 +02:00
{"{%if true%}true{%endif%}", "true"},
{"{%if false%}false{%endif%}", ""},
{"{%if 0%}true{%endif%}", "true"},
2017-06-25 23:26:14 +02:00
{"{%if 1%}true{%endif%}", "true"},
{"{%if x%}true{%endif%}", "true"},
2017-06-26 02:20:58 +02:00
{"{%if y%}true{%endif%}", ""},
{"{%if true%}true{%endif%}", "true"},
{"{%if false%}false{%endif%}", ""},
{"{%if true%}true{%else%}false{%endif%}", "true"},
{"{%if false%}false{%else%}true{%endif%}", "true"},
{"{%if true%}0{%elsif true%}1{%else%}2{%endif%}", "0"},
{"{%if false%}0{%elsif true%}1{%else%}2{%endif%}", "1"},
{"{%if false%}0{%elsif false%}1{%else%}2{%endif%}", "2"},
2017-06-26 21:36:05 +02:00
2017-06-26 02:20:58 +02:00
{"{%unless true%}false{%endif%}", ""},
{"{%unless false%}true{%endif%}", "true"},
{"{%unless true%}false{%else%}true{%endif%}", "true"},
{"{%unless false%}true{%else%}false{%endif%}", "true"},
{"{%unless false%}0{%elsif true%}1{%else%}2{%endif%}", "0"},
{"{%unless true%}0{%elsif true%}1{%else%}2{%endif%}", "1"},
{"{%unless true%}0{%elsif false%}1{%else%}2{%endif%}", "2"},
2017-06-26 21:36:05 +02:00
{"{%assign av = 1%}{{av}}", "1"},
{"{%assign av = obj.a%}{{av}}", "1"},
2017-06-27 00:55:12 +02:00
2017-06-26 15:33:07 +02:00
// {"{%for a in ar%}{{a}} {{%endfor%}", "first second third "},
}
2017-06-27 00:55:12 +02:00
var filterTests = []struct{ in, expected string }{
// filters
// {{ product_price | default: 2.99 }}
// list filters
// {{ site.pages | map: 'category' | compact | join "," %}
// {% assign my_array = "apples, oranges, peaches, plums" | split: ", " %}{{ my_array.first }}
2017-06-27 03:32:08 +02:00
// {`{{"John, Paul, George, Ringo" | split: ", " }}`, "John and Paul and George and Ringo"},
{`{{"John, Paul, George, Ringo" | split: ", " | join: " and "}}`, "John and Paul and George and Ringo"},
{`{{ animals | sort | join: ", " }}`, "Sally Snake, giraffe, octopus, zebra"},
2017-06-27 00:55:12 +02:00
// join, last, map, slice, sort, sort_natural, reverse, size, uniq
// string filters
// {{ "/my/fancy/url" | append: ".html" }}
// {% assign filename = "/index.html" %}{{ "website.com" | append: filename }}
// {{ "title" | capitalize }}
// {{ "my great title" | capitalize }}
// {{ "Parker Moore" | downcase }}
// {{ "Have you read 'James & the Giant Peach'?" | escape }}
// {{ "1 < 2 & 3" | escape_once }}
// {{ "1 &lt; 2 &amp; 3" | escape_once }}
// lstrip, newline_to_br, prepend, remove, remove_first, replace, replace_first
// rstrip, split, strip, strip_html, strip_newlines, truncate, truncatewords, upcase
// url_decode, url_encode
// number filters
// {{ -17 | abs }}
// {{ 4 | abs }}
// {{ "-19.86" | abs }}
// {{ 1.2 | ceil }}
// {{ 2.0 | ceil }}
// {{ 183.357 | ceil }}
// {{ "3.5" | ceil }}
// {{ 16 | divided_by: 4 }}
// {{ 5 | divided_by: 3 }}
// {{ 20 | divided_by: 7.0 }}
// {{ 1.2 | floor }}
// {{ 2.0 | floor }}
// {{ 183.357 | floor }}
// minus, modulo, plus, round,times
// date filters
// {{ article.published_at | date: "%a, %b %d, %y" }}
// {{ article.published_at | date: "%Y" }}
// {{ "March 14, 2016" | date: "%b %d, %y" }}
// {{ "now" | date: "%Y-%m-%d %H:%M" }
}
var renderTestContext = Context{map[string]interface{}{
2017-06-26 21:36:05 +02:00
"x": 123,
"obj": map[string]interface{}{
"a": 1,
},
2017-06-27 00:55:12 +02:00
"animals": []string{"zebra", "octopus", "giraffe", "Sally Snake"},
"pages": []map[string]interface{}{
{"category": "business"},
{"category": "celebrities"},
{},
{"category": "lifestyle"},
{"category": "sports"},
{},
{"category": "technology"},
},
2017-06-26 15:33:07 +02:00
"ar": []string{"first", "second", "third"},
"page": map[string]interface{}{
"title": "Introduction",
},
},
}
2017-06-27 00:55:12 +02:00
func TestRender(t *testing.T) {
for i, test := range renderTests {
t.Run(fmt.Sprintf("%02d", i), func(t *testing.T) {
tokens := Scan(test.in, "")
2017-06-26 15:33:07 +02:00
// fmt.Println(tokens)
ast, err := Parse(tokens)
require.NoErrorf(t, err, test.in)
// fmt.Println(MustYAML(ast))
buf := new(bytes.Buffer)
2017-06-27 00:55:12 +02:00
err = ast.Render(buf, renderTestContext)
require.NoErrorf(t, err, test.in)
require.Equalf(t, test.expected, buf.String(), test.in)
})
}
}
func TestFilters(t *testing.T) {
for i, test := range filterTests {
t.Run(fmt.Sprintf("%02d", i), func(t *testing.T) {
tokens := Scan(test.in, "")
ast, err := Parse(tokens)
require.NoErrorf(t, err, test.in)
buf := new(bytes.Buffer)
err = ast.Render(buf, renderTestContext)
2017-06-26 15:33:07 +02:00
require.NoErrorf(t, err, test.in)
require.Equalf(t, test.expected, buf.String(), test.in)
})
}
2017-06-25 17:23:20 +02:00
}