Merge pull request #368 from apatniv/update_test_case
This commit is contained in:
commit
a39ffb2fb8
|
@ -29,7 +29,8 @@ impl Default for Person {
|
||||||
// 1. If the length of the provided string is 0, then return the default of 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
|
// 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
|
// 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
|
// If while parsing the age, something goes wrong, then return the default of Person
|
||||||
// Otherwise, then return an instantiated Person object with the results
|
// Otherwise, then return an instantiated Person object with the results
|
||||||
impl From<&str> for Person {
|
impl From<&str> for Person {
|
||||||
|
@ -77,4 +78,39 @@ mod tests {
|
||||||
assert_eq!(p.name, "John");
|
assert_eq!(p.name, "John");
|
||||||
assert_eq!(p.age, 30);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,7 +15,8 @@ struct Person {
|
||||||
// 1. If the length of the provided string is 0, then return an error
|
// 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
|
// 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
|
// 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
|
// If while parsing the age, something goes wrong, then return an error
|
||||||
// Otherwise, then return a Result of a Person object
|
// Otherwise, then return a Result of a Person object
|
||||||
impl FromStr for Person {
|
impl FromStr for Person {
|
||||||
|
@ -39,11 +40,46 @@ mod tests {
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn good_input() {
|
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]
|
#[test]
|
||||||
#[should_panic]
|
#[should_panic]
|
||||||
fn missing_age() {
|
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();
|
"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();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -22,7 +22,8 @@ struct Person {
|
||||||
// 1. If the length of the provided string is 0, then return an error
|
// 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
|
// 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
|
// 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
|
// If while parsing the age, something goes wrong, then return an error
|
||||||
// Otherwise, then return a Result of a Person object
|
// Otherwise, then return a Result of a Person object
|
||||||
impl TryFrom<&str> for Person {
|
impl TryFrom<&str> for Person {
|
||||||
|
@ -68,4 +69,34 @@ mod tests {
|
||||||
fn test_panic_bad_age() {
|
fn test_panic_bad_age() {
|
||||||
let p = Person::try_from("Mark,twenty").unwrap();
|
let p = Person::try_from("Mark,twenty").unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[should_panic]
|
||||||
|
fn test_missing_comma_and_age() {
|
||||||
|
let _: Person = "Mark".try_into().unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[should_panic]
|
||||||
|
fn test_missing_age() {
|
||||||
|
let _: Person = "Mark,".try_into().unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[should_panic]
|
||||||
|
fn test_missing_name() {
|
||||||
|
let _ : Person = ",1".try_into().unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[should_panic]
|
||||||
|
fn test_missing_name_and_age() {
|
||||||
|
let _: Person = ",".try_into().unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[should_panic]
|
||||||
|
fn test_missing_name_and_invalid_age() {
|
||||||
|
let _: Person = ",one".try_into().unwrap();
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue