|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/RewriteToday/cli/internal/clierr" |
| 7 | +) |
| 8 | + |
| 9 | +func TestResolveOutputFormat(t *testing.T) { |
| 10 | + tests := []struct { |
| 11 | + name string |
| 12 | + args []string |
| 13 | + want string |
| 14 | + }{ |
| 15 | + {name: "default", args: []string{}, want: "text"}, |
| 16 | + {name: "long flag", args: []string{"--output", "json"}, want: "json"}, |
| 17 | + {name: "short flag", args: []string{"-o", "json"}, want: "json"}, |
| 18 | + {name: "equals", args: []string{"--output=json"}, want: "json"}, |
| 19 | + {name: "invalid falls back", args: []string{"--output=xml"}, want: "text"}, |
| 20 | + } |
| 21 | + |
| 22 | + for _, tt := range tests { |
| 23 | + t.Run(tt.name, func(t *testing.T) { |
| 24 | + if got := ResolveOutputFormat(tt.args); got != tt.want { |
| 25 | + t.Fatalf("expected %q, got %q", tt.want, got) |
| 26 | + } |
| 27 | + }) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +func TestNormalizeOutputFormat_InvalidReturnsUsageError(t *testing.T) { |
| 32 | + _, err := normalizeOutputFormat("xml") |
| 33 | + if err == nil { |
| 34 | + t.Fatal("expected an error") |
| 35 | + } |
| 36 | + |
| 37 | + if got := clierr.ExitCode(err); got != int(clierr.CodeUsage) { |
| 38 | + t.Fatalf("expected code %d, got %d", clierr.CodeUsage, got) |
| 39 | + } |
| 40 | +} |
0 commit comments