2016-03-04 12:29:44 -05:00
|
|
|
package templates
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
2017-01-26 15:49:40 -05:00
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/testutil/assert"
|
2016-03-04 12:29:44 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestParseStringFunctions(t *testing.T) {
|
|
|
|
tm, err := Parse(`{{join (split . ":") "/"}}`)
|
2017-01-26 15:49:40 -05:00
|
|
|
assert.NilError(t, err)
|
2016-03-04 12:29:44 -05:00
|
|
|
|
|
|
|
var b bytes.Buffer
|
2017-01-26 15:49:40 -05:00
|
|
|
assert.NilError(t, tm.Execute(&b, "text:with:colon"))
|
2016-03-04 12:29:44 -05:00
|
|
|
want := "text/with/colon"
|
2017-01-26 15:49:40 -05:00
|
|
|
assert.Equal(t, b.String(), want)
|
2016-03-04 12:29:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewParse(t *testing.T) {
|
|
|
|
tm, err := NewParse("foo", "this is a {{ . }}")
|
2017-01-26 15:49:40 -05:00
|
|
|
assert.NilError(t, err)
|
2016-03-04 12:29:44 -05:00
|
|
|
|
|
|
|
var b bytes.Buffer
|
2017-01-26 15:49:40 -05:00
|
|
|
assert.NilError(t, tm.Execute(&b, "string"))
|
2016-03-04 12:29:44 -05:00
|
|
|
want := "this is a string"
|
2017-01-26 15:49:40 -05:00
|
|
|
assert.Equal(t, b.String(), want)
|
|
|
|
}
|
|
|
|
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, testCase := range testCases {
|
|
|
|
tm, err := Parse(testCase.template)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
assert.NilError(t, tm.Execute(&b, source))
|
|
|
|
assert.Equal(t, b.String(), testCase.expected)
|
2016-03-04 12:29:44 -05:00
|
|
|
}
|
|
|
|
}
|