Skip to content

Commit

Permalink
test(clap_complete): Add test case for multi-values of positional arg…
Browse files Browse the repository at this point in the history
…ument with `num_args(N)`
  • Loading branch information
shannmu committed Jul 31, 2024
1 parent 75a45e5 commit 3f2466b
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions clap_complete/tests/testsuite/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,96 @@ val3
);
}

#[test]
fn suggest_multi_positional() {
let mut cmd = Command::new("dynamic")
.arg(
clap::Arg::new("positional")
.value_parser(["pos_1, pos_2, pos_3"])
.index(1),
)
.arg(
clap::Arg::new("positional-2")
.value_parser(["pos_a", "pos_b", "pos_c"])
.index(2)
.num_args(3),
)
.arg(
clap::Arg::new("--format")
.long("format")
.short('F')
.value_parser(["json", "yaml", "toml"]),
);

assert_data_eq!(
complete!(cmd, "pos_1 pos_a [TAB]"),
snapbox::str![
"--format
--help\tPrint help
-F
-h\tPrint help"
]
);

assert_data_eq!(
complete!(cmd, "pos_1 pos_a pos_b [TAB]"),
snapbox::str![
"--format
--help\tPrint help
-F
-h\tPrint help"
]
);

assert_data_eq!(
complete!(cmd, "--format json pos_1 [TAB]"),
snapbox::str![
"--format
--help\tPrint help
-F
-h\tPrint help
pos_a
pos_b
pos_c"
]
);

assert_data_eq!(
complete!(cmd, "--format json pos_1 pos_a [TAB]"),
snapbox::str![
"--format
--help\tPrint help
-F
-h\tPrint help"
]
);

assert_data_eq!(
complete!(cmd, "--format json pos_1 pos_a pos_b pos_c [TAB]"),
snapbox::str![
"--format
--help\tPrint help
-F
-h\tPrint help"
]
);

assert_data_eq!(
complete!(cmd, "--format json -- pos_1 pos_a [TAB]"),
snapbox::str![""]
);

assert_data_eq!(
complete!(cmd, "--format json -- pos_1 pos_a pos_b [TAB]"),
snapbox::str![""]
);

assert_data_eq!(
complete!(cmd, "--format json -- pos_1 pos_a pos_b pos_c [TAB]"),
snapbox::str![""]
);
}

fn complete(cmd: &mut Command, args: impl AsRef<str>, current_dir: Option<&Path>) -> String {
let input = args.as_ref();
let mut args = vec![std::ffi::OsString::from(cmd.get_name())];
Expand Down

0 comments on commit 3f2466b

Please sign in to comment.