From 2f53bb3f907a130248371a0f3a0412c062927f73 Mon Sep 17 00:00:00 2001 From: shannmu Date: Wed, 19 Jun 2024 16:15:02 +0800 Subject: [PATCH] test(clap_complete): Add test cases for `--flag bar ` and `-f bar` completion --- clap_complete/tests/testsuite/dynamic.rs | 153 ++++++++++++++++++++++- 1 file changed, 152 insertions(+), 1 deletion(-) diff --git a/clap_complete/tests/testsuite/dynamic.rs b/clap_complete/tests/testsuite/dynamic.rs index 6eff7f29c18..93898b62e13 100644 --- a/clap_complete/tests/testsuite/dynamic.rs +++ b/clap_complete/tests/testsuite/dynamic.rs @@ -1,5 +1,6 @@ #![cfg(feature = "unstable-dynamic")] +use std::fs; use std::path::Path; use clap::{builder::PossibleValue, Command}; @@ -9,7 +10,7 @@ macro_rules! complete { ($cmd:expr, $input:expr$(, current_dir = $current_dir:expr)? $(,)?) => { { #[allow(unused)] - let current_dir = None; + let current_dir: Option<&Path> = None; $(let current_dir = $current_dir;)? complete(&mut $cmd, $input, current_dir) } @@ -288,6 +289,156 @@ goodbye-world ); } +#[test] +fn suggest_argument_value() { + let mut cmd = Command::new("dynamic") + .arg( + clap::Arg::new("input") + .long("input") + .short('i') + .value_hint(clap::ValueHint::FilePath), + ) + .arg( + clap::Arg::new("format") + .long("format") + .short('F') + .value_parser(["json", "yaml", "toml"]), + ) + .arg( + clap::Arg::new("count") + .long("count") + .short('c') + .action(clap::ArgAction::Count), + ) + .arg(clap::Arg::new("positional").value_parser(["pos_a", "pos_b", "pos_c"])) + .args_conflicts_with_subcommands(true); + + let testdir = snapbox::dir::DirRoot::mutable_temp().unwrap(); + let testdir_path = testdir.path().unwrap(); + + fs::write(testdir_path.join("a_file"), "").unwrap(); + fs::write(testdir_path.join("b_file"), "").unwrap(); + fs::create_dir_all(testdir_path.join("c_dir")).unwrap(); + fs::create_dir_all(testdir_path.join("d_dir")).unwrap(); + + assert_data_eq!( + complete!(cmd, "--input [TAB]", current_dir = Some(testdir_path)), + snapbox::str![ + "--input +--format +--count +--help Print help +-i +-F +-c +-h Print help +pos_a +pos_b +pos_c" + ], + ); + + assert_data_eq!( + complete!(cmd, "-i [TAB]", current_dir = Some(testdir_path)), + snapbox::str![ + "--input +--format +--count +--help Print help +-i +-F +-c +-h Print help +pos_a +pos_b +pos_c" + ], + ); + + assert_data_eq!( + complete!(cmd, "--input a[TAB]", current_dir = Some(testdir_path)), + snapbox::str![""], + ); + + assert_data_eq!( + complete!(cmd, "-i b[TAB]", current_dir = Some(testdir_path)), + snapbox::str![""], + ); + + assert_data_eq!( + complete!(cmd, "--format [TAB]"), + snapbox::str![ + "--input +--format +--count +--help Print help +-i +-F +-c +-h Print help +pos_a +pos_b +pos_c" + ], + ); + + assert_data_eq!( + complete!(cmd, "-F [TAB]"), + snapbox::str![ + "--input +--format +--count +--help Print help +-i +-F +-c +-h Print help +pos_a +pos_b +pos_c" + ], + ); + + assert_data_eq!(complete!(cmd, "--format j[TAB]"), snapbox::str![""],); + + assert_data_eq!(complete!(cmd, "-F j[TAB]"), snapbox::str![""],); + + assert_data_eq!(complete!(cmd, "--format t[TAB]"), snapbox::str![""],); + + assert_data_eq!(complete!(cmd, "-F t[TAB]"), snapbox::str![""],); + + assert_data_eq!( + complete!(cmd, "-cccF [TAB]"), + snapbox::str![ + "--input +--format +--count +--help\tPrint help +-i +-F +-c +-h\tPrint help +pos_a +pos_b +pos_c" + ] + ); + + assert_data_eq!( + complete!(cmd, "--input a_file [TAB]"), + snapbox::str![ + "--input +--format +--count +--help\tPrint help +-i +-F +-c +-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())];