2019: tests(validators): Add tests for `clap_app!` macro and `FromStr` trait validator r=pksunkara a=nickelc



Co-authored-by: Constantin Nickel <constantin.nickel@gmail.com>
This commit is contained in:
bors[bot] 2020-07-19 10:28:03 +00:00 committed by GitHub
commit 1dd3fcb954
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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("multiarg"), Some("flag-set"));
assert_eq!(matches.value_of("multiarg2"), 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"]); .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] #[test]
fn test_validator_msg_newline() { fn test_validator_msg_newline() {
let res = App::new("test") let res = App::new("test")