2017-06-26 16:15:01 +02:00
|
|
|
package liquid
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-06-26 16:36:53 +02:00
|
|
|
"log"
|
2017-06-26 16:15:01 +02:00
|
|
|
"testing"
|
|
|
|
|
2017-06-27 17:39:32 +02:00
|
|
|
"github.com/osteele/liquid/tags"
|
2017-06-26 16:15:01 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2017-06-27 17:39:32 +02:00
|
|
|
func init() {
|
|
|
|
tags.DefineStandardTags()
|
|
|
|
}
|
|
|
|
|
2017-06-26 16:15:01 +02:00
|
|
|
var liquidTests = []struct{ in, expected string }{
|
|
|
|
{"{{page.title}}", "Introduction"},
|
|
|
|
{"{%if x%}true{%endif%}", "true"},
|
|
|
|
}
|
|
|
|
|
|
|
|
var liquidTestScope = map[string]interface{}{
|
|
|
|
"x": 123,
|
|
|
|
"ar": []string{"first", "second", "third"},
|
|
|
|
"page": map[string]interface{}{
|
|
|
|
"title": "Introduction",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestChunkParser(t *testing.T) {
|
|
|
|
engine := NewEngine()
|
|
|
|
for i, test := range liquidTests {
|
|
|
|
t.Run(fmt.Sprint(i), func(t *testing.T) {
|
|
|
|
out, err := engine.ParseAndRenderString(test.in, liquidTestScope)
|
|
|
|
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()
|
|
|
|
template := `<h1>{{page.title}}</h1>`
|
2017-06-29 19:08:25 +02:00
|
|
|
bindings := map[string]interface{}{
|
2017-06-26 16:36:53 +02:00
|
|
|
"page": map[string]interface{}{
|
|
|
|
"title": "Introduction",
|
|
|
|
},
|
|
|
|
}
|
2017-06-29 19:08:25 +02:00
|
|
|
out, err := engine.ParseAndRenderString(template, bindings)
|
2017-06-26 16:36:53 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
fmt.Println(out)
|
|
|
|
// Output: <h1>Introduction</h1>
|
|
|
|
}
|