summaryrefslogtreecommitdiff
path: root/exercises/conversions/from_str.rs
diff options
context:
space:
mode:
Diffstat (limited to 'exercises/conversions/from_str.rs')
-rw-r--r--exercises/conversions/from_str.rs40
1 files changed, 38 insertions, 2 deletions
diff --git a/exercises/conversions/from_str.rs b/exercises/conversions/from_str.rs
index 3c889d7..14e9e09 100644
--- a/exercises/conversions/from_str.rs
+++ b/exercises/conversions/from_str.rs
@@ -15,7 +15,8 @@ struct Person {
// 1. If the length of the provided string is 0, then return an error
// 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. Extract the other element from the split operation and parse it into a `usize` as the age
+// 4. If the name is empty, then return an error
+// 5. Extract the other element from the split operation and parse it into a `usize` as the age
// If while parsing the age, something goes wrong, then return an error
// Otherwise, then return a Result of a Person object
impl FromStr for Person {
@@ -39,11 +40,46 @@ mod tests {
}
#[test]
fn good_input() {
- assert!("John,32".parse::<Person>().is_ok());
+ let p = "John,32".parse::<Person>();
+ assert!(p.is_ok());
+ let p = p.unwrap();
+ assert_eq!(p.name, "John");
+ assert_eq!(p.age, 32);
}
#[test]
#[should_panic]
fn missing_age() {
+ "John,".parse::<Person>().unwrap();
+ }
+
+ #[test]
+ #[should_panic]
+ fn invalid_age() {
+ "John,twenty".parse::<Person>().unwrap();
+ }
+
+ #[test]
+ #[should_panic]
+ fn missing_comma_and_age() {
"John".parse::<Person>().unwrap();
}
+
+ #[test]
+ #[should_panic]
+ fn missing_name() {
+ ",1".parse::<Person>().unwrap();
+ }
+
+ #[test]
+ #[should_panic]
+ fn missing_name_and_age() {
+ ",".parse::<Person>().unwrap();
+ }
+
+ #[test]
+ #[should_panic]
+ fn missing_name_and_invalid_age() {
+ ",one".parse::<Person>().unwrap();
+ }
+
} \ No newline at end of file