rustlings icon indicating copy to clipboard operation
rustlings copied to clipboard

Incorrect test case in /conversions/from_into.rs

Open jonah-saltzman opened this issue 2 years ago • 1 comments

The task is to implement From trait for Person. The steps are explicitly given:

// Steps:
// 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. 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 object with the results

There is a test:

    #[test]
    fn test_trailing_comma() {
        let p: Person = Person::from("Mike,32,");
        assert_eq!(p.name, "John");
        assert_eq!(p.age, 30);
    }

If the steps are followed exactly, Person::from("Mike,32,"); should return Person{ name: "Mike", age: 32 }, not the default person.

  • The length of the provided string is not zero
  • Splitting on commas yields ["Mike", "32", ""]
  • The first element is not empty, will become the name, "Mike"
  • The second element is able to be parsed to 32usize

The instructions do not say to check the length of the Vec<&str> created in step 2.

jonah-saltzman avatar May 21 '22 15:05 jonah-saltzman

I would argue that the test is fine, but the problem description is incomplete. When I did this problem, I was also confused here, but eventually took the tests and the language "the other element" (step 5) to imply this was supposed to return a BadLen error if we had anything other than two elements after splitting.

So I agree that there's a disconnect, but my inclination would be to modify the write-up instead of changing the test. Thoughts?

NicMcPhee avatar Jun 19 '22 04:06 NicMcPhee