-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(try_from_into): Add tests #571
Conversation
There was a problem hiding this 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(); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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.
Just going to link this PR with issue #549. If the unit tests can be rewritten to replace Thanks, |
I doubt if I should replace |
@fiplox It should be fine to do that, as long as the Thanks, |
@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? |
@fiplox That would be fine with me. |
There was a problem hiding this 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.
There was a problem hiding this 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
It should not add any confusion as it is one of the last exercises. 😉 |
158c322
to
0b5dfbf
Compare
Did force push to leave only useful information. |
Co-authored-by: Volodymyr Patuta <6977238-fiplox@users.noreply.gitlab.com>
Co-authored-by: Volodymyr Patuta <6977238-fiplox@users.noreply.gitlab.com>
Co-authored-by: Volodymyr Patuta <6977238-fiplox@users.noreply.gitlab.com>
Co-authored-by: Volodymyr Patuta <6977238-fiplox@users.noreply.gitlab.com>
Co-authored-by: Volodymyr Patuta <6977238-fiplox@users.noreply.gitlab.com>
Co-authored-by: Volodymyr Patuta <6977238-fiplox@users.noreply.gitlab.com>
Co-authored-by: Volodymyr Patuta <6977238-fiplox@users.noreply.gitlab.com>
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:
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 ! 😃