Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(try_from_into): Add tests #571

Merged
merged 2 commits into from
Nov 7, 2020

Conversation

fiplox
Copy link
Contributor

@fiplox fiplox commented Oct 26, 2020

Hello everyone 馃槃. I was trying to resolve the try_from_into exercise and found out that I passed all the tests if I sum up for example all elements in array and test if it is <= 765 or >= 0. But if we sum up (255 + 255 + (-1)) it gives us a good result but the third value is negative, so we should not pass it.

I added 3 tests for each type to prevent users pass this type of errors.

Example for array:

// Array implementation
impl TryFrom<[i16; 3]> for Color {
    type Error = String;
    fn try_from(arr: [i16; 3]) -> Result<Self, Self::Error> {
        if arr[0] + arr[1] + arr[2] <= 765 && arr[0] + arr[1] + arr[2] >= 0 {
            let c = [arr[0] as u8, arr[1] as u8, arr[2] as u8];
            Ok(Color {
                red: c[0],
                green: c[1],
                blue: c[2],
            })
        } else {
            Err("NOP".to_string())
        }
    }
}

This code passes all the tests, but if we add test with [-1, 255, 255] it does not pass 馃憤.
I'm open for discussion if it is not merge-able to correct ! 馃槂

Copy link
Contributor

@darnuria darnuria left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise I love the idea, because it help to avoid sneaking around the intent of the exercise. When I "cheese" an exercise I always personally feel bad. ;P

@@ -76,6 +73,11 @@ mod tests {
let _ = Color::try_from((-1, -10, -256)).unwrap();
}
#[test]
#[should_panic]
fn test_tuple_sum() {
let _ = Color::try_from((-1, 255, 255)).unwrap();
Copy link
Contributor

@darnuria darnuria Oct 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disclamer: I am not maintainer of Rustlings just a modest suggestion:

Would have done something
assert!(Color::try_from((-1, 255, 255)).is_err())
playground link

I try to avoid unwrap in test because I hate the stack-trace it generate would prefers a expect or if the error is precise even an assert_eq! or a assert!(expr.is_ok) or assert!(expr.is_err()) if it's hard to match value.

Copy link
Contributor Author

@fiplox fiplox Oct 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then we would need to rewrite every test with is_err() because every test uses unwrap().
If there is demand to rewrite every test without unwrap() I'm in!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi y'all, this is a great addition. The desire to move from .unwrap() has been brought up in #549. If you're up for it, please feel free to rewrite the tests by substituting .unwrap() with .is_err() and removing the #should_panic directives.

@AbdouSeck
Copy link
Contributor

AbdouSeck commented Oct 30, 2020

Just going to link this PR with issue #549. If the unit tests can be rewritten to replace .unwrap() with .is_err(), then this PR could close the linked issue.

Thanks,
Abdou

@AbdouSeck AbdouSeck self-assigned this Oct 30, 2020
@AbdouSeck AbdouSeck added A-exercises Area: Exercises S-waiting-on-author Status: Waiting on issue/PR author labels Oct 30, 2020
@fiplox
Copy link
Contributor Author

fiplox commented Oct 30, 2020

I doubt if I should replace unwrap() in correct tests too

@AbdouSeck
Copy link
Contributor

@fiplox It should be fine to do that, as long as the #[should_panic] directive is removed from the target tests.

Thanks,
Abdou

@fiplox
Copy link
Contributor Author

fiplox commented Oct 30, 2020

@AbdouSeck Would it be ok to replace this test :

#[test]
    fn test_slice_correct() {
        let v = vec![183, 65, 14];
        let c = Color::try_from(&v[..]).unwrap();
        assert_eq!(c.red, 183);
        assert_eq!(c.green, 65);
        assert_eq!(c.blue, 14);
    }

with this :

    #[test]
    fn test_slice_correct() {
        let v = vec![183, 65, 14];
        let c = match Color::try_from(&v[..]) {
            Ok(color) => color,
            Err(_) => Color {
                red: 0,
                green: 0,
                blue: 0,
            },
        };
        assert_eq!(c.red, 183);
        assert_eq!(c.green, 65);
        assert_eq!(c.blue, 14);
    }

and other correct tests too the same way?

@AbdouSeck
Copy link
Contributor

@fiplox That would be fine with me.

Copy link
Contributor

@darnuria darnuria left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorrryyyy i know i look like the bikesheding strange guy who comes up after the discussion settled.

But I think it's better to rethink the way of checking error, beginners look in rustlings tests to find out a solution or to learn idiomatic testing. It's like correction of homework, giving the best of ourself help beginners to tackle Rust difficulties. :D Just IMHO.

exercises/conversions/try_from_into.rs Outdated Show resolved Hide resolved
exercises/conversions/try_from_into.rs Outdated Show resolved Hide resolved
exercises/conversions/try_from_into.rs Outdated Show resolved Hide resolved
exercises/conversions/try_from_into.rs Show resolved Hide resolved
Copy link
Contributor

@AbdouSeck AbdouSeck left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not a huge fan of the additional PartialEq derive, but I hope it does not add any confusion to beginners. @fmoko this should be good to go, unless something else comes up.

Thanks,
Abdou

@fiplox
Copy link
Contributor Author

fiplox commented Oct 30, 2020

It should not add any confusion as it is one of the last exercises. 馃槈

@fiplox
Copy link
Contributor Author

fiplox commented Oct 31, 2020

Did force push to leave only useful information.

@shadows-withal shadows-withal changed the title Add tests in try_from_into.rs feat(try_from_into): Add tests Nov 7, 2020
@shadows-withal shadows-withal merged commit 95ccd92 into rust-lang:main Nov 7, 2020
catull pushed a commit to catull/rustlings that referenced this pull request Dec 14, 2020
Co-authored-by: Volodymyr Patuta <6977238-fiplox@users.noreply.gitlab.com>
jfchevrette pushed a commit to jfchevrette/rustlings that referenced this pull request Jan 21, 2021
Co-authored-by: Volodymyr Patuta <6977238-fiplox@users.noreply.gitlab.com>
JuniousY pushed a commit to JuniousY/rustlings that referenced this pull request Jan 28, 2021
Co-authored-by: Volodymyr Patuta <6977238-fiplox@users.noreply.gitlab.com>
JuniousY pushed a commit to JuniousY/rustlings that referenced this pull request Jan 28, 2021
Co-authored-by: Volodymyr Patuta <6977238-fiplox@users.noreply.gitlab.com>
mccormickt pushed a commit to mccormickt/rustlings that referenced this pull request Mar 10, 2021
Co-authored-by: Volodymyr Patuta <6977238-fiplox@users.noreply.gitlab.com>
noiffion pushed a commit to noiffion/rustlings that referenced this pull request Aug 20, 2021
Co-authored-by: Volodymyr Patuta <6977238-fiplox@users.noreply.gitlab.com>
bugaolengdeyuxiaoer pushed a commit to bugaolengdeyuxiaoer/rustlings that referenced this pull request Dec 28, 2021
Co-authored-by: Volodymyr Patuta <6977238-fiplox@users.noreply.gitlab.com>
ppp3 pushed a commit to ppp3/rustlings that referenced this pull request May 23, 2022
Co-authored-by: Volodymyr Patuta <6977238-fiplox@users.noreply.gitlab.com>
dmoore04 pushed a commit to dmoore04/rustlings that referenced this pull request Sep 11, 2022
Co-authored-by: Volodymyr Patuta <6977238-fiplox@users.noreply.gitlab.com>
jsattari pushed a commit to jsattari/rustlings that referenced this pull request Nov 9, 2022
Co-authored-by: Volodymyr Patuta <6977238-fiplox@users.noreply.gitlab.com>
Spacebody pushed a commit to Spacebody/my-rustlings that referenced this pull request Nov 18, 2022
Co-authored-by: Volodymyr Patuta <6977238-fiplox@users.noreply.gitlab.com>
ComixHe pushed a commit to ComixHe/rustlings that referenced this pull request Jun 20, 2023
Co-authored-by: Volodymyr Patuta <6977238-fiplox@users.noreply.gitlab.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-exercises Area: Exercises S-waiting-on-author Status: Waiting on issue/PR author
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants