Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions clap_complete/tests/testsuite/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1443,6 +1443,59 @@ pos-c
);
}

#[test]
fn suggest_require_equals() {
let mut cmd = Command::new("exhaustive")
.arg(
clap::Arg::new("format")
.long("format")
.require_equals(true)
.value_parser(["json", "yaml", "toml"]),
)
.arg(
clap::Arg::new("name")
.long("name")
.short('n'),
);

Comment on lines +1447 to +1460
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need a test case with num_args(0..=1) like at https://docs.rs/clap/latest/src/git/git.rs.html#25-31 which is a major use case for this feature

// When completing after an empty input, --format should appear with = appended
assert_data_eq!(
complete!(cmd, " [TAB]"),
snapbox::str![[r#"
--format
--name
--help Print help
"#]]
);

// Should complete values after --format=
assert_data_eq!(
complete!(cmd, "--format=[TAB]"),
snapbox::str![[r#"
--format=json
--format=yaml
--format=toml
"#]]
);

// Should complete values after --format=j
assert_data_eq!(
complete!(cmd, "--format=j[TAB]"),
snapbox::str!["--format=json"]
);

// When typing --format (space), should NOT suggest values since require_equals forbids it
// Current behavior: suggests values (this will change with the fix)
assert_data_eq!(
complete!(cmd, "--format [TAB]"),
snapbox::str![[r#"
json
yaml
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