@@ -11,15 +11,17 @@ struct Person {
11
11
}
12
12
13
13
// I AM NOT DONE
14
+
14
15
// 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
16
17
// 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
19
20
// 5. Extract the other element from the split operation and parse it into a `usize` as the age
20
21
// 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
+
23
25
impl FromStr for Person {
24
26
type Err = String ;
25
27
fn from_str ( s : & str ) -> Result < Person , Self :: Err > {
@@ -48,50 +50,42 @@ mod tests {
48
50
assert_eq ! ( p. age, 32 ) ;
49
51
}
50
52
#[ test]
51
- #[ should_panic]
52
53
fn missing_age ( ) {
53
- "John," . parse :: < Person > ( ) . unwrap ( ) ;
54
+ assert ! ( "John," . parse:: <Person >( ) . is_err ( ) ) ;
54
55
}
55
56
56
57
#[ test]
57
- #[ should_panic]
58
58
fn invalid_age ( ) {
59
- "John,twenty" . parse :: < Person > ( ) . unwrap ( ) ;
59
+ assert ! ( "John,twenty" . parse:: <Person >( ) . is_err ( ) ) ;
60
60
}
61
61
62
62
#[ test]
63
- #[ should_panic]
64
63
fn missing_comma_and_age ( ) {
65
- "John" . parse :: < Person > ( ) . unwrap ( ) ;
64
+ assert ! ( "John" . parse:: <Person >( ) . is_err ( ) ) ;
66
65
}
67
66
68
67
#[ test]
69
- #[ should_panic]
70
68
fn missing_name ( ) {
71
- ",1" . parse :: < Person > ( ) . unwrap ( ) ;
69
+ assert ! ( ",1" . parse:: <Person >( ) . is_err ( ) ) ;
72
70
}
73
71
74
72
#[ test]
75
- #[ should_panic]
76
73
fn missing_name_and_age ( ) {
77
- "," . parse :: < Person > ( ) . unwrap ( ) ;
74
+ assert ! ( "," . parse:: <Person >( ) . is_err ( ) ) ;
78
75
}
79
76
80
77
#[ test]
81
- #[ should_panic]
82
78
fn missing_name_and_invalid_age ( ) {
83
- ",one" . parse :: < Person > ( ) . unwrap ( ) ;
79
+ assert ! ( ",one" . parse:: <Person >( ) . is_err ( ) ) ;
84
80
}
85
81
86
82
#[ test]
87
- #[ should_panic]
88
83
fn trailing_comma ( ) {
89
- "John,32," . parse :: < Person > ( ) . unwrap ( ) ;
84
+ assert ! ( "John,32," . parse:: <Person >( ) . is_err ( ) ) ;
90
85
}
91
86
92
87
#[ test]
93
- #[ should_panic]
94
88
fn trailing_comma_and_some_string ( ) {
95
- "John,32,man" . parse :: < Person > ( ) . unwrap ( ) ;
89
+ assert ! ( "John,32,man" . parse:: <Person >( ) . is_err ( ) ) ;
96
90
}
97
91
}
0 commit comments