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

72 lines
2.0 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-25 22:21:31 +02:00
var chunkTests = []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"},
// "{% assign var = obj.a | sort: 'weight' %}"
2017-06-26 15:33:07 +02:00
// {"{%for a in ar%}{{a}} {{%endfor%}", "first second third "},
}
var chunkTestContext = Context{map[string]interface{}{
2017-06-26 21:36:05 +02:00
"x": 123,
"obj": map[string]interface{}{
"a": 1,
},
2017-06-26 15:33:07 +02:00
"ar": []string{"first", "second", "third"},
"page": map[string]interface{}{
"title": "Introduction",
},
},
}
func TestChunkParser(t *testing.T) {
for i, test := range chunkTests {
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)
err = ast.Render(buf, chunkTestContext)
require.NoErrorf(t, err, test.in)
require.Equalf(t, test.expected, buf.String(), test.in)
})
}
2017-06-25 17:23:20 +02:00
}