From 5d8c84b83e9e5850d1e34b4bac516c1627c55691 Mon Sep 17 00:00:00 2001 From: shannmu Date: Wed, 24 Jul 2024 19:45:46 +0800 Subject: [PATCH] test(clap_complete): Add test case for multi-values comlpetion after flags --- clap_complete/tests/testsuite/dynamic.rs | 151 +++++++++++++++++++++++ 1 file changed, 151 insertions(+) diff --git a/clap_complete/tests/testsuite/dynamic.rs b/clap_complete/tests/testsuite/dynamic.rs index 0264aea38ab..4b5da1b32c6 100644 --- a/clap_complete/tests/testsuite/dynamic.rs +++ b/clap_complete/tests/testsuite/dynamic.rs @@ -439,6 +439,157 @@ pos_c" ); } +#[test] +fn suggest_argument_multi_values() { + let mut cmd = Command::new("dynamic") + .arg( + clap::Arg::new("certain-num") + .long("certain-num") + .short('Y') + .value_parser(["val1", "val2", "val3"]) + .num_args(3), + ) + .arg( + clap::Arg::new("uncertain-num") + .long("uncertain-num") + .short('N') + .value_parser(["val1", "val2", "val3"]) + .num_args(1..=3), + ); + + assert_data_eq!( + complete!(cmd, "--certain-num [TAB]"), + snapbox::str![ + "val1 +val2 +val3" + ] + ); + + assert_data_eq!( + complete!(cmd, "--certain-num val1 [TAB]"), + snapbox::str![ + "--certain-num +--uncertain-num +--help\tPrint help +-Y +-N +-h\tPrint help" + ] + ); + + assert_data_eq!( + complete!(cmd, "--certain-num val1 val2 val3 [TAB]"), + snapbox::str![ + "--certain-num +--uncertain-num +--help\tPrint help +-Y +-N +-h\tPrint help" + ] + ); + + assert_data_eq!( + complete!(cmd, "--uncertain-num [TAB]"), + snapbox::str![ + "val1 +val2 +val3" + ] + ); + + assert_data_eq!( + complete!(cmd, "--uncertain-num val1 [TAB]"), + snapbox::str![ + "--certain-num +--uncertain-num +--help\tPrint help +-Y +-N +-h\tPrint help" + ] + ); + + assert_data_eq!( + complete!(cmd, "--uncertain-num val1 val2 val3 [TAB]"), + snapbox::str![ + "--certain-num +--uncertain-num +--help\tPrint help +-Y +-N +-h\tPrint help" + ] + ); + + assert_data_eq!( + complete!(cmd, "-Y [TAB]"), + snapbox::str![ + "val1 +val2 +val3" + ] + ); + + assert_data_eq!( + complete!(cmd, "-Y val1 [TAB]"), + snapbox::str![ + "--certain-num +--uncertain-num +--help\tPrint help +-Y +-N +-h\tPrint help" + ] + ); + + assert_data_eq!( + complete!(cmd, "-Y val1 val2 val3 [TAB]"), + snapbox::str![ + "--certain-num +--uncertain-num +--help\tPrint help +-Y +-N +-h\tPrint help" + ] + ); + + assert_data_eq!( + complete!(cmd, "-N [TAB]"), + snapbox::str![ + "val1 +val2 +val3" + ] + ); + + assert_data_eq!( + complete!(cmd, "-N val1 [TAB]"), + snapbox::str![ + "--certain-num +--uncertain-num +--help\tPrint help +-Y +-N +-h\tPrint help" + ] + ); + + assert_data_eq!( + complete!(cmd, "-N val1 val2 val3 [TAB]"), + snapbox::str![ + "--certain-num +--uncertain-num +--help\tPrint help +-Y +-N +-h\tPrint help" + ] + ); +} + fn complete(cmd: &mut Command, args: impl AsRef, current_dir: Option<&Path>) -> String { let input = args.as_ref(); let mut args = vec![std::ffi::OsString::from(cmd.get_name())];