mirror of
https://github.com/rust-lang/rustlings.git
synced 2024-12-27 00:00:03 +03:00
fix(from_str): test for error instead of unwrap/should_panic
This commit is contained in:
parent
9f988bfe29
commit
15e71535f3
|
@ -11,15 +11,17 @@ struct Person {
|
||||||
}
|
}
|
||||||
|
|
||||||
// I AM NOT DONE
|
// I AM NOT DONE
|
||||||
|
|
||||||
// Steps:
|
// Steps:
|
||||||
// 1. If the length of the provided string is 0, then return an error
|
// 1. If the length of the provided string is 0 an error should be returned
|
||||||
// 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. Only 2 elements should returned from the split, otherwise return an error
|
||||||
// 4. If the name is empty, then return an error
|
// 4. Extract the first element from the split operation and use it as the name
|
||||||
// 5. Extract the other element from the split operation and parse it into a `usize` as the age
|
// 5. Extract the other element from the split operation and parse it into a `usize` as the age
|
||||||
// with something like `"4".parse::<usize>()`.
|
// with something like `"4".parse::<usize>()`.
|
||||||
// If while parsing the age, something goes wrong, then return an error
|
// 5. If while extracting the name and the age something goes wrong an error should be returned
|
||||||
// Otherwise, then return a Result of a Person object
|
// If everything goes well, then return a Result of a Person object
|
||||||
|
|
||||||
impl FromStr for Person {
|
impl FromStr for Person {
|
||||||
type Err = String;
|
type Err = String;
|
||||||
fn from_str(s: &str) -> Result<Person, Self::Err> {
|
fn from_str(s: &str) -> Result<Person, Self::Err> {
|
||||||
|
@ -48,50 +50,42 @@ mod tests {
|
||||||
assert_eq!(p.age, 32);
|
assert_eq!(p.age, 32);
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
|
||||||
fn missing_age() {
|
fn missing_age() {
|
||||||
"John,".parse::<Person>().unwrap();
|
assert!("John,".parse::<Person>().is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
|
||||||
fn invalid_age() {
|
fn invalid_age() {
|
||||||
"John,twenty".parse::<Person>().unwrap();
|
assert!("John,twenty".parse::<Person>().is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
|
||||||
fn missing_comma_and_age() {
|
fn missing_comma_and_age() {
|
||||||
"John".parse::<Person>().unwrap();
|
assert!("John".parse::<Person>().is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
|
||||||
fn missing_name() {
|
fn missing_name() {
|
||||||
",1".parse::<Person>().unwrap();
|
assert!(",1".parse::<Person>().is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
|
||||||
fn missing_name_and_age() {
|
fn missing_name_and_age() {
|
||||||
",".parse::<Person>().unwrap();
|
assert!(",".parse::<Person>().is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
|
||||||
fn missing_name_and_invalid_age() {
|
fn missing_name_and_invalid_age() {
|
||||||
",one".parse::<Person>().unwrap();
|
assert!(",one".parse::<Person>().is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
|
||||||
fn trailing_comma() {
|
fn trailing_comma() {
|
||||||
"John,32,".parse::<Person>().unwrap();
|
assert!("John,32,".parse::<Person>().is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
|
||||||
fn trailing_comma_and_some_string() {
|
fn trailing_comma_and_some_string() {
|
||||||
"John,32,man".parse::<Person>().unwrap();
|
assert!("John,32,man".parse::<Person>().is_err());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue