github.com/jiasir/docker@v1.3.3-0.20170609024000-252e610103e7/pkg/templates/templates_test.go (about) 1 package templates 2 3 import ( 4 "bytes" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 ) 9 10 // Github #32120 11 func TestParseJSONFunctions(t *testing.T) { 12 tm, err := Parse(`{{json .Ports}}`) 13 assert.NoError(t, err) 14 15 var b bytes.Buffer 16 assert.NoError(t, tm.Execute(&b, map[string]string{"Ports": "0.0.0.0:2->8/udp"})) 17 want := "\"0.0.0.0:2->8/udp\"" 18 assert.Equal(t, want, b.String()) 19 } 20 21 func TestParseStringFunctions(t *testing.T) { 22 tm, err := Parse(`{{join (split . ":") "/"}}`) 23 assert.NoError(t, err) 24 25 var b bytes.Buffer 26 assert.NoError(t, tm.Execute(&b, "text:with:colon")) 27 want := "text/with/colon" 28 assert.Equal(t, want, b.String()) 29 } 30 31 func TestNewParse(t *testing.T) { 32 tm, err := NewParse("foo", "this is a {{ . }}") 33 assert.NoError(t, err) 34 35 var b bytes.Buffer 36 assert.NoError(t, tm.Execute(&b, "string")) 37 want := "this is a string" 38 assert.Equal(t, want, b.String()) 39 } 40 41 func TestParseTruncateFunction(t *testing.T) { 42 source := "tupx5xzf6hvsrhnruz5cr8gwp" 43 44 testCases := []struct { 45 template string 46 expected string 47 }{ 48 { 49 template: `{{truncate . 5}}`, 50 expected: "tupx5", 51 }, 52 { 53 template: `{{truncate . 25}}`, 54 expected: "tupx5xzf6hvsrhnruz5cr8gwp", 55 }, 56 { 57 template: `{{truncate . 30}}`, 58 expected: "tupx5xzf6hvsrhnruz5cr8gwp", 59 }, 60 } 61 62 for _, testCase := range testCases { 63 tm, err := Parse(testCase.template) 64 assert.NoError(t, err) 65 66 var b bytes.Buffer 67 assert.NoError(t, tm.Execute(&b, source)) 68 assert.Equal(t, testCase.expected, b.String()) 69 } 70 }