Skip to content

Commit 44929b6

Browse files
committed
test: Add tests for subcommand headings
- add tests for subcommand headings feature - include tests for single and multiple help headers - add test for hiding commands header - add test for mixed standard and custom headers
1 parent bb0b2f1 commit 44929b6

File tree

1 file changed

+181
-0
lines changed

1 file changed

+181
-0
lines changed

tests/builder/subcommands.rs

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,3 +635,184 @@ fn duplicate_subcommand_alias() {
635635
.subcommand(Command::new("unique").alias("repeat"))
636636
.build();
637637
}
638+
639+
#[test]
640+
fn test_help_header() {
641+
static VISIBLE_ALIAS_HELP: &str = "\
642+
Usage: clap-test [COMMAND]
643+
644+
Commands:
645+
help Print this message or the help of the given subcommand(s)
646+
647+
Test commands:
648+
test Some help
649+
650+
Options:
651+
-h, --help Print help
652+
-V, --version Print version
653+
";
654+
655+
let cmd = Command::new("clap-test")
656+
.version("2.6")
657+
.subcommand(
658+
Command::new("test")
659+
.about("Some help")
660+
.help_heading("Test commands"),
661+
);
662+
utils::assert_output(cmd, "clap-test --help", VISIBLE_ALIAS_HELP, false);
663+
}
664+
665+
#[test]
666+
fn test_help_header_multiple_help_headers() {
667+
static VISIBLE_ALIAS_HELP: &str = "\
668+
Usage: clap-test [COMMAND]
669+
670+
Commands:
671+
help Print this message or the help of the given subcommand(s)
672+
673+
Test Commands 1:
674+
test1 Some help
675+
676+
Test Commands 2:
677+
test2 Some help
678+
679+
Options:
680+
-h, --help Print help
681+
-V, --version Print version
682+
";
683+
684+
let cmd = Command::new("clap-test")
685+
.version("2.6")
686+
.subcommand(
687+
Command::new("test1")
688+
.about("Some help")
689+
.help_heading("Test Commands 1"),
690+
)
691+
.subcommand(
692+
Command::new("test2")
693+
.about("Some help")
694+
.help_heading("Test Commands 2"),
695+
);
696+
697+
utils::assert_output(cmd, "clap-test --help", VISIBLE_ALIAS_HELP, false);
698+
}
699+
700+
#[test]
701+
fn test_help_header_hide_commands_header() {
702+
static VISIBLE_ALIAS_HELP: &str = "\
703+
Usage: clap-test [COMMAND]
704+
705+
Test commands:
706+
test Some help
707+
708+
Options:
709+
-h, --help Print help
710+
-V, --version Print version
711+
";
712+
713+
let cmd = Command::new("clap-test")
714+
.version("2.6")
715+
.disable_help_subcommand(true)
716+
.subcommand_help_heading("Test commands")
717+
.subcommand(Command::new("test").about("Some help"));
718+
utils::assert_output(cmd, "clap-test --help", VISIBLE_ALIAS_HELP, false);
719+
}
720+
721+
#[test]
722+
fn test_multiple_commands_mixed_standard_and_custom_headers() {
723+
static VISIBLE_ALIAS_HELP: &str = "\
724+
Usage: clap-test [COMMAND]
725+
726+
Help Section:
727+
help Print this message or the help of the given subcommand(s)
728+
729+
Commands:
730+
def_cmd1 First command under default command heading
731+
def_cmd2 Second command under default command heading
732+
def_cmd3 Third command under default command heading
733+
def_cmd4 Fourth command under default command heading
734+
735+
First Custom:
736+
ch1_cmd1 First command under first custom command heading
737+
ch1_cmd2 Second command under first custom command heading
738+
ch1_cmd3 Third command under first custom command heading
739+
ch1_cmd4 Fourth command under first custom command heading
740+
741+
Second Custom:
742+
ch2_cmd1 First command under second custom command heading
743+
ch2_cmd2 Second command under second custom command heading
744+
ch2_cmd3 Third command under second custom command heading
745+
ch2_cmd4 Fourth command under second custom command heading
746+
747+
Options:
748+
-h, --help Print help
749+
-V, --version Print version
750+
";
751+
752+
let cmd = Command::new("clap-test")
753+
.version("2.6")
754+
.subcommand_help_heading("Help Section")
755+
.subcommand(
756+
Command::new("def_cmd1")
757+
.about("First command under default command heading")
758+
.help_heading("Commands"),
759+
)
760+
.subcommand(
761+
Command::new("def_cmd2")
762+
.about("Second command under default command heading")
763+
.help_heading("Commands"),
764+
)
765+
.subcommand(
766+
Command::new("def_cmd3")
767+
.about("Third command under default command heading")
768+
.help_heading("Commands"),
769+
)
770+
.subcommand(
771+
Command::new("def_cmd4")
772+
.about("Fourth command under default command heading")
773+
.help_heading("Commands"),
774+
)
775+
.subcommand(
776+
Command::new("ch1_cmd1")
777+
.about("First command under first custom command heading")
778+
.help_heading("First Custom"),
779+
)
780+
.subcommand(
781+
Command::new("ch1_cmd2")
782+
.about("Second command under first custom command heading")
783+
.help_heading("First Custom"),
784+
)
785+
.subcommand(
786+
Command::new("ch1_cmd3")
787+
.about("Third command under first custom command heading")
788+
.help_heading("First Custom"),
789+
)
790+
.subcommand(
791+
Command::new("ch1_cmd4")
792+
.about("Fourth command under first custom command heading")
793+
.help_heading("First Custom"),
794+
)
795+
.subcommand(
796+
Command::new("ch2_cmd1")
797+
.about("First command under second custom command heading")
798+
.help_heading("Second Custom"),
799+
)
800+
.subcommand(
801+
Command::new("ch2_cmd2")
802+
.about("Second command under second custom command heading")
803+
.help_heading("Second Custom"),
804+
)
805+
.subcommand(
806+
Command::new("ch2_cmd3")
807+
.about("Third command under second custom command heading")
808+
.help_heading("Second Custom"),
809+
)
810+
.subcommand(
811+
Command::new("ch2_cmd4")
812+
.about("Fourth command under second custom command heading")
813+
.help_heading("Second Custom"),
814+
);
815+
816+
utils::assert_output(cmd, "clap-test --help", VISIBLE_ALIAS_HELP, false);
817+
}
818+

0 commit comments

Comments
 (0)