Skip to content

Commit 15e7153

Browse files
committed
fix(from_str): test for error instead of unwrap/should_panic
1 parent 9f988bf commit 15e7153

File tree

1 file changed

+15
-21
lines changed

1 file changed

+15
-21
lines changed

exercises/conversions/from_str.rs

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@ struct Person {
1111
}
1212

1313
// I AM NOT DONE
14+
1415
// Steps:
15-
// 1. If the length of the provided string is 0, then return an error
16+
// 1. If the length of the provided string is 0 an error should be returned
1617
// 2. Split the given string on the commas present in it
17-
// 3. Extract the first element from the split operation and use it as the name
18-
// 4. If the name is empty, then return an error
18+
// 3. Only 2 elements should returned from the split, otherwise return an error
19+
// 4. Extract the first element from the split operation and use it as the name
1920
// 5. Extract the other element from the split operation and parse it into a `usize` as the age
2021
// with something like `"4".parse::<usize>()`.
21-
// If while parsing the age, something goes wrong, then return an error
22-
// Otherwise, then return a Result of a Person object
22+
// 5. If while extracting the name and the age something goes wrong an error should be returned
23+
// If everything goes well, then return a Result of a Person object
24+
2325
impl FromStr for Person {
2426
type Err = String;
2527
fn from_str(s: &str) -> Result<Person, Self::Err> {
@@ -48,50 +50,42 @@ mod tests {
4850
assert_eq!(p.age, 32);
4951
}
5052
#[test]
51-
#[should_panic]
5253
fn missing_age() {
53-
"John,".parse::<Person>().unwrap();
54+
assert!("John,".parse::<Person>().is_err());
5455
}
5556

5657
#[test]
57-
#[should_panic]
5858
fn invalid_age() {
59-
"John,twenty".parse::<Person>().unwrap();
59+
assert!("John,twenty".parse::<Person>().is_err());
6060
}
6161

6262
#[test]
63-
#[should_panic]
6463
fn missing_comma_and_age() {
65-
"John".parse::<Person>().unwrap();
64+
assert!("John".parse::<Person>().is_err());
6665
}
6766

6867
#[test]
69-
#[should_panic]
7068
fn missing_name() {
71-
",1".parse::<Person>().unwrap();
69+
assert!(",1".parse::<Person>().is_err());
7270
}
7371

7472
#[test]
75-
#[should_panic]
7673
fn missing_name_and_age() {
77-
",".parse::<Person>().unwrap();
74+
assert!(",".parse::<Person>().is_err());
7875
}
7976

8077
#[test]
81-
#[should_panic]
8278
fn missing_name_and_invalid_age() {
83-
",one".parse::<Person>().unwrap();
79+
assert!(",one".parse::<Person>().is_err());
8480
}
8581

8682
#[test]
87-
#[should_panic]
8883
fn trailing_comma() {
89-
"John,32,".parse::<Person>().unwrap();
84+
assert!("John,32,".parse::<Person>().is_err());
9085
}
9186

9287
#[test]
93-
#[should_panic]
9488
fn trailing_comma_and_some_string() {
95-
"John,32,man".parse::<Person>().unwrap();
89+
assert!("John,32,man".parse::<Person>().is_err());
9690
}
9791
}

0 commit comments

Comments
 (0)