2020-12-26 15:53:04 +00:00
|
|
|
package handlebars
|
|
|
|
|
|
|
|
import (
|
2021-01-09 17:54:50 +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
|
|
|
|
2021-01-09 18:48:29 +00:00
|
|
|
"github.com/mickael-menu/zk/core/style"
|
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"
|
|
|
|
"github.com/mickael-menu/zk/util/fixtures"
|
|
|
|
)
|
|
|
|
|
2020-12-26 19:10:39 +00:00
|
|
|
func init() {
|
2021-01-09 17:54:50 +00:00
|
|
|
Init("en", &util.NullLogger, &styler{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// styler is a test double for core.Styler
|
|
|
|
// "hello", "red" -> "red(hello)"
|
|
|
|
type styler struct{}
|
|
|
|
|
2021-01-09 18:48:29 +00:00
|
|
|
func (s *styler) Style(text string, rules ...style.Rule) (string, error) {
|
2021-01-09 17:54:50 +00:00
|
|
|
for _, rule := range rules {
|
|
|
|
text = fmt.Sprintf("%s(%s)", rule, text)
|
|
|
|
}
|
|
|
|
return text, nil
|
2020-12-26 19:10:39 +00:00
|
|
|
}
|
|
|
|
|
2020-12-29 16:22:19 +00:00
|
|
|
func testString(t *testing.T, template string, context interface{}, expected string) {
|
|
|
|
sut := NewLoader()
|
|
|
|
|
|
|
|
templ, err := sut.Load(template)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
actual, err := templ.Render(context)
|
2020-12-26 15:53:04 +00:00
|
|
|
assert.Nil(t, err)
|
2020-12-29 16:22:19 +00:00
|
|
|
assert.Equal(t, actual, expected)
|
2020-12-26 15:53:04 +00:00
|
|
|
}
|
|
|
|
|
2020-12-29 16:22:19 +00:00
|
|
|
func testFile(t *testing.T, name string, context interface{}, expected string) {
|
|
|
|
sut := NewLoader()
|
|
|
|
|
|
|
|
templ, err := sut.LoadFile(fixtures.Path(name))
|
2020-12-26 15:53:04 +00:00
|
|
|
assert.Nil(t, err)
|
2020-12-29 16:22:19 +00:00
|
|
|
|
|
|
|
actual, err := templ.Render(context)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, actual, expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRenderString(t *testing.T) {
|
|
|
|
testString(t,
|
|
|
|
"Goodbye, {{name}}",
|
|
|
|
map[string]string{"name": "Ed"},
|
|
|
|
"Goodbye, Ed",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRenderFile(t *testing.T) {
|
|
|
|
testFile(t,
|
|
|
|
"template.txt",
|
|
|
|
map[string]string{"name": "Thom"},
|
|
|
|
"Hello, Thom\n",
|
|
|
|
)
|
2020-12-26 15:53:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnknownVariable(t *testing.T) {
|
2020-12-29 16:22:19 +00:00
|
|
|
testString(t,
|
|
|
|
"Hi, {{unknown}}!",
|
|
|
|
nil,
|
|
|
|
"Hi, !",
|
|
|
|
)
|
2020-12-26 15:53:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDoesntEscapeHTML(t *testing.T) {
|
2020-12-29 16:22:19 +00:00
|
|
|
testString(t,
|
|
|
|
"Salut, {{name}}!",
|
|
|
|
map[string]string{"name": "l'ami"},
|
|
|
|
"Salut, l'ami!",
|
|
|
|
)
|
2020-12-26 15:53:04 +00:00
|
|
|
|
2020-12-29 16:22:19 +00:00
|
|
|
testFile(t,
|
|
|
|
"unescape.txt",
|
|
|
|
map[string]string{"name": "l'ami"},
|
|
|
|
"Salut, l'ami!\n",
|
|
|
|
)
|
2020-12-26 19:10:39 +00:00
|
|
|
}
|
|
|
|
|
2021-01-06 20:43:58 +00:00
|
|
|
func TestPrependHelper(t *testing.T) {
|
|
|
|
// inline
|
|
|
|
testString(t, "{{prepend '> ' 'A quote'}}", nil, "> A quote")
|
|
|
|
|
2020-12-27 17:09:45 +00:00
|
|
|
// block
|
2021-01-06 20:43:58 +00:00
|
|
|
testString(t, "{{#prepend '> '}}A quote{{/prepend}}", nil, "> A quote")
|
|
|
|
testString(t, "{{#prepend '> '}}A quote on\nseveral lines{{/prepend}}", nil, "> A quote on\n> several lines")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSlugHelper(t *testing.T) {
|
|
|
|
// inline
|
2020-12-29 16:22:19 +00:00
|
|
|
testString(t,
|
2021-01-06 20:43:58 +00:00
|
|
|
`{{slug "This will be slugified!"}}`,
|
2020-12-29 16:22:19 +00:00
|
|
|
nil,
|
|
|
|
"this-will-be-slugified",
|
|
|
|
)
|
2021-01-06 20:43:58 +00:00
|
|
|
// block
|
2020-12-29 16:22:19 +00:00
|
|
|
testString(t,
|
2021-01-06 20:43:58 +00:00
|
|
|
"{{#slug}}This will be slugified!{{/slug}}",
|
2020-12-29 16:22:19 +00:00
|
|
|
nil,
|
|
|
|
"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) {
|
2021-01-06 20:19:43 +00:00
|
|
|
context := map[string]interface{}{"now": time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC)}
|
|
|
|
testString(t, "{{date now}}", context, "2009-11-17")
|
|
|
|
testString(t, "{{date now 'short'}}", context, "11/17/2009")
|
|
|
|
testString(t, "{{date now 'medium'}}", context, "Nov 17, 2009")
|
|
|
|
testString(t, "{{date now 'long'}}", context, "November 17, 2009")
|
|
|
|
testString(t, "{{date now 'full'}}", context, "Tuesday, November 17, 2009")
|
|
|
|
testString(t, "{{date now 'year'}}", context, "2009")
|
|
|
|
testString(t, "{{date now 'time'}}", context, "20:34")
|
|
|
|
testString(t, "{{date now 'timestamp'}}", context, "200911172034")
|
|
|
|
testString(t, "{{date now 'timestamp-unix'}}", context, "1258490098")
|
|
|
|
testString(t, "{{date now 'cust: %Y-%m'}}", context, "cust: 2009-11")
|
2021-01-07 21:14:27 +00:00
|
|
|
testString(t, "{{date now 'elapsed'}}", context, "12 years ago")
|
2020-12-27 17:09:45 +00:00
|
|
|
}
|
2020-12-27 17:58:22 +00:00
|
|
|
|
|
|
|
func TestShellHelper(t *testing.T) {
|
|
|
|
// block is passed as piped input
|
2020-12-29 16:22:19 +00:00
|
|
|
testString(t,
|
|
|
|
`{{#sh "tr '[a-z]' '[A-Z]'"}}Hello, world!{{/sh}}`,
|
|
|
|
nil,
|
|
|
|
"HELLO, WORLD!",
|
|
|
|
)
|
2020-12-27 17:58:22 +00:00
|
|
|
// inline
|
2020-12-29 16:22:19 +00:00
|
|
|
testString(t,
|
|
|
|
`{{sh "echo 'Hello, world!'"}}`,
|
|
|
|
nil,
|
|
|
|
"Hello, world!\n",
|
|
|
|
)
|
2020-12-27 17:58:22 +00:00
|
|
|
}
|
2021-01-09 17:54:50 +00:00
|
|
|
|
|
|
|
func TestStyleHelper(t *testing.T) {
|
|
|
|
// inline
|
|
|
|
testString(t, "{{style 'single' 'Some text'}}", nil, "single(Some text)")
|
|
|
|
testString(t, "{{style 'red bold' 'Another text'}}", nil, "bold(red(Another text))")
|
|
|
|
|
|
|
|
// block
|
|
|
|
testString(t, "{{#style 'single'}}A multiline\ntext{{/style}}", nil, "single(A multiline\ntext)")
|
|
|
|
}
|