TestAddJSONFlagsSetsAnnotations asserts that `AddJSONFlags` function adds the appropriate annotation to the command, which could later be used by doc generator functions.
(t *testing.T)
| 161 | // appropriate annotation to the command, which could later be used by doc |
| 162 | // generator functions. |
| 163 | func TestAddJSONFlagsSetsAnnotations(t *testing.T) { |
| 164 | tests := []struct { |
| 165 | name string |
| 166 | cmd *cobra.Command |
| 167 | jsonFields []string |
| 168 | expectedAnnotations map[string]string |
| 169 | }{ |
| 170 | { |
| 171 | name: "empty set of fields", |
| 172 | cmd: &cobra.Command{}, |
| 173 | jsonFields: []string{}, |
| 174 | expectedAnnotations: nil, |
| 175 | }, |
| 176 | { |
| 177 | name: "empty set of fields, with existing annotations", |
| 178 | cmd: &cobra.Command{Annotations: map[string]string{"foo": "bar"}}, |
| 179 | jsonFields: []string{}, |
| 180 | expectedAnnotations: map[string]string{"foo": "bar"}, |
| 181 | }, |
| 182 | { |
| 183 | name: "no other annotations", |
| 184 | cmd: &cobra.Command{}, |
| 185 | jsonFields: []string{"few", "json", "fields"}, |
| 186 | expectedAnnotations: map[string]string{ |
| 187 | "help:json-fields": "few,json,fields", |
| 188 | }, |
| 189 | }, |
| 190 | { |
| 191 | name: "with existing annotations (ensure no overwrite)", |
| 192 | cmd: &cobra.Command{Annotations: map[string]string{"foo": "bar"}}, |
| 193 | jsonFields: []string{"few", "json", "fields"}, |
| 194 | expectedAnnotations: map[string]string{ |
| 195 | "foo": "bar", |
| 196 | "help:json-fields": "few,json,fields", |
| 197 | }, |
| 198 | }, |
| 199 | } |
| 200 | |
| 201 | for _, tt := range tests { |
| 202 | t.Run(tt.name, func(t *testing.T) { |
| 203 | AddJSONFlags(tt.cmd, nil, tt.jsonFields) |
| 204 | assert.Equal(t, tt.expectedAnnotations, tt.cmd.Annotations) |
| 205 | }) |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | func TestAddFormatFlags(t *testing.T) { |
| 210 | tests := []struct { |
nothing calls this directly
no test coverage detected