51 lines
2.0 KiB
Go
51 lines
2.0 KiB
Go
package commandmeta
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestReportingFlagsAreBooleanAndReturnedIndependently(t *testing.T) {
|
|
flags := ReportingFlags()
|
|
want := []string{"quiet", "verbose", "debug", "progress"}
|
|
if len(flags) != len(want)+1 {
|
|
t.Fatalf("reporting flags = %#v", flags)
|
|
}
|
|
for index, name := range want {
|
|
if flags[index].Name != name || !flags[index].Boolean || flags[index].Short != "" || flags[index].Default != "false" {
|
|
t.Errorf("reporting flag %d = %#v", index, flags[index])
|
|
}
|
|
if !IsReportingFlag(name) {
|
|
t.Errorf("%q is not recognized as a reporting flag", name)
|
|
}
|
|
}
|
|
if flags[4].Name != "format" || flags[4].Boolean || !IsReportingFlag("format") {
|
|
t.Fatalf("format metadata = %#v", flags[4])
|
|
}
|
|
flags[4].Completion.Choices[0].Value = "changed"
|
|
if ReportingFlags()[4].Completion.Choices[0].Value != "text" {
|
|
t.Fatal("caller mutation changed format choices")
|
|
}
|
|
flags[0].Name = "changed"
|
|
if ReportingFlags()[0].Name != "quiet" {
|
|
t.Fatal("caller mutation changed shared reporting metadata")
|
|
}
|
|
for _, name := range []string{"help", "quietly", ""} {
|
|
if IsReportingFlag(name) {
|
|
t.Errorf("%q was incorrectly recognized as a reporting flag", name)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestEffectiveFlagsCombinesDebugMetadata(t *testing.T) {
|
|
flags := []Flag{{Name: "before"}, {Name: "debug", Short: "d", Boolean: true, Default: "true", Required: true, Usage: "application diagnostics", Completion: CompletionHint{Kind: CompletionFiles}}, {Name: "after"}}
|
|
core := ReportingFlags()
|
|
got := EffectiveFlags(flags, core)
|
|
if len(got) != len(flags)+len(core)-1 || got[1].Name != "debug" || got[1].Default != "false" || got[1].Short != "d" || !got[1].Required || got[1].Completion.Kind != CompletionFiles || !strings.Contains(got[1].Usage, "application diagnostics") || !strings.Contains(got[1].Usage, "application input defaults to true") {
|
|
t.Fatalf("effective debug = %#v", got)
|
|
}
|
|
if flags[1].Default != "true" || core[2].Short != "" {
|
|
t.Fatal("input metadata changed")
|
|
}
|
|
}
|