From 9fdad2e970b84fb25db0cade6a3a42df2e87572e Mon Sep 17 00:00:00 2001 From: Kevin K Date: Mon, 2 May 2016 14:12:57 -0400 Subject: [PATCH] tests: adds tests for required_unless settings --- src/args/arg.rs | 24 ++++----- tests/require.rs | 127 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 139 insertions(+), 12 deletions(-) diff --git a/src/args/arg.rs b/src/args/arg.rs index 55aea50761f..8b2d94fe481 100644 --- a/src/args/arg.rs +++ b/src/args/arg.rs @@ -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 @@ -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. /// @@ -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()); @@ -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 @@ -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") @@ -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 @@ -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 @@ -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") diff --git a/tests/require.rs b/tests/require.rs index 7bf5a9f49d0..b01d64e029b 100644 --- a/tests/require.rs +++ b/tests/require.rs @@ -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); +}