test(clap_complete): Add test cases for --flag bar and -f bar completion

This commit is contained in:
shannmu 2024-06-19 16:15:02 +08:00
parent 932ca1356f
commit 2f53bb3f90

View file

@ -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<str>, current_dir: Option<&Path>) -> String {
let input = args.as_ref();
let mut args = vec![std::ffi::OsString::from(cmd.get_name())];