Skip to content

Commit 41f9891

Browse files
committed
Review Comments: Add other test cases
1 parent 19fb1c2 commit 41f9891

File tree

3 files changed

+102
-3
lines changed

3 files changed

+102
-3
lines changed

exercises/conversions/from_into.rs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ impl Default for Person {
2929
// 1. If the length of the provided string is 0, then return the default of Person
3030
// 2. Split the given string on the commas present in it
3131
// 3. Extract the first element from the split operation and use it as the name
32-
// 4. Extract the other element from the split operation and parse it into a `usize` as the age
32+
// 4. If the name is empty, then return the default of Person
33+
// 5. Extract the other element from the split operation and parse it into a `usize` as the age
3334
// If while parsing the age, something goes wrong, then return the default of Person
3435
// Otherwise, then return an instantiated Person onject with the results
3536
impl From<&str> for Person {
@@ -77,4 +78,39 @@ mod tests {
7778
assert_eq!(p.name, "John");
7879
assert_eq!(p.age, 30);
7980
}
81+
82+
#[test]
83+
fn test_missing_comma_and_age() {
84+
let p: Person = Person::from("Mark");
85+
assert_eq!(p.name, "John");
86+
assert_eq!(p.age, 30);
87+
}
88+
89+
#[test]
90+
fn test_missing_age() {
91+
let p: Person = Person::from("Mark,");
92+
assert_eq!(p.name, "John");
93+
assert_eq!(p.age, 30);
94+
}
95+
96+
#[test]
97+
fn test_missing_name() {
98+
let p: Person = Person::from(",1");
99+
assert_eq!(p.name, "John");
100+
assert_eq!(p.age, 30);
101+
}
102+
103+
#[test]
104+
fn test_missing_name_and_age() {
105+
let p: Person = Person::from(",");
106+
assert_eq!(p.name, "John");
107+
assert_eq!(p.age, 30);
108+
}
109+
110+
#[test]
111+
fn test_missing_name_and_invalid_age() {
112+
let p: Person = Person::from(",one");
113+
assert_eq!(p.name, "John");
114+
assert_eq!(p.age, 30);
115+
}
80116
}

exercises/conversions/from_str.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ struct Person {
1515
// 1. If the length of the provided string is 0, then return an error
1616
// 2. Split the given string on the commas present in it
1717
// 3. Extract the first element from the split operation and use it as the name
18-
// 4. Extract the other element from the split operation and parse it into a `usize` as the age
18+
// 4. If the name is empty, then return an error
19+
// 5. Extract the other element from the split operation and parse it into a `usize` as the age
1920
// If while parsing the age, something goes wrong, then return an error
2021
// Otherwise, then return a Result of a Person object
2122
impl FromStr for Person {
@@ -48,6 +49,37 @@ mod tests {
4849
#[test]
4950
#[should_panic]
5051
fn missing_age() {
52+
"John,".parse::<Person>().unwrap();
53+
}
54+
55+
#[test]
56+
#[should_panic]
57+
fn invalid_age() {
58+
"John,twenty".parse::<Person>().unwrap();
59+
}
60+
61+
#[test]
62+
#[should_panic]
63+
fn missing_comma_and_age() {
5164
"John".parse::<Person>().unwrap();
5265
}
66+
67+
#[test]
68+
#[should_panic]
69+
fn missing_name() {
70+
",1".parse::<Person>().unwrap();
71+
}
72+
73+
#[test]
74+
#[should_panic]
75+
fn missing_name_and_age() {
76+
",".parse::<Person>().unwrap();
77+
}
78+
79+
#[test]
80+
#[should_panic]
81+
fn missing_name_and_invalid_age() {
82+
",one".parse::<Person>().unwrap();
83+
}
84+
5385
}

exercises/conversions/try_from_into.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ struct Person {
2222
// 1. If the length of the provided string is 0, then return an error
2323
// 2. Split the given string on the commas present in it
2424
// 3. Extract the first element from the split operation and use it as the name
25-
// 4. Extract the other element from the split operation and parse it into a `usize` as the age
25+
// 4. If the name is empty, then return an error.
26+
// 5. Extract the other element from the split operation and parse it into a `usize` as the age
2627
// If while parsing the age, something goes wrong, then return an error
2728
// Otherwise, then return a Result of a Person object
2829
impl TryFrom<&str> for Person {
@@ -68,4 +69,34 @@ mod tests {
6869
fn test_panic_bad_age() {
6970
let p = Person::try_from("Mark,twenty").unwrap();
7071
}
72+
73+
#[test]
74+
#[should_panic]
75+
fn test_missing_comma_and_age() {
76+
let _: Person = "Mark".try_into().unwrap();
77+
}
78+
79+
#[test]
80+
#[should_panic]
81+
fn test_missing_age() {
82+
let _: Person = "Mark,".try_into().unwrap();
83+
}
84+
85+
#[test]
86+
#[should_panic]
87+
fn test_missing_name() {
88+
let _ : Person = ",1".try_into().unwrap();
89+
}
90+
91+
#[test]
92+
#[should_panic]
93+
fn test_missing_name_and_age() {
94+
let _: Person = ",".try_into().unwrap();
95+
}
96+
97+
#[test]
98+
#[should_panic]
99+
fn test_missing_name_and_invalid_age() {
100+
let _: Person = ",one".try_into().unwrap();
101+
}
71102
}

0 commit comments

Comments
 (0)