From 65e90cd31fe9f5c7309268bccd91361d20ce52e0 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 6 Jun 2024 14:56:40 -0500 Subject: [PATCH] test(parser): Show conditional requireds and exclusive --- tests/builder/conflicts.rs | 120 +++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) diff --git a/tests/builder/conflicts.rs b/tests/builder/conflicts.rs index 7053f94bf2a..65eee61de38 100644 --- a/tests/builder/conflicts.rs +++ b/tests/builder/conflicts.rs @@ -684,6 +684,126 @@ fn exclusive_with_required() { cmd.clone().try_get_matches_from(["bug", "--test"]).unwrap(); } +#[test] +fn exclusive_with_required_unless_present() { + let cmd = Command::new("bug") + .arg( + Arg::new("exclusive") + .long("exclusive") + .action(ArgAction::SetTrue) + .exclusive(true), + ) + .arg( + Arg::new("required") + .long("required") + .action(ArgAction::SetTrue) + .required_unless_present("alternative"), + ) + .arg( + Arg::new("alternative") + .long("alternative") + .action(ArgAction::SetTrue), + ); + + cmd.clone() + .try_get_matches_from(["bug", "--required"]) + .unwrap(); + + cmd.clone() + .try_get_matches_from(["bug", "--alternative"]) + .unwrap(); + + cmd.clone().try_get_matches_from(["bug"]).unwrap_err(); + + cmd.clone() + .try_get_matches_from(["bug", "--exclusive", "--required"]) + .unwrap_err(); + + cmd.clone() + .try_get_matches_from(["bug", "--exclusive"]) + .unwrap_err(); +} + +#[test] +fn exclusive_with_required_unless_present_any() { + let cmd = Command::new("bug") + .arg( + Arg::new("exclusive") + .long("exclusive") + .action(ArgAction::SetTrue) + .exclusive(true), + ) + .arg( + Arg::new("required") + .long("required") + .action(ArgAction::SetTrue) + .required_unless_present_any(["alternative"]), + ) + .arg( + Arg::new("alternative") + .long("alternative") + .action(ArgAction::SetTrue), + ); + + cmd.clone() + .try_get_matches_from(["bug", "--required"]) + .unwrap(); + + cmd.clone() + .try_get_matches_from(["bug", "--alternative"]) + .unwrap(); + + cmd.clone().try_get_matches_from(["bug"]).unwrap_err(); + + cmd.clone() + .try_get_matches_from(["bug", "--exclusive", "--required"]) + .unwrap_err(); + + cmd.clone() + .try_get_matches_from(["bug", "--exclusive"]) + .unwrap_err(); +} + +#[test] +fn exclusive_with_required_unless_present_all() { + let cmd = Command::new("bug") + .arg( + Arg::new("exclusive") + .long("exclusive") + .action(ArgAction::SetTrue) + .exclusive(true), + ) + .arg( + Arg::new("required") + .long("required") + .action(ArgAction::SetTrue) + .required_unless_present_all(["alternative"]), + ) + .arg( + Arg::new("alternative") + .long("alternative") + .action(ArgAction::SetTrue), + ); + + cmd.clone() + .try_get_matches_from(["bug", "--required"]) + .unwrap(); + + cmd.clone() + .try_get_matches_from(["bug", "--alternative"]) + .unwrap(); + + cmd.clone().try_get_matches_from(["bug"]).unwrap_err(); + + cmd.clone() + .try_get_matches_from(["bug", "--exclusive", "--required"]) + .unwrap_err(); + + cmd.clone() + .try_get_matches_from(["bug", "--exclusive"]) + .unwrap_err(); +} + #[test] #[cfg(feature = "error-context")] fn option_conflicts_with_subcommand() {