Provide tests for Arg::default_value_if

While reading the code for the yaml translation, I've noticed that
there is a bug in the macro `yaml_opt_str` as well as a wrong `null`
value in the test fixture.

These tests add the expected behaviour on the given fixture, e.g.

    prog
and
    prog <WRONG>
where `<WRONG>` is **not** `other` should yield `None` for
`"positional2"`, whereas

    prog other
should yield "something" and

    prog --flag ARBITRARY_VALUE
should yield "some". The first two tests

- default_value_if_not_triggered
- default_value_if_not_triggered_by_argument

fail, as the second positional arguments *gets set*, although its
conditions aren't fulfilled.
This commit is contained in:
Benjamin Kästner 2020-08-14 17:27:58 +02:00
parent 5cf782afc0
commit 8a2e9804d7

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");
}