github.com/fabiokung/docker@v0.11.2-0.20170222101415-4534dcd49497/pkg/templates/templates_test.go (about) 1 package templates 2 3 import ( 4 "bytes" 5 "testing" 6 7 "github.com/docker/docker/pkg/testutil/assert" 8 ) 9 10 func TestParseStringFunctions(t *testing.T) { 11 tm, err := Parse(`{{join (split . ":") "/"}}`) 12 assert.NilError(t, err) 13 14 var b bytes.Buffer 15 assert.NilError(t, tm.Execute(&b, "text:with:colon")) 16 want := "text/with/colon" 17 assert.Equal(t, b.String(), want) 18 } 19 20 func TestNewParse(t *testing.T) { 21 tm, err := NewParse("foo", "this is a {{ . }}") 22 assert.NilError(t, err) 23 24 var b bytes.Buffer 25 assert.NilError(t, tm.Execute(&b, "string")) 26 want := "this is a string" 27 assert.Equal(t, b.String(), want) 28 } 29 30 func TestParseTruncateFunction(t *testing.T) { 31 source := "tupx5xzf6hvsrhnruz5cr8gwp" 32 33 testCases := []struct { 34 template string 35 expected string 36 }{ 37 { 38 template: `{{truncate . 5}}`, 39 expected: "tupx5", 40 }, 41 { 42 template: `{{truncate . 25}}`, 43 expected: "tupx5xzf6hvsrhnruz5cr8gwp", 44 }, 45 { 46 template: `{{truncate . 30}}`, 47 expected: "tupx5xzf6hvsrhnruz5cr8gwp", 48 }, 49 } 50 51 for _, testCase := range testCases { 52 tm, err := Parse(testCase.template) 53 assert.NilError(t, err) 54 55 var b bytes.Buffer 56 assert.NilError(t, tm.Execute(&b, source)) 57 assert.Equal(t, b.String(), testCase.expected) 58 } 59 }