-
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 assert_ne #1653
propose assert_ne #1653
Conversation
@ashleygwilliams How often do you need assert_ne? I didn't want it once |
@KalitaAlexey i found that when writing tests i wanted it pretty often and on tweeting that i made the crate i got feedback that people were excited and wanted me to try to get it into the language. hence this rfc. |
[summary]: #summary | ||
|
||
`assert_ne` is a macro that takes 2 arguments and panics if they are equal. It | ||
works and is implemented identically to `assert_eq` and serves as its compliment. |
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.
s/compliment/complement/
(And below.)
I personally have found it annoying sometimes fall back on |
I have often come across cases where I would prefer to use assert_ne. |
Yes, to this. assert_ne! is easier to read, less error prone, and more ergonomic. I usually wind up writing some form of this in a lot of my projects. |
Bikeshed alert: I have a slight preference for a more informative suffix than
I think that I understand where But in either of those cases, there tends to be an emphasis on both:
In Rust we have certainly de-emphasized 1 in favor of familiarity (e.g. the shift from Update: (I admit I have not proven whether |
@pnkfelix totally hear this and mostly agree! i chose would be totally open to changing it if others agreed. |
@pnkfelix: I agree with your sentiments, but also think about other things in the API, like |
@bjz One rarely types or reads |
There are many other operations that would make sense like all the If This has the major advantage of not needing textual identifiers, but using the actual operation and still producing a nice error message. It would probably be possible to improve the errors produced by these macros, since the syntax extension can produce better spans than macros. |
I think making Pytest does the same thing (demo) and I've found that extremely helpful. |
|
A syntax extension could convert |
Given that we already have an |
@ashleygwilliams Thanks so much for this. I have wanted this occasionally, and falling back to bare I agree that a more ambitious refactor of I'm for this 😄 |
Why not include |
I rolled my own |
I don't think we need this in the standard library, this should be fine to live in a crate. You could use this: |
I specifically wished for this a few days ago when writing test code. I'd love to see a smarter Thanks for proposing this! |
Like @kennytm I'd prever having all comparison operators represented by
Having only |
i'm happy to write the rest of these as per @Evrey , i actually have a crate called in other news, i've updated my |
i would propose that using a crate is not ideal, if only because when updates like the one i just mentioned happen the behavior is out of sync. it'd be great to just be able to edit and improve them all at once. |
@tbu- ah I forgot that the alternative format of In a quick search over the |
One good way to look at this is to look at examples from a language that has a richer set of assertions built in, and has for a long time so people have had a chance to use them. Python has a fairly rich set available for unit tests. If I grep the Django codebase for anything that looks like a call to one of the unit test assertions, I see the following top 20. Note that several of these are custom assertions, not the built in ones:
I'll also note that I first tried this on Flask, but only found one use of the above assertions. It turns out that Flask uses Pytest, which instead provides support for printing values of subexpressions directly from the I've also run these same search on my dayjob codebases, and see similar results; more than an order of magnitude more use of Given the large proliferation of both built-in assertions, and custom assertions, in Python codebases using the standard unittest framework, I'd probably be more in favor of moving towards a pytest/power-assert style |
I may have sounded a little too negative in the previous comment, so want to clarify. I support either adding richer assertions, or making If we're not going with a more powerful |
I don't think it matters that assert_ne is needed less frequently than asset_eq. Its my expectation that when I have an "eq" form of an action, its paired with an "ne" form as well. For the occasion when "ne" is what I need, it is frustrating and disruptive to my work that the macro isn't at hand. |
I think the key here is that the majority of not-equal assertions have the form |
@Amanieu ...unless you're testing a PartialEq implementation that defines "semantic equivalence" for values that aren't structurally equal. In which case you'd like to know what actual values you had on both sides. |
@Amanieu Where are you getting your "majority" from? When I look at one of my (Python) codebases, I find out of 20 When I look at Django, I see a higher percentage of comparing against a primitive constant, but I also see a good number in which neither side is a constant, and a number of tests which are testing a custom equality predicate to ensure that particular different objects don't compare as equal, while other ones do compare as equal, like @joshtriplett mentions. |
@lambda In one of my C codebases I have 27 asserts which contain |
The libs team got a chance to discuss this RFC during triage yesterday, and the decision was to merge. This macro helps reduce the "surprise factor" when looking at the standard library's macros and also isn't much of a maintenance burden, so it seems like an all-around win. We have also decided to not merge #1662 for now (the other family of assertion macros), but I'll write more over there in a second. @ashleygwilliams could you also update this to include the |
@ashleygwilliams asked me to watch out for this decision, because she's in a few meetings today. As it turns out, she's in one literally right now. She says she'll update it later today, once she's out of said meeting. |
@alexcrichton Could the response by the libs team acknowledge that inequality assertions are used less frequently? I'm fine with the decision, but I think it's not nice to only mention the arguments in favor of the decision of the libs team, rather it should state relevant counter-arguments and say that the benefits outweigh them. Sorry to ask for this in a thread that I have participated in as well, but I think the proclaimed "all-around win" is maybe a bit disingenuous. |
(Unless the libs team doesn't think that frequency of use is a relevant argument.) |
@tbu- ah yes I forgot to mention that we discussed that although this seems to be used not as often as the equality macro, for example, it's still a surprise that it doesn't exist. That, when coupled with the fact that the maintenance overhead is basically zero, convinced us that it'd be fine to merge. |
@alexcrichton updated. lemme know if you'd like me to squash/rebase etc. |
also: is the next step that i make a PR on the language repo? |
@ashleygwilliams Yup. |
Awesome, thanks again @ashleygwilliams! We actually prefer to avoid squashes on RFCs to preserve history, so as-is was fine. Also yeah the next step is to make a PR against rust-lang/rust, referencing the... |
🎉 i'm on it! |
this is an rfc to add a
assert_ne
macro to complimentassert_eq
. it exists as a crate: https://crates.io/crates/assert_ne.