mirror of
https://github.com/clap-rs/clap
synced 2024-12-13 22:32:33 +00:00
Merge #2019
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:
commit
1dd3fcb954
2 changed files with 35 additions and 0 deletions
|
@ -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));
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
|
|
Loading…
Reference in a new issue