Skip to content

Commit af21dc2

Browse files
committed
More cases to parse_test.go
1 parent c8e5022 commit af21dc2

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

parse_test.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
// ├── add --dry-run
1818
// └── nested --force
1919
// └── sub --echo
20-
// └── hello --mandatory-flag
20+
// └── hello --mandatory-flag=false --another-mandatory-flag some-value
2121
type testState struct {
2222
add *Command
2323
nested, sub, hello *Command
@@ -47,9 +47,11 @@ func newTestState() testState {
4747
Name: "hello",
4848
Flags: FlagsFunc(func(fset *flag.FlagSet) {
4949
fset.Bool("mandatory-flag", false, "mandatory flag")
50+
fset.String("another-mandatory-flag", "", "another mandatory flag")
5051
}),
5152
FlagsMetadata: []FlagMetadata{
5253
{Name: "mandatory-flag", Required: true},
54+
{Name: "another-mandatory-flag", Required: true},
5355
},
5456
Exec: exec,
5557
}
@@ -308,23 +310,32 @@ func TestParse(t *testing.T) {
308310
require.Error(t, err)
309311
require.ErrorContains(t, err, `subcommand in path "todo nested" has no name`)
310312
})
311-
t.Run("required flag not set", func(t *testing.T) {
313+
t.Run("required flag", func(t *testing.T) {
312314
t.Parallel()
313315
{
314316
s := newTestState()
315317
err := Parse(s.root, []string{"nested", "hello"})
316318
require.Error(t, err)
317-
require.ErrorContains(t, err, `command "todo nested hello": required flag "-mandatory-flag" not set`)
319+
require.ErrorContains(t, err, `command "todo nested hello": required flags "-mandatory-flag, -another-mandatory-flag" not set`)
318320
}
319321
{
320-
// Correct type
322+
// Correct type - true
321323
s := newTestState()
322-
err := Parse(s.root, []string{"nested", "hello", "--mandatory-flag", "true"})
324+
err := Parse(s.root, []string{"nested", "hello", "--mandatory-flag=true", "--another-mandatory-flag", "some-value"})
323325
require.NoError(t, err)
324326
cmd, state := s.root.terminal()
325327
assert.Equal(t, s.hello, cmd)
326328
require.True(t, GetFlag[bool](state, "mandatory-flag"))
327329
}
330+
{
331+
// Correct type - false
332+
s := newTestState()
333+
err := Parse(s.root, []string{"nested", "hello", "--mandatory-flag=false", "--another-mandatory-flag=some-value"})
334+
require.NoError(t, err)
335+
cmd, state := s.root.terminal()
336+
assert.Equal(t, s.hello, cmd)
337+
require.False(t, GetFlag[bool](state, "mandatory-flag"))
338+
}
328339
{
329340
// Incorrect type
330341
s := newTestState()

0 commit comments

Comments
 (0)