tests(validators): Add tests for clap_app! macro and FromStr trait

This commit is contained in:
Constantin Nickel 2020-07-16 19:42:24 +02:00
parent ff6beebd6e
commit 066d745653
2 changed files with 35 additions and 0 deletions

View file

@ -360,3 +360,26 @@ fn multiarg() {
assert_eq!(matches.value_of("multiarg"), Some("flag-set"));
assert_eq!(matches.value_of("multiarg2"), Some("flag-set"));
}
#[test]
fn validator() {
use std::str::FromStr;
fn validate(val: &str) -> Result<u32, String> {
val.parse::<u32>().map_err(|e| e.to_string())
}
let app = clap_app!(claptests =>
(@arg inline: { |val| val.parse::<u16>() })
(@arg func1: { validate })
(@arg func2: { u64::from_str })
);
let matches = app
.try_get_matches_from(&["bin", "12", "34", "56"])
.expect("match failed");
assert_eq!(matches.value_of_t::<u16>("inline").ok(), Some(12));
assert_eq!(matches.value_of_t::<u16>("func1").ok(), Some(34));
assert_eq!(matches.value_of_t::<u16>("func2").ok(), Some(56));
}

View file

@ -18,6 +18,18 @@ fn both_validator_and_validator_os() {
.try_get_matches_from(&["app", "1"]);
}
#[test]
fn test_validator_fromstr_trait() {
use std::str::FromStr;
let matches = App::new("test")
.arg(Arg::new("from_str").validator(u32::from_str))
.try_get_matches_from(&["app", "1234"])
.expect("match failed");
assert_eq!(matches.value_of_t::<u32>("from_str").ok(), Some(1234));
}
#[test]
fn test_validator_msg_newline() {
let res = App::new("test")