2017-06-26 16:15:01 +02:00
|
|
|
package liquid
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-06-26 16:36:53 +02:00
|
|
|
"log"
|
2017-07-02 13:51:24 +02:00
|
|
|
"strings"
|
2017-06-26 16:15:01 +02:00
|
|
|
"testing"
|
|
|
|
|
2017-07-04 17:03:18 +02:00
|
|
|
"github.com/osteele/liquid/render"
|
2017-06-26 16:15:01 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2017-07-04 22:48:38 +02:00
|
|
|
var emptyBindings = map[string]interface{}{}
|
|
|
|
|
2017-06-30 14:45:22 +02:00
|
|
|
// There's a lot more tests in the filters and tags sub-packages.
|
|
|
|
// This collects a minimal set for testing end-to-end.
|
2017-06-26 16:15:01 +02:00
|
|
|
var liquidTests = []struct{ in, expected string }{
|
2017-06-30 14:45:22 +02:00
|
|
|
{`{{ page.title }}`, "Introduction"},
|
2017-06-30 22:13:18 +02:00
|
|
|
{`{% if x %}true{% endif %}`, "true"},
|
|
|
|
{`{{ "upper" | upcase }}`, "UPPER"},
|
2017-06-26 16:15:01 +02:00
|
|
|
}
|
|
|
|
|
2017-07-03 03:17:04 +02:00
|
|
|
var testBindings = map[string]interface{}{
|
2017-06-26 16:15:01 +02:00
|
|
|
"x": 123,
|
|
|
|
"ar": []string{"first", "second", "third"},
|
|
|
|
"page": map[string]interface{}{
|
|
|
|
"title": "Introduction",
|
|
|
|
},
|
2017-07-03 03:17:04 +02:00
|
|
|
}
|
2017-06-26 16:15:01 +02:00
|
|
|
|
2017-07-04 22:48:38 +02:00
|
|
|
func TestEngine_ParseAndRenderString(t *testing.T) {
|
2017-06-26 16:15:01 +02:00
|
|
|
engine := NewEngine()
|
|
|
|
for i, test := range liquidTests {
|
2017-06-30 22:13:18 +02:00
|
|
|
t.Run(fmt.Sprint(i+1), func(t *testing.T) {
|
2017-07-03 03:17:04 +02:00
|
|
|
out, err := engine.ParseAndRenderString(test.in, testBindings)
|
2017-06-26 16:15:01 +02:00
|
|
|
require.NoErrorf(t, err, test.in)
|
2017-06-26 16:36:53 +02:00
|
|
|
require.Equalf(t, test.expected, out, test.in)
|
2017-06-26 16:15:01 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2017-06-26 16:36:53 +02:00
|
|
|
|
|
|
|
func Example() {
|
|
|
|
engine := NewEngine()
|
2017-07-10 15:38:46 +02:00
|
|
|
source := `<h1>{{ page.title }}</h1>`
|
2017-06-29 19:08:25 +02:00
|
|
|
bindings := map[string]interface{}{
|
2017-07-02 13:51:24 +02:00
|
|
|
"page": map[string]string{
|
2017-06-26 16:36:53 +02:00
|
|
|
"title": "Introduction",
|
|
|
|
},
|
|
|
|
}
|
2017-07-10 15:38:46 +02:00
|
|
|
out, err := engine.ParseAndRenderString(source, bindings)
|
2017-06-26 16:36:53 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
fmt.Println(out)
|
|
|
|
// Output: <h1>Introduction</h1>
|
|
|
|
}
|
2017-07-02 13:51:24 +02:00
|
|
|
|
2017-07-10 15:38:46 +02:00
|
|
|
func ExampleEngine_ParseAndRenderString() {
|
|
|
|
engine := NewEngine()
|
|
|
|
source := `{{ hello | capitalize | append: " Mundo" }}`
|
|
|
|
bindings := map[string]interface{}{"hello": "hola"}
|
|
|
|
out, err := engine.ParseAndRenderString(source, bindings)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
fmt.Println(out)
|
|
|
|
// Output: Hola Mundo
|
|
|
|
}
|
|
|
|
func ExampleEngine_ParseTemplate() {
|
|
|
|
engine := NewEngine()
|
|
|
|
source := `{{ hello | capitalize | append: " Mundo" }}`
|
|
|
|
bindings := map[string]interface{}{"hello": "hola"}
|
|
|
|
tpl, err := engine.ParseTemplate([]byte(source))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
out, err := tpl.RenderString(bindings)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
fmt.Println(out)
|
|
|
|
// Output: Hola Mundo
|
|
|
|
}
|
2017-07-04 22:48:38 +02:00
|
|
|
func ExampleEngine_RegisterFilter() {
|
2017-07-02 13:51:24 +02:00
|
|
|
engine := NewEngine()
|
2017-07-02 19:31:34 +02:00
|
|
|
engine.RegisterFilter("has_prefix", strings.HasPrefix)
|
2017-07-02 13:51:24 +02:00
|
|
|
template := `{{ title | has_prefix: "Intro" }}`
|
|
|
|
bindings := map[string]interface{}{
|
|
|
|
"title": "Introduction",
|
|
|
|
}
|
2017-07-03 03:17:04 +02:00
|
|
|
out, err := engine.ParseAndRenderString(template, bindings)
|
2017-07-02 13:51:24 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
fmt.Println(out)
|
|
|
|
// Output: true
|
|
|
|
}
|
2017-07-10 15:38:46 +02:00
|
|
|
func ExampleEngine_RegisterFilter_optional_argument() {
|
|
|
|
engine := NewEngine()
|
|
|
|
// func(a, b int) int) would default the second argument to zero.
|
|
|
|
// Then we can't tell the difference between {{ n | inc }} and
|
|
|
|
// {{ n | inc: 0 }}. A function in the parameter list has a special
|
|
|
|
// meaning as a default parameter.
|
|
|
|
engine.RegisterFilter("inc", func(a int, b func(int) int) int {
|
|
|
|
return a + b(1)
|
|
|
|
})
|
|
|
|
template := `10 + 1 = {{ m | inc }}; 20 + 5 = {{ n | inc: 5 }}`
|
|
|
|
bindings := map[string]interface{}{
|
|
|
|
"m": 10,
|
|
|
|
"n": "20",
|
|
|
|
}
|
|
|
|
out, err := engine.ParseAndRenderString(template, bindings)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
fmt.Println(out)
|
|
|
|
// Output: 10 + 1 = 11; 20 + 5 = 25
|
|
|
|
}
|
2017-07-02 13:51:24 +02:00
|
|
|
|
2017-07-04 22:48:38 +02:00
|
|
|
func ExampleEngine_RegisterTag() {
|
2017-07-02 13:51:24 +02:00
|
|
|
engine := NewEngine()
|
2017-07-04 17:41:45 +02:00
|
|
|
engine.RegisterTag("echo", func(c render.Context) (string, error) {
|
2017-07-04 13:41:17 +02:00
|
|
|
return c.TagArgs(), nil
|
2017-07-02 13:51:24 +02:00
|
|
|
})
|
|
|
|
template := `{% echo hello world %}`
|
2017-07-04 22:48:38 +02:00
|
|
|
out, err := engine.ParseAndRenderString(template, emptyBindings)
|
2017-07-02 13:51:24 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
fmt.Println(out)
|
|
|
|
// Output: hello world
|
|
|
|
}
|
|
|
|
|
2017-07-04 22:48:38 +02:00
|
|
|
func ExampleEngine_RegisterBlock() {
|
2017-07-02 13:51:24 +02:00
|
|
|
engine := NewEngine()
|
2017-07-04 17:41:45 +02:00
|
|
|
engine.RegisterBlock("length", func(c render.Context) (string, error) {
|
2017-07-02 13:51:24 +02:00
|
|
|
s, err := c.InnerString()
|
|
|
|
if err != nil {
|
2017-07-04 13:41:17 +02:00
|
|
|
return "", err
|
2017-07-02 13:51:24 +02:00
|
|
|
}
|
|
|
|
n := len(s)
|
2017-07-04 13:41:17 +02:00
|
|
|
return fmt.Sprint(n), nil
|
2017-07-02 13:51:24 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
template := `{% length %}abc{% endlength %}`
|
2017-07-04 22:48:38 +02:00
|
|
|
out, err := engine.ParseAndRenderString(template, emptyBindings)
|
2017-07-02 13:51:24 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
fmt.Println(out)
|
|
|
|
// Output: 3
|
|
|
|
}
|
2017-07-03 18:00:43 +02:00
|
|
|
|
|
|
|
type redConvertible struct{}
|
|
|
|
|
|
|
|
func (c redConvertible) ToLiquid() interface{} {
|
|
|
|
return "red"
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleDrop() {
|
|
|
|
// type redConvertible struct{}
|
|
|
|
//
|
|
|
|
// func (c redConvertible) ToLiquid() interface{} {
|
|
|
|
// return "red"
|
|
|
|
// }
|
|
|
|
engine := NewEngine()
|
|
|
|
bindings := map[string]interface{}{
|
|
|
|
"drop": redConvertible{},
|
|
|
|
}
|
|
|
|
template := `{{ drop }}`
|
|
|
|
out, err := engine.ParseAndRenderString(template, bindings)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
fmt.Println(out)
|
|
|
|
// Output: red
|
|
|
|
}
|