-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests ~ add tests for new 'default_missing_value' configuration option
- Loading branch information
Showing
1 changed file
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
|
||
} |