|
|
|
@ -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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|