2072: Fix YAML's default_value_if handling r=pksunkara a=bkaestner



Co-authored-by: Benjamin Kästner <benjamin.kaestner@gmail.com>
This commit is contained in:
bors[bot] 2020-08-14 17:05:08 +00:00 committed by GitHub
commit 16cf7685d7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 2 deletions

View file

@ -65,7 +65,7 @@ macro_rules! yaml_vec_or_str {
#[cfg(feature = "yaml")]
macro_rules! yaml_opt_str {
($v:expr) => {{
if $v.is_null() {
if !$v.is_null() {
Some(
$v.as_str()
.unwrap_or_else(|| panic!("failed to convert YAML {:?} value to a string", $v)),

View file

@ -21,7 +21,7 @@ args:
about: tests positionals with exclusions
index: 2
default_value_if:
- [flag, Null, some]
- [flag, null, some]
- [positional, other, something]
- flag:
short: f

View file

@ -54,3 +54,48 @@ fn value_hint() {
.unwrap();
assert_eq!(arg.get_value_hint(), ValueHint::FilePath);
}
#[test]
fn default_value_if_not_triggered_by_argument() {
let yml = load_yaml!("fixtures/app.yaml");
let app = App::from(yml);
// Fixtures use "other" as value
let matches = app.try_get_matches_from(vec!["prog", "wrong"]).unwrap();
assert!(matches.value_of("positional2").is_none());
}
#[test]
fn default_value_if_triggered_by_matching_argument() {
let yml = load_yaml!("fixtures/app.yaml");
let app = App::from(yml);
let matches = app.try_get_matches_from(vec!["prog", "other"]).unwrap();
assert_eq!(matches.value_of("positional2").unwrap(), "something");
}
#[test]
fn default_value_if_triggered_by_flag() {
let yml = load_yaml!("fixtures/app.yaml");
let app = App::from(yml);
let matches = app
.try_get_matches_from(vec!["prog", "--flag", "flagvalue"])
.unwrap();
assert_eq!(matches.value_of("positional2").unwrap(), "some");
}
#[test]
fn default_value_if_triggered_by_flag_and_argument() {
let yml = load_yaml!("fixtures/app.yaml");
let app = App::from(yml);
let matches = app
.try_get_matches_from(vec!["prog", "--flag", "flagvalue", "other"])
.unwrap();
// First condition triggers, therefore "some"
assert_eq!(matches.value_of("positional2").unwrap(), "some");
}