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 Jul 23, 2024
1 parent 932ca13 commit 2f53bb3
Showing 1 changed file with 152 additions and 1 deletion.
153 changes: 152 additions & 1 deletion clap_complete/tests/testsuite/dynamic.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![cfg(feature = "unstable-dynamic")]

use std::fs;
use std::path::Path;

use clap::{builder::PossibleValue, Command};
Expand All @@ -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)
}
Expand Down Expand Up @@ -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())];
Expand Down

0 comments on commit 2f53bb3

Please sign in to comment.