From 4a9e01b0bce380619779d099d9f1dbaad7f1c568 Mon Sep 17 00:00:00 2001 From: Patrick Marks Date: Fri, 15 May 2020 09:09:07 -0700 Subject: [PATCH] ensure that validation failure error message includes a newline. also add a basic test of a failed validation --- src/parse/errors.rs | 1 + tests/validators.rs | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/parse/errors.rs b/src/parse/errors.rs index 8da0bf87..ace794c8 100644 --- a/src/parse/errors.rs +++ b/src/parse/errors.rs @@ -851,6 +851,7 @@ impl Error { } c.none(&format!(": {}", err))?; + try_help(&mut c)?; Ok(Error { cause: format!( diff --git a/tests/validators.rs b/tests/validators.rs index 31e7186d..8914eefc 100644 --- a/tests/validators.rs +++ b/tests/validators.rs @@ -17,3 +17,22 @@ fn both_validator_and_validator_os() { ) .try_get_matches_from(&["app", "1"]); } + +#[test] +fn test_validator_msg_newline() { + let res = App::new("test") + .arg(Arg::new("test").validator(|val| val.parse::().map_err(|e| e.to_string()))) + .try_get_matches_from(&["app", "f"]); + + assert!(res.is_err()); + let err = res.unwrap_err(); + + assert_eq!( + err.cause, + "Invalid value for \'\': invalid digit found in string" + ); + + // This message is the only thing that gets printed -- make sure it ends with a newline + let msg = format!("{}", err); + assert!(msg.ends_with('\n')); +}