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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

propose asserts #1662

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions text/0000-asserts.md
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.
Copy link
Contributor

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.


# 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.