-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
propose asserts #1662
propose asserts #1662
Conversation
Why not just
adds as much additional value as adding a bunch of macros for different operators. |
@nagisa What's important here is the formatting. |
Instead of @nagisa's implementation it could be macro_rules! assert_op {
($op:tt, $left:expr, $right:expr) => ({
match (&$left, &$right) {
(left_val, right_val) => {
if !(*left_val $op *right_val) {
panic!("assertion failed: `(left {} right)` \
(left: `{:?}`, right: `{:?}`)", stringify!($op), left_val, right_val)
}
}
}
})
} to get the desired formatting (may need an |
@durka I like it. |
Would an macro_rules! assert_approx_eq {
($left: expr, $right: expr, $tol: expr) => ({
match (&$left, &$right, $tol) {
(left_val , right_val, tol_val) => {
let delta = (left_val - right_val).abs();
if !(delta < tol_val) {
panic!(
"assertion failed: `(left ≈ right)` \
(left: `{:?}`, right: `{:?}`) \
with ∆={:1.1e} (allowed ∆={:e})",
*left_val , *right_val, delta, tol_val
)
}
}
}
});
($left: expr, $right: expr) => (assert_approx_eq!(($left), ($right), 1e-15))
} |
@Luthaf i think it would! in fact it still exists in the tests for the standard library. https://github.com/rust-lang/rust/search?utf8=%E2%9C%93&q=assert_approx_eq i have a crate for it here: https://crates.io/crates/assert_approx_eq |
I would be vary of adding absolute delta based floating point equality checking. |
While I'm not particularly against this, I'm also not sure it's really worth it. It's trivial to define your own macros if you really want inequality ones and I'm not sure asserting inequalities is common enough to warrant special macros for them in the standard library. |
I like the idea. I have a few comments, if you don't mind.
I believe that there is room in the standard library for a decent selection of assertions, and I don't particularly agree with "it's trivial to define your own" so there is no need for this. The code shown in the link for People who are just starting out may be far more likely to heavily rely on testing libraries while learning, and providing a modest selection of testing capabilities can certainly be very convenient. As opposed to: just write your own macros, or just download a new crate. |
Big 👍 to this! For the same reason, I think If we wanted we could implement them with |
I think it makes sense to reduce the surprise factor and add |
I don't have any super strong opinions here personally, although I will note that I'd personally probably never import an entire crate just to get these macros and would instead just write them myself. 'Tis the nature of utility functions and does somewhat bring me on the side of favoring their inclusion in |
🔔 This RFC is now entering its week-long final comment period 🔔 The libs team discussed this RFC today (along with #1653) and the overall feeling was that these are a very low maintenance burden, if any, and the road is already paved with |
cc rust-lang/rust#2367, which seems like an alternative to this |
We currently have |
If it would add them, it would add 8 macros in total, tripling the number of assert macros so we'd have 12 in total. I count 24 non-assert macros. Seems like a bit much to me :/ |
|
||
Any addition to the standard library will need to be maintained forever, so it is | ||
worth weighing the maintenance cost of this over the value add. Given that it is so | ||
similar to `assert_eq`, I believe the weight of this drawback is low. |
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.
It's similar, but it won't be used nearly as often. I at least never felt the need to have something like it in std.
Rust has taken many steps to reduce roadblocks for unit testing. Cargo integration, attributes, doctests, etc all take away the "it's too much of a pain in the ass to unit test" excuse. These asserts continue in that direction, so I think it's a good move. I'd also support an "assert fuzzy equality" for floating point numbers, as that would eliminate helper function boilerplate for unit testing floating point math code. |
I agree, and when you put that together with the likely papercut of trying other assertion forms after seeing |
The libs team got a chance to discuss this RFC during yesterday's triage and the conclusion was to not merge. The data collected by @lambda shows that these may not really be used all that much in practice and with the explicit purpose of just testing the standard library may not be the best place for such macros. In that sense we were a little uncomfortable with the combinatorial explosion of macros and have decided to go with just Thanks of course though for the RFC @ashleygwilliams and the discussion everyone! |
this is an rfc to add several more assert macros. it is a follow up to #1653, specifically @wycats's comment: #1653 (comment).