@@ -9,53 +9,57 @@ import (
99)
1010
1111func TestStackContextWrite (t * testing.T ) {
12- cases := []struct {
13- context formatter.Context
12+ tests := []struct {
13+ name string
14+ format formatter.Format
1415 expected string
1516 }{
16- // Errors
1717 {
18- formatter.Context {Format : "{{InvalidFunction}}" },
19- `template parsing error: template: :1: function "InvalidFunction" not defined` ,
18+ name : "invalid function" ,
19+ format : `{{InvalidFunction}}` ,
20+ expected : `template parsing error: template: :1: function "InvalidFunction" not defined` ,
2021 },
2122 {
22- formatter.Context {Format : "{{nil}}" },
23- `template parsing error: template: :1:2: executing "" at <nil>: nil is not a command` ,
23+ name : "invalid placeholder" ,
24+ format : `{{nil}}` ,
25+ expected : `template parsing error: template: :1:2: executing "" at <nil>: nil is not a command` ,
2426 },
25- // Table format
2627 {
27- formatter.Context {Format : SwarmStackTableFormat },
28- `NAME SERVICES
28+ name : "table format" ,
29+ format : SwarmStackTableFormat ,
30+ expected : `NAME SERVICES
2931baz 2
3032bar 1
3133` ,
3234 },
3335 {
34- formatter.Context {Format : formatter .Format ("table {{.Name}}" )},
35- `NAME
36+ name : "custom table format" ,
37+ format : `table {{.Name}}` ,
38+ expected : `NAME
3639baz
3740bar
3841` ,
3942 },
40- // Custom Format
4143 {
42- formatter.Context {Format : formatter .Format ("{{.Name}}" )},
43- `baz
44+ name : "custom format" ,
45+ format : `{{.Name}}` ,
46+ expected : `baz
4447bar
4548` ,
4649 },
4750 }
4851
49- stacks := []* Stack {
50- {Name : "baz" , Services : 2 },
51- {Name : "bar" , Services : 1 },
52- }
53- for _ , tc := range cases {
54- t .Run (string (tc .context .Format ), func (t * testing.T ) {
52+ for _ , tc := range tests {
53+ t .Run (tc .name , func (t * testing.T ) {
5554 var out bytes.Buffer
56- tc .context .Output = & out
57-
58- if err := StackWrite (tc .context , stacks ); err != nil {
55+ fmtCtx := formatter.Context {
56+ Format : tc .format ,
57+ Output : & out ,
58+ }
59+ if err := StackWrite (fmtCtx , []Stack {
60+ {Name : "baz" , Services : 2 },
61+ {Name : "bar" , Services : 1 },
62+ }); err != nil {
5963 assert .Error (t , err , tc .expected )
6064 } else {
6165 assert .Equal (t , out .String (), tc .expected )
0 commit comments