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.rs34
1 files changed, 33 insertions, 1 deletions
diff --git a/exercises/conversions/from_str.rs b/exercises/conversions/from_str.rs
index 7b4fdac..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 {
@@ -48,6 +49,37 @@ mod tests {
#[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