zk/adapter/handlebars/handlebars_test.go

95 lines
2.5 KiB
Go
Raw Normal View History

2020-12-26 15:53:04 +00:00
package handlebars
import (
2020-12-27 17:09:45 +00:00
"fmt"
2020-12-26 15:53:04 +00:00
"testing"
2020-12-27 17:09:45 +00:00
"time"
2020-12-26 15:53:04 +00:00
2020-12-27 17:58:22 +00:00
"github.com/mickael-menu/zk/util"
2020-12-26 15:53:04 +00:00
"github.com/mickael-menu/zk/util/assert"
2020-12-27 17:09:45 +00:00
"github.com/mickael-menu/zk/util/date"
2020-12-26 15:53:04 +00:00
"github.com/mickael-menu/zk/util/fixtures"
)
2020-12-26 19:10:39 +00:00
func init() {
2020-12-27 17:09:45 +00:00
date := date.NewFrozen(time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC))
2020-12-27 17:58:22 +00:00
Init("en", &util.NullLogger, &date)
2020-12-26 19:10:39 +00:00
}
2020-12-26 15:53:04 +00:00
func TestRenderString(t *testing.T) {
sut := NewRenderer()
res, err := sut.Render("Goodbye, {{name}}", map[string]string{"name": "Ed"})
assert.Nil(t, err)
assert.Equal(t, res, "Goodbye, Ed")
}
func TestRenderFile(t *testing.T) {
sut := NewRenderer()
res, err := sut.RenderFile(fixtures.Path("template.txt"), map[string]string{"name": "Thom"})
assert.Nil(t, err)
assert.Equal(t, res, "Hello, Thom\n")
}
func TestUnknownVariable(t *testing.T) {
sut := NewRenderer()
res, err := sut.Render("Hi, {{unknown}}!", nil)
assert.Nil(t, err)
assert.Equal(t, res, "Hi, !")
}
func TestDoesntEscapeHTML(t *testing.T) {
sut := NewRenderer()
2020-12-26 19:10:39 +00:00
res, err := sut.Render("Salut, {{name}}!", map[string]string{"name": "l'ami"})
2020-12-26 15:53:04 +00:00
assert.Nil(t, err)
2020-12-26 19:10:39 +00:00
assert.Equal(t, res, "Salut, l'ami!")
2020-12-26 15:53:04 +00:00
res, err = sut.RenderFile(fixtures.Path("unescape.txt"), map[string]string{"name": "l'ami"})
assert.Nil(t, err)
2020-12-26 19:10:39 +00:00
assert.Equal(t, res, "Salut, l'ami!\n")
}
2020-12-27 17:09:45 +00:00
func TestSlugHelper(t *testing.T) {
2020-12-26 19:10:39 +00:00
sut := NewRenderer()
2020-12-27 17:09:45 +00:00
// block
2020-12-26 19:10:39 +00:00
res, err := sut.Render("{{#slug}}This will be slugified!{{/slug}}", nil)
assert.Nil(t, err)
assert.Equal(t, res, "this-will-be-slugified")
2020-12-27 17:09:45 +00:00
// inline
2020-12-26 19:10:39 +00:00
res, err = sut.Render(`{{slug "This will be slugified!"}}`, nil)
assert.Nil(t, err)
assert.Equal(t, res, "this-will-be-slugified")
2020-12-26 15:53:04 +00:00
}
2020-12-27 17:09:45 +00:00
func TestDateHelper(t *testing.T) {
sut := NewRenderer()
test := func(format string, expected string) {
res, err := sut.Render(fmt.Sprintf("{{date '%s'}}", format), nil)
assert.Nil(t, err)
assert.Equal(t, res, expected)
}
test("short", "11/17/2009")
test("medium", "Nov 17, 2009")
test("long", "November 17, 2009")
test("full", "Tuesday, November 17, 2009")
test("year", "2009")
test("time", "20:34")
test("timestamp", "200911172034")
test("timestamp-unix", "1258490098")
test("cust: %Y-%m", "cust: 2009-11")
}
2020-12-27 17:58:22 +00:00
func TestShellHelper(t *testing.T) {
sut := NewRenderer()
// block is passed as piped input
res, err := sut.Render(`{{#sh "tr '[a-z]' '[A-Z]'"}}Hello, world!{{/sh}}`, nil)
assert.Nil(t, err)
assert.Equal(t, res, "HELLO, WORLD!")
// inline
res, err = sut.Render(`{{sh "echo 'Hello, world!'"}}`, nil)
assert.Nil(t, err)
assert.Equal(t, res, "Hello, world!\n")
}