feat(bonfire): add FlagBool and read every bare switch with it (IN-04, quick-261028-rqd)

This commit is contained in:
Jakub Zych
2026-10-28 20:32:38 +01:00
parent e34d113c80
commit 795c4f6b29
15 changed files with 86 additions and 54 deletions

View File

@@ -115,3 +115,40 @@ func TestHelpShowsLongOrDescription(t *testing.T) {
})
}
}
// TestFlagBool (IN-04): FlagBool reads a bare switch with
// strconv.ParseBool; unset, empty or unparsable values are false, and a
// nil Input has no flags.
func TestFlagBool(t *testing.T) {
cases := []struct {
args []string
want bool
}{
{nil, false},
{[]string{"--x"}, true},
{[]string{"--x=1"}, true},
{[]string{"--x=t"}, true},
{[]string{"--x=TRUE"}, true},
{[]string{"--x=True"}, true},
{[]string{"--x=false"}, false},
{[]string{"--x=0"}, false},
{[]string{"--x="}, false},
{[]string{"--x=yes"}, false},
}
for _, tc := range cases {
var got bool
cmd := Command{Name: "acme:switch", Flags: []Flag{{Name: "x", Bare: true}}, Run: func(_ context.Context, in Input, _ Output) error {
got = FlagBool(in, "x")
return nil
}}
if err := Call(context.Background(), []Command{cmd}, "acme:switch", tc.args, nil); err != nil {
t.Fatalf("%q: %v", tc.args, err)
}
if got != tc.want {
t.Errorf("FlagBool with %q = %v, want %v", tc.args, got, tc.want)
}
}
if FlagBool(nil, "x") {
t.Error("FlagBool(nil) = true")
}
}