-
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
Closed
Closed
propose asserts #1662
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
- Feature Name: Asserts | ||
- Start Date: 2016-06-29 | ||
- RFC PR: | ||
- Rust Issue: | ||
|
||
# Summary | ||
[summary]: #summary | ||
|
||
Asserts would be a series of macros to use in `#[test]`, expanding | ||
on `assert_eq`. This rfc proposes that the following macros be added: | ||
|
||
- `assert_gt` (greater than) | ||
- `assert_lt` (less than) | ||
- `assert_ge` (greater than or equal) | ||
- `assert_le` (less than or equal) | ||
|
||
# Motivation | ||
[motivation]: #motivation | ||
|
||
The goal of this feature is to provide a feature-ful set of asserts | ||
with consistent formatting and messaging for use in `#[test]`. This | ||
proposal is a follow up to a previous rfc that proposed only | ||
`assert_ne` (not equals). | ||
|
||
# Detailed design | ||
[design]: #detailed-design | ||
|
||
These macros should be added with nearly identical implentation as | ||
`assert_eq`, with changes to the condition and message only: | ||
|
||
```rust | ||
macro_rules! assert_eq { | ||
($left:expr , $right:expr) => ({ | ||
match (&$left, &$right) { | ||
(left_val, right_val) => { | ||
if !(*left_val == *right_val) { | ||
panic!("assertion failed: `(left == right)` \ | ||
(left: `{:?}`, right: `{:?}`)", left_val, right_val) | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
``` | ||
|
||
# Drawbacks | ||
[drawbacks]: #drawbacks | ||
|
||
Why should we *not* do this? | ||
|
||
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. | ||
|
||
# Alternatives | ||
[alternatives]: #alternatives | ||
|
||
Alternatively, users implement this feature themselves, or use a crate. | ||
|
||
# Unresolved questions | ||
[unresolved]: #unresolved-questions | ||
|
||
It was brought up in the rfc on `assert_ne` that if `assert!` were rewritten as a | ||
syntax extension instead of as a macro as it is now, then it would be possible to | ||
automatically detect `assert!(x < y)`, `assert!(x == y)`, `assert!(x != y)`. | ||
|
||
This, however, would be a longer and more serious change than the proposed addtional | ||
macros. | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.