2017-08-08 11:26:24 -04:00
|
|
|
package templates
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
|
|
|
|
2018-06-08 12:24:26 -04:00
|
|
|
"gotest.tools/assert"
|
|
|
|
is "gotest.tools/assert/cmp"
|
2017-08-08 11:26:24 -04:00
|
|
|
)
|
|
|
|
|
2017-10-04 13:03:55 -04:00
|
|
|
// GitHub #32120
|
2017-08-08 11:26:24 -04:00
|
|
|
func TestParseJSONFunctions(t *testing.T) {
|
|
|
|
tm, err := Parse(`{{json .Ports}}`)
|
2018-03-06 14:44:13 -05:00
|
|
|
assert.NilError(t, err)
|
2017-08-08 11:26:24 -04:00
|
|
|
|
|
|
|
var b bytes.Buffer
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, tm.Execute(&b, map[string]string{"Ports": "0.0.0.0:2->8/udp"}))
|
2017-08-08 11:26:24 -04:00
|
|
|
want := "\"0.0.0.0:2->8/udp\""
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal(want, b.String()))
|
2017-08-08 11:26:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseStringFunctions(t *testing.T) {
|
|
|
|
tm, err := Parse(`{{join (split . ":") "/"}}`)
|
2018-03-06 14:44:13 -05:00
|
|
|
assert.NilError(t, err)
|
2017-08-08 11:26:24 -04:00
|
|
|
|
|
|
|
var b bytes.Buffer
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, tm.Execute(&b, "text:with:colon"))
|
2017-08-08 11:26:24 -04:00
|
|
|
want := "text/with/colon"
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal(want, b.String()))
|
2017-08-08 11:26:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewParse(t *testing.T) {
|
|
|
|
tm, err := NewParse("foo", "this is a {{ . }}")
|
2018-03-06 14:44:13 -05:00
|
|
|
assert.NilError(t, err)
|
2017-08-08 11:26:24 -04:00
|
|
|
|
|
|
|
var b bytes.Buffer
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, tm.Execute(&b, "string"))
|
2017-08-08 11:26:24 -04:00
|
|
|
want := "this is a string"
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal(want, b.String()))
|
2017-08-08 11:26:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseTruncateFunction(t *testing.T) {
|
|
|
|
source := "tupx5xzf6hvsrhnruz5cr8gwp"
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
template string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
template: `{{truncate . 5}}`,
|
|
|
|
expected: "tupx5",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
template: `{{truncate . 25}}`,
|
|
|
|
expected: "tupx5xzf6hvsrhnruz5cr8gwp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
template: `{{truncate . 30}}`,
|
|
|
|
expected: "tupx5xzf6hvsrhnruz5cr8gwp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
template: `{{pad . 3 3}}`,
|
|
|
|
expected: " tupx5xzf6hvsrhnruz5cr8gwp ",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, testCase := range testCases {
|
2019-10-29 08:35:54 -04:00
|
|
|
testCase := testCase
|
|
|
|
|
2017-08-08 11:26:24 -04:00
|
|
|
tm, err := Parse(testCase.template)
|
2018-03-06 14:44:13 -05:00
|
|
|
assert.NilError(t, err)
|
2017-08-08 11:26:24 -04:00
|
|
|
|
|
|
|
t.Run("Non Empty Source Test with template: "+testCase.template, func(t *testing.T) {
|
|
|
|
var b bytes.Buffer
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, tm.Execute(&b, source))
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal(testCase.expected, b.String()))
|
2017-08-08 11:26:24 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Empty Source Test with template: "+testCase.template, func(t *testing.T) {
|
|
|
|
var c bytes.Buffer
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, tm.Execute(&c, ""))
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal("", c.String()))
|
2017-08-08 11:26:24 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Nil Source Test with template: "+testCase.template, func(t *testing.T) {
|
|
|
|
var c bytes.Buffer
|
2017-12-21 16:27:57 -05:00
|
|
|
assert.Check(t, tm.Execute(&c, nil) != nil)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal("", c.String()))
|
2017-08-08 11:26:24 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|