Skip to content

Commit

Permalink
test(clap_complete): Add test cases for --flag bar and -f bar co…
Browse files Browse the repository at this point in the history
…mpletion
  • Loading branch information
shannmu committed Jun 29, 2024
1 parent 5b70856 commit 456053d
Showing 1 changed file with 113 additions and 1 deletion.
114 changes: 113 additions & 1 deletion clap_complete/tests/testsuite/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,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)
}
Expand Down Expand Up @@ -132,6 +132,118 @@ 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"]),
)
.args_conflicts_with_subcommands(true);

{
use std::fs;

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(testdir_path.join("c_dir")).unwrap();
fs::create_dir(testdir_path.join("d_dir")).unwrap();

assert_data_eq!(
complete!(cmd, "--input [TAB]", current_dir = Some(testdir_path)),
snapbox::str![
"a_file
b_file
c_dir/
d_dir/"
],
);

assert_data_eq!(
complete!(cmd, "-i [TAB]", current_dir = Some(testdir_path)),
snapbox::str![
"a_file
b_file
c_dir/
d_dir/"
],
);

assert_data_eq!(
complete!(cmd, "--input a[TAB]", current_dir = Some(testdir_path)),
snapbox::str![
"a_file"
],
);

assert_data_eq!(
complete!(cmd, "-i b[TAB]", current_dir = Some(testdir_path)),
snapbox::str![
"b_file"
],
);
}



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

assert_data_eq!(
complete!(cmd, "-F [TAB]"),
snapbox::str![
"json
yaml
toml"
],
);

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

assert_data_eq!(
complete!(cmd, "-F j[TAB]"),
snapbox::str![
"json"
],
);

assert_data_eq!(
complete!(cmd, "--format t[TAB]"),
snapbox::str![
"toml"
],
);

assert_data_eq!(
complete!(cmd, "-F t[TAB]"),
snapbox::str![
"toml"
],
);
}

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 456053d

Please sign in to comment.