From 15e71535f37cfaed36e22eb778728d186e2104ab Mon Sep 17 00:00:00 2001 From: Jean-Francois Chevrette Date: Thu, 21 Jan 2021 07:55:22 -0500 Subject: [PATCH] fix(from_str): test for error instead of unwrap/should_panic --- exercises/conversions/from_str.rs | 36 +++++++++++++------------------ 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/exercises/conversions/from_str.rs b/exercises/conversions/from_str.rs index 70ed1796b4..558a903365 100644 --- a/exercises/conversions/from_str.rs +++ b/exercises/conversions/from_str.rs @@ -11,15 +11,17 @@ struct Person { } // I AM NOT DONE + // Steps: -// 1. If the length of the provided string is 0, then return an error +// 1. If the length of the provided string is 0 an error should be returned // 2. Split the given string on the commas present in it -// 3. Extract the first element from the split operation and use it as the name -// 4. If the name is empty, then return an error +// 3. Only 2 elements should returned from the split, otherwise return an error +// 4. Extract the first element from the split operation and use it as the name // 5. Extract the other element from the split operation and parse it into a `usize` as the age // with something like `"4".parse::()`. -// If while parsing the age, something goes wrong, then return an error -// Otherwise, then return a Result of a Person object +// 5. If while extracting the name and the age something goes wrong an error should be returned +// If everything goes well, then return a Result of a Person object + impl FromStr for Person { type Err = String; fn from_str(s: &str) -> Result { @@ -48,50 +50,42 @@ mod tests { assert_eq!(p.age, 32); } #[test] - #[should_panic] fn missing_age() { - "John,".parse::().unwrap(); + assert!("John,".parse::().is_err()); } #[test] - #[should_panic] fn invalid_age() { - "John,twenty".parse::().unwrap(); + assert!("John,twenty".parse::().is_err()); } #[test] - #[should_panic] fn missing_comma_and_age() { - "John".parse::().unwrap(); + assert!("John".parse::().is_err()); } #[test] - #[should_panic] fn missing_name() { - ",1".parse::().unwrap(); + assert!(",1".parse::().is_err()); } #[test] - #[should_panic] fn missing_name_and_age() { - ",".parse::().unwrap(); + assert!(",".parse::().is_err()); } #[test] - #[should_panic] fn missing_name_and_invalid_age() { - ",one".parse::().unwrap(); + assert!(",one".parse::().is_err()); } #[test] - #[should_panic] fn trailing_comma() { - "John,32,".parse::().unwrap(); + assert!("John,32,".parse::().is_err()); } #[test] - #[should_panic] fn trailing_comma_and_some_string() { - "John,32,man".parse::().unwrap(); + assert!("John,32,man".parse::().is_err()); } }