summaryrefslogtreecommitdiff
path: root/exercises/conversions/from_into.rs
diff options
context:
space:
mode:
Diffstat (limited to 'exercises/conversions/from_into.rs')
-rw-r--r--exercises/conversions/from_into.rs38
1 files changed, 37 insertions, 1 deletions
diff --git a/exercises/conversions/from_into.rs b/exercises/conversions/from_into.rs
index c5cba23..8fb9eb0 100644
--- a/exercises/conversions/from_into.rs
+++ b/exercises/conversions/from_into.rs
@@ -29,7 +29,8 @@ impl Default for Person {
// 1. If the length of the provided string is 0, then return the default of Person
// 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 the default of Person
+// 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 the default of Person
// Otherwise, then return an instantiated Person object with the results
impl From<&str> for Person {
@@ -77,4 +78,39 @@ mod tests {
assert_eq!(p.name, "John");
assert_eq!(p.age, 30);
}
+
+ #[test]
+ fn test_missing_comma_and_age() {
+ let p: Person = Person::from("Mark");
+ assert_eq!(p.name, "John");
+ assert_eq!(p.age, 30);
+ }
+
+ #[test]
+ fn test_missing_age() {
+ let p: Person = Person::from("Mark,");
+ assert_eq!(p.name, "John");
+ assert_eq!(p.age, 30);
+ }
+
+ #[test]
+ fn test_missing_name() {
+ let p: Person = Person::from(",1");
+ assert_eq!(p.name, "John");
+ assert_eq!(p.age, 30);
+ }
+
+ #[test]
+ fn test_missing_name_and_age() {
+ let p: Person = Person::from(",");
+ assert_eq!(p.name, "John");
+ assert_eq!(p.age, 30);
+ }
+
+ #[test]
+ fn test_missing_name_and_invalid_age() {
+ let p: Person = Person::from(",one");
+ assert_eq!(p.name, "John");
+ assert_eq!(p.age, 30);
+ }
}