mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
5766317e33
Signed-off-by: Boaz Shuster <ripcurld.github@gmail.com>
70 lines
1.5 KiB
Go
70 lines
1.5 KiB
Go
package templates
|
|
|
|
import (
|
|
"bytes"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
// Github #32120
|
|
func TestParseJSONFunctions(t *testing.T) {
|
|
tm, err := Parse(`{{json .Ports}}`)
|
|
assert.NoError(t, err)
|
|
|
|
var b bytes.Buffer
|
|
assert.NoError(t, tm.Execute(&b, map[string]string{"Ports": "0.0.0.0:2->8/udp"}))
|
|
want := "\"0.0.0.0:2->8/udp\""
|
|
assert.Equal(t, want, b.String())
|
|
}
|
|
|
|
func TestParseStringFunctions(t *testing.T) {
|
|
tm, err := Parse(`{{join (split . ":") "/"}}`)
|
|
assert.NoError(t, err)
|
|
|
|
var b bytes.Buffer
|
|
assert.NoError(t, tm.Execute(&b, "text:with:colon"))
|
|
want := "text/with/colon"
|
|
assert.Equal(t, want, b.String())
|
|
}
|
|
|
|
func TestNewParse(t *testing.T) {
|
|
tm, err := NewParse("foo", "this is a {{ . }}")
|
|
assert.NoError(t, err)
|
|
|
|
var b bytes.Buffer
|
|
assert.NoError(t, tm.Execute(&b, "string"))
|
|
want := "this is a string"
|
|
assert.Equal(t, want, b.String())
|
|
}
|
|
|
|
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.NoError(t, err)
|
|
|
|
var b bytes.Buffer
|
|
assert.NoError(t, tm.Execute(&b, source))
|
|
assert.Equal(t, testCase.expected, b.String())
|
|
}
|
|
}
|