Skip to content

Commit

Permalink
tests: adds tests for required_unless settings
Browse files Browse the repository at this point in the history
  • Loading branch information
kbknapp committed May 3, 2016
1 parent 7c89fc5 commit 9fdad2e
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/args/arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ impl<'a, 'b> Arg<'a, 'b> {
if r { self.set(ArgSettings::Required) } else { self.unset(ArgSettings::Required) }
}

/// Sets an arg that override this arg's required setting. (i.e. this arg will be required
/// Sets an arg that override this arg's required setting. (i.e. this arg will be required
/// unless this other argument is present).
///
/// **Pro Tip:** Using `Arg::required_unless` implies `Arg::required` and is therefore not
Expand All @@ -556,7 +556,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// # ;
/// ```
///
/// Setting `required_unless(name)` requires that the argument be used at runtime *unless*
/// Setting `required_unless(name)` requires that the argument be used at runtime *unless*
/// `name` is present. In the following example, the required argument is *not* provided, but
/// it's not an error because the `unless` arg has been supplied.
///
Expand Down Expand Up @@ -588,7 +588,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .arg(Arg::with_name("dbg")
/// .long("debug"))
/// .get_matches_from_safe(vec![
/// "unlesstest"
/// "unlesstest"
/// ]);
///
/// assert!(res.is_err());
Expand Down Expand Up @@ -618,8 +618,8 @@ impl<'a, 'b> Arg<'a, 'b> {
/// # ;
/// ```
///
/// Setting `required_unless_all(names)` requires that the argument be used at runtime *unless*
/// *all* the args in `names` are present. In the following example, the required argument is
/// Setting `required_unless_all(names)` requires that the argument be used at runtime *unless*
/// *all* the args in `names` are present. In the following example, the required argument is
/// *not* provided, but it's not an error because all the `unless` args have been supplied.
///
/// ```rust
Expand All @@ -643,7 +643,7 @@ impl<'a, 'b> Arg<'a, 'b> {
///
/// Setting `required_unless_all(names)` and *not* supplying *all* of `names` or this arg is an
/// error.
///
///
/// ```rust
/// # use clap::{App, Arg, ErrorKind};
/// let res = App::new("unlessall")
Expand Down Expand Up @@ -678,7 +678,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// Sets args that override this arg's required setting. (i.e. this arg will be required unless
/// *at least one of* these other argument are present).
///
/// **NOTE:** If you wish for the this argument to only be required if *all of* these args are
/// **NOTE:** If you wish for the this argument to only be required if *all of* these args are
/// present see `Arg::required_unless_all`
///
/// # Examples
Expand All @@ -690,9 +690,9 @@ impl<'a, 'b> Arg<'a, 'b> {
/// # ;
/// ```
///
/// Setting `required_unless_one(names)` requires that the argument be used at runtime *unless*
/// *at least one of* the args in `names` are present. In the following example, the required
/// argument is *not* provided, but it's not an error because one the `unless` args have been
/// Setting `required_unless_one(names)` requires that the argument be used at runtime *unless*
/// *at least one of* the args in `names` are present. In the following example, the required
/// argument is *not* provided, but it's not an error because one the `unless` args have been
/// supplied.
///
/// ```rust
Expand All @@ -714,9 +714,9 @@ impl<'a, 'b> Arg<'a, 'b> {
/// assert!(res.is_ok());
/// ```
///
/// Setting `required_unless_one(names)` and *not* supplying *at least one of* `names` or this
/// Setting `required_unless_one(names)` and *not* supplying *at least one of* `names` or this
/// arg is an error.
///
///
/// ```rust
/// # use clap::{App, Arg, ErrorKind};
/// let res = App::new("unlessone")
Expand Down
127 changes: 127 additions & 0 deletions tests/require.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,130 @@ fn arg_require_group_3() {
assert!(m.is_present("other"));
assert!(m.is_present("flag"));
}

// REQUIRED_UNLESS

#[test]
fn required_unless() {
let res = App::new("unlesstest")
.arg(Arg::with_name("cfg")
.required_unless("dbg")
.takes_value(true)
.long("config"))
.arg(Arg::with_name("dbg")
.long("debug"))
.get_matches_from_safe(vec![
"unlesstest", "--debug"
]);

assert!(res.is_ok());
let m = res.unwrap();
assert!(m.is_present("dbg"));
assert!(!m.is_present("cfg"));
}

#[test]
fn required_unless_err() {
let res = App::new("unlesstest")
.arg(Arg::with_name("cfg")
.required_unless("dbg")
.takes_value(true)
.long("config"))
.arg(Arg::with_name("dbg")
.long("debug"))
.get_matches_from_safe(vec![
"unlesstest"
]);

assert!(res.is_err());
assert_eq!(res.unwrap_err().kind, ErrorKind::MissingRequiredArgument);
}

// REQUIRED_UNLESS_ALL

#[test]
fn required_unless_all() {
let res = App::new("unlessall")
.arg(Arg::with_name("cfg")
.required_unless_all(&["dbg", "infile"])
.takes_value(true)
.long("config"))
.arg(Arg::with_name("dbg")
.long("debug"))
.arg(Arg::with_name("infile")
.short("i")
.takes_value(true))
.get_matches_from_safe(vec![
"unlessall", "--debug", "-i", "file"
]);

assert!(res.is_ok());
let m = res.unwrap();
assert!(m.is_present("dbg"));
assert!(m.is_present("infile"));
assert!(!m.is_present("cfg"));
}

#[test]
fn required_unless_all_err() {
let res = App::new("unlessall")
.arg(Arg::with_name("cfg")
.required_unless_all(&["dbg", "infile"])
.takes_value(true)
.long("config"))
.arg(Arg::with_name("dbg")
.long("debug"))
.arg(Arg::with_name("infile")
.short("i")
.takes_value(true))
.get_matches_from_safe(vec![
"unlessall", "--debug"
]);

assert!(res.is_err());
assert_eq!(res.unwrap_err().kind, ErrorKind::MissingRequiredArgument);
}

// REQUIRED_UNLESS_ONE

#[test]
fn required_unless_one() {
let res = App::new("unlessone")
.arg(Arg::with_name("cfg")
.required_unless_one(&["dbg", "infile"])
.takes_value(true)
.long("config"))
.arg(Arg::with_name("dbg")
.long("debug"))
.arg(Arg::with_name("infile")
.short("i")
.takes_value(true))
.get_matches_from_safe(vec![
"unlessone", "--debug"
]);

assert!(res.is_ok());
let m = res.unwrap();
assert!(m.is_present("dbg"));
assert!(!m.is_present("cfg"));
}

#[test]
fn required_unless_one_err() {
let res = App::new("unlessone")
.arg(Arg::with_name("cfg")
.required_unless_one(&["dbg", "infile"])
.takes_value(true)
.long("config"))
.arg(Arg::with_name("dbg")
.long("debug"))
.arg(Arg::with_name("infile")
.short("i")
.takes_value(true))
.get_matches_from_safe(vec![
"unlessone"
]);

assert!(res.is_err());
assert_eq!(res.unwrap_err().kind, ErrorKind::MissingRequiredArgument);
}

0 comments on commit 9fdad2e

Please sign in to comment.