From 066d7456530756dfd29f0e499cb3c3e4d5bea78a Mon Sep 17 00:00:00 2001 From: Constantin Nickel Date: Thu, 16 Jul 2020 19:42:24 +0200 Subject: [PATCH] tests(validators): Add tests for `clap_app!` macro and `FromStr` trait --- tests/macros.rs | 23 +++++++++++++++++++++++ tests/validators.rs | 12 ++++++++++++ 2 files changed, 35 insertions(+) diff --git a/tests/macros.rs b/tests/macros.rs index 9404b2e1..e3df8aa8 100644 --- a/tests/macros.rs +++ b/tests/macros.rs @@ -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 { + val.parse::().map_err(|e| e.to_string()) + } + + let app = clap_app!(claptests => + (@arg inline: { |val| val.parse::() }) + (@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::("inline").ok(), Some(12)); + assert_eq!(matches.value_of_t::("func1").ok(), Some(34)); + assert_eq!(matches.value_of_t::("func2").ok(), Some(56)); +} diff --git a/tests/validators.rs b/tests/validators.rs index 8914eefc..bcc8f56b 100644 --- a/tests/validators.rs +++ b/tests/validators.rs @@ -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::("from_str").ok(), Some(1234)); +} + #[test] fn test_validator_msg_newline() { let res = App::new("test")