Skip to content

Commit

Permalink
test(clap_complete): Add test case for hiding subcommands and their a…
Browse files Browse the repository at this point in the history
…liases
  • Loading branch information
shannmu committed Jul 19, 2024
1 parent 9e5a071 commit 6a64c24
Showing 1 changed file with 60 additions and 10 deletions.
70 changes: 60 additions & 10 deletions clap_complete/tests/testsuite/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ fn suggest_subcommand_subset() {
.subcommand(Command::new("hello-moon"))
.subcommand(Command::new("goodbye-world"));

assert_data_eq!(complete!(cmd, "he"), snapbox::str![[r#"
assert_data_eq!(
complete!(cmd, "he"),
snapbox::str![[r#"
hello-moon
hello-world
help Print this message or the help of the given subcommand(s)
"#]],);
"#]],
);
}

#[test]
Expand All @@ -51,6 +54,41 @@ fn suggest_hidden_long_flags() {
)
}

#[test]
fn suggest_hidden_subcommand_and_aliases() {
let mut cmd = Command::new("exhaustive")
.subcommand(
Command::new("hello_world_visible")
.visible_alias("hello_world_visible-alias")
.alias("hello_world_visible-hidden-alias"),
)
.subcommand(
Command::new("hello_world_hidden")
.visible_alias("hello_world_hidden-alias")
.alias("hello_world_hidden-hidden-alias")
.hide(true),
);

assert_data_eq!(
complete!(cmd, "hello_"),
snapbox::str![
"hello_world_hidden
hello_world_hidden-alias
hello_world_visible
hello_world_visible-alias
"
]
);

assert_data_eq!(
complete!(cmd, "hello_world_h"),
snapbox::str![
"hello_world_hidden
hello_world_hidden-alias"
]
);
}

#[test]
fn suggest_subcommand_aliases() {
let mut cmd = Command::new("exhaustive")
Expand Down Expand Up @@ -150,11 +188,14 @@ fn suggest_long_flag_subset() {
.action(clap::ArgAction::Count),
);

assert_data_eq!(complete!(cmd, "--he"), snapbox::str![[r#"
assert_data_eq!(
complete!(cmd, "--he"),
snapbox::str![[r#"
--hello-world
--hello-moon
--help Print help
"#]],);
"#]],
);
}

#[test]
Expand All @@ -166,10 +207,13 @@ fn suggest_possible_value_subset() {
"goodbye-world".into(),
]));

assert_data_eq!(complete!(cmd, "hello"), snapbox::str![[r#"
assert_data_eq!(
complete!(cmd, "hello"),
snapbox::str![[r#"
hello-world Say hello to the world
hello-moon
"#]],);
"#]],
);
}

#[test]
Expand All @@ -191,12 +235,15 @@ fn suggest_additional_short_flags() {
.action(clap::ArgAction::Count),
);

assert_data_eq!(complete!(cmd, "-a"), snapbox::str![[r#"
assert_data_eq!(
complete!(cmd, "-a"),
snapbox::str![[r#"
-aa
-ab
-ac
-ah Print help
"#]],);
"#]],
);
}

#[test]
Expand All @@ -209,13 +256,16 @@ fn suggest_subcommand_positional() {
]),
));

assert_data_eq!(complete!(cmd, "hello-world [TAB]"), snapbox::str![[r#"
assert_data_eq!(
complete!(cmd, "hello-world [TAB]"),
snapbox::str![[r#"
--help Print help (see more with '--help')
-h Print help (see more with '--help')
hello-world Say hello to the world
hello-moon
goodbye-world
"#]],);
"#]],
);
}

fn complete(cmd: &mut Command, args: impl AsRef<str>, current_dir: Option<&Path>) -> String {
Expand Down

0 comments on commit 6a64c24

Please sign in to comment.