Skip to content

Commit

Permalink
test(parser): Show conditional requireds and exclusive
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Jun 6, 2024
1 parent 5e3386b commit 65e90cd
Showing 1 changed file with 120 additions and 0 deletions.
120 changes: 120 additions & 0 deletions tests/builder/conflicts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 65e90cd

Please sign in to comment.