github.com/panekj/cli@v0.0.0-20230304125325-467dd2f3797e/cli/command/stack/formatter/formatter_test.go (about)

     1  package formatter
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	"github.com/docker/cli/cli/command/formatter"
     8  	"gotest.tools/v3/assert"
     9  )
    10  
    11  func TestStackContextWrite(t *testing.T) {
    12  	cases := []struct {
    13  		context  formatter.Context
    14  		expected string
    15  	}{
    16  		// Errors
    17  		{
    18  			formatter.Context{Format: "{{InvalidFunction}}"},
    19  			`template parsing error: template: :1: function "InvalidFunction" not defined`,
    20  		},
    21  		{
    22  			formatter.Context{Format: "{{nil}}"},
    23  			`template parsing error: template: :1:2: executing "" at <nil>: nil is not a command`,
    24  		},
    25  		// Table format
    26  		{
    27  			formatter.Context{Format: SwarmStackTableFormat},
    28  			`NAME      SERVICES
    29  baz       2
    30  bar       1
    31  `,
    32  		},
    33  		{
    34  			formatter.Context{Format: formatter.Format("table {{.Name}}")},
    35  			`NAME
    36  baz
    37  bar
    38  `,
    39  		},
    40  		// Custom Format
    41  		{
    42  			formatter.Context{Format: formatter.Format("{{.Name}}")},
    43  			`baz
    44  bar
    45  `,
    46  		},
    47  	}
    48  
    49  	stacks := []*Stack{
    50  		{Name: "baz", Services: 2},
    51  		{Name: "bar", Services: 1},
    52  	}
    53  	for _, tc := range cases {
    54  		tc := tc
    55  		t.Run(string(tc.context.Format), func(t *testing.T) {
    56  			var out bytes.Buffer
    57  			tc.context.Output = &out
    58  
    59  			if err := StackWrite(tc.context, stacks); err != nil {
    60  				assert.Error(t, err, tc.expected)
    61  			} else {
    62  				assert.Equal(t, out.String(), tc.expected)
    63  			}
    64  		})
    65  	}
    66  }