summaryrefslogtreecommitdiff
path: root/exercises/conversions/from_into.rs
diff options
context:
space:
mode:
authorapatniv <apatniv@gmail.com>2020-05-02 20:41:11 -0400
committerapatniv <apatniv@gmail.com>2020-05-02 20:41:11 -0400
commit41f989135d79eba4602d4fde828698acf9a9f235 (patch)
tree57f75e01245db3ec36e4200c2eeb4913e64f8ec7 /exercises/conversions/from_into.rs
parent19fb1c240c25b7dc50fc64c48a60bdb5f28f6de6 (diff)
Review Comments: Add other test cases
Diffstat (limited to 'exercises/conversions/from_into.rs')
-rw-r--r--exercises/conversions/from_into.rs38
1 files changed, 37 insertions, 1 deletions
diff --git a/exercises/conversions/from_into.rs b/exercises/conversions/from_into.rs
index 58b860e..cfcfa30 100644
--- a/exercises/conversions/from_into.rs
+++ b/exercises/conversions/from_into.rs
@@ -29,7 +29,8 @@ impl Default for 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
// 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
// Otherwise, then return an instantiated Person onject with the results
impl From<&str> for Person {
@@ -77,4 +78,39 @@ mod tests {
assert_eq!(p.name, "John");
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);
+ }
}