(t *testing.T)
| 187 | } |
| 188 | |
| 189 | func TestManPrintFlagsShowsDefaultValues(t *testing.T) { |
| 190 | type TestOptions struct { |
| 191 | Limit int |
| 192 | Template string |
| 193 | Fork bool |
| 194 | NoArchive bool |
| 195 | Topic []string |
| 196 | } |
| 197 | opts := TestOptions{} |
| 198 | // Int flag should show it |
| 199 | c := &cobra.Command{} |
| 200 | c.Flags().IntVar(&opts.Limit, "limit", 30, "Some limit") |
| 201 | |
| 202 | buf := new(bytes.Buffer) |
| 203 | manPrintFlags(buf, c.Flags()) |
| 204 | |
| 205 | got := buf.String() |
| 206 | expected := "`--limit` `<int> (default 30)`\n: Some limit\n\n" |
| 207 | if got != expected { |
| 208 | t.Errorf("Expected %q, got %q", expected, got) |
| 209 | } |
| 210 | |
| 211 | // Bool flag should hide it if default is false |
| 212 | c = &cobra.Command{} |
| 213 | c.Flags().BoolVar(&opts.Fork, "fork", false, "Show only forks") |
| 214 | |
| 215 | buf = new(bytes.Buffer) |
| 216 | manPrintFlags(buf, c.Flags()) |
| 217 | |
| 218 | got = buf.String() |
| 219 | expected = "`--fork`\n: Show only forks\n\n" |
| 220 | if got != expected { |
| 221 | t.Errorf("Expected %q, got %q", expected, got) |
| 222 | } |
| 223 | |
| 224 | // Bool flag should show it if default is true |
| 225 | c = &cobra.Command{} |
| 226 | c.Flags().BoolVar(&opts.NoArchive, "no-archived", true, "Hide archived") |
| 227 | |
| 228 | buf = new(bytes.Buffer) |
| 229 | manPrintFlags(buf, c.Flags()) |
| 230 | |
| 231 | got = buf.String() |
| 232 | expected = "`--no-archived` `(default true)`\n: Hide archived\n\n" |
| 233 | if got != expected { |
| 234 | t.Errorf("Expected %q, got %q", expected, got) |
| 235 | } |
| 236 | |
| 237 | // String flag should show it if default is not an empty string |
| 238 | c = &cobra.Command{} |
| 239 | c.Flags().StringVar(&opts.Template, "template", "T1", "Some template") |
| 240 | |
| 241 | buf = new(bytes.Buffer) |
| 242 | manPrintFlags(buf, c.Flags()) |
| 243 | |
| 244 | got = buf.String() |
| 245 | expected = "`--template` `<string> (default \"T1\")`\n: Some template\n\n" |
| 246 | if got != expected { |
nothing calls this directly
no test coverage detected