Skip to content

Commit

Permalink
tests ~ add tests for new 'default_missing_value' configuration option
Browse files Browse the repository at this point in the history
  • Loading branch information
rivy committed May 20, 2020
1 parent c37ae4a commit b4cf266
Showing 1 changed file with 116 additions and 0 deletions.
116 changes: 116 additions & 0 deletions tests/default_missing_vals.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#[cfg(test)]
mod default_missing_vals {

use clap::{App, Arg};

#[test]
fn opt_missing() {
let r = App::new("df")
.arg(Arg::new("color")
.long("color")
.value_name("WHEN")
.possible_values(&["always", "auto", "never"])
.multiple(true)
.overrides_with("color")
.about(
r#"Specify WHEN to colorize output.
[WHEN] == ( *always* | auto | never ) ; "--color=auto" is assumed if the option is never specified
"#,
)
.default_value("auto")
.hide_default_value(true)
.min_values(0)
.require_equals(true)
.default_missing_value("always")
)
.try_get_matches_from(vec![""]);
assert!(r.is_ok());
let m = r.unwrap();
assert!(m.is_present("color"));
assert_eq!(m.value_of("color").unwrap(), "auto");
}

#[test]
fn opt_present_with_no_value() {
let r = App::new("df")
.arg(Arg::new("color").long("color")
.value_name("WHEN")
.possible_values(&["always", "auto", "never"])
.multiple(true)
.overrides_with("color")
.about(
r#"Specify WHEN to colorize output.
[WHEN] == ( *always* | auto | never ) ; "--color=auto" is assumed if the option is never specified
"#,
)
.default_value("auto")
.hide_default_value(true)
.min_values(0)
.require_equals(true)
.default_missing_value("always")
)
.try_get_matches_from(vec!["", "--color"]);
assert!(r.is_ok());
let m = r.unwrap();
assert!(m.is_present("color"));
assert_eq!(m.value_of("color").unwrap(), "always");
}

#[test]
fn opt_present_with_value() {
let r = App::new("df")
.arg(Arg::new("color")
.long("color")
.value_name("WHEN")
.possible_values(&["always", "auto", "never"])
.multiple(true)
.overrides_with("color")
.about(
r#"Specify WHEN to colorize output.
[WHEN] == ( *always* | auto | never ) ; "--color=auto" is assumed if the option is never specified
"#,
)
.default_value("auto")
.hide_default_value(true)
.min_values(0)
.require_equals(true)
.default_missing_value("always")
)
.try_get_matches_from(vec!["", "--color=never"]);
assert!(r.is_ok());
let m = r.unwrap();
assert!(m.is_present("color"));
assert_eq!(m.value_of("color").unwrap(), "never");
}

#[test]
fn opt_default() {
// assert no change to usual argument handling when adding default_missing_value()
let r = App::new("df")
.arg(Arg::from("-o [opt] 'some opt'")
.default_value("default")
.default_missing_value("default_missing")
)
.try_get_matches_from(vec![""]);
assert!(r.is_ok());
let m = r.unwrap();
assert!(m.is_present("o"));
assert_eq!(m.value_of("o").unwrap(), "default");
}

#[test]
fn opt_default_user_override() {
// assert no change to usual argument handling when adding default_missing_value()
let r = App::new("df")
.arg(Arg::from("--opt [FILE] 'some arg'")
.default_value("default")
.default_missing_value("default_missing")
)
.try_get_matches_from(vec!["", "--opt", "value"]);
assert!(r.is_ok());
let m = r.unwrap();
assert!(m.is_present("opt"));
assert_eq!(m.value_of("opt").unwrap(), "value");
}

}

0 comments on commit b4cf266

Please sign in to comment.