| 1 |
# Pretty Assertions |
| 2 |
|
| 3 |
[](https://crates.io/crates/pretty-assertions) |
| 4 |
[](https://docs.rs/pretty_assertions) |
| 5 |
[](https://crates.io/crates/pretty-assertions) |
| 6 |
[](https://crates.io/crates/pretty-assertions) |
| 7 |
|
| 8 |
Overwrite `assert_eq!` with a drop-in replacement, adding a colorful diff. |
| 9 |
|
| 10 |
## Usage |
| 11 |
|
| 12 |
When writing tests in Rust, you'll probably use `assert_eq!(a, b)` _a lot_. |
| 13 |
|
| 14 |
If such a test fails, it will present all the details of `a` and `b`. |
| 15 |
But you have to spot the differences yourself, which is not always straightforward, |
| 16 |
like here: |
| 17 |
|
| 18 |
 |
| 19 |
|
| 20 |
Wouldn't that task be _much_ easier with a colorful diff? |
| 21 |
|
| 22 |
 |
| 23 |
|
| 24 |
Yep — and you only need **one line of code** to make it happen: |
| 25 |
|
| 26 |
```rust,ignore |
| 27 |
use pretty_assertions::{assert_eq, assert_ne}; |
| 28 |
``` |
| 29 |
|
| 30 |
<details> |
| 31 |
<summary>Show the example behind the screenshots above.</summary> |
| 32 |
|
| 33 |
```rust,ignore |
| 34 |
// 1. add the `pretty_assertions` dependency to `Cargo.toml`. |
| 35 |
// 2. insert this line at the top of each module, as needed |
| 36 |
use pretty_assertions::{assert_eq, assert_ne}; |
| 37 |
|
| 38 |
fn main() { |
| 39 |
#[derive(Debug, PartialEq)] |
| 40 |
struct Foo { |
| 41 |
lorem: &'static str, |
| 42 |
ipsum: u32, |
| 43 |
dolor: Result<String, String>, |
| 44 |
} |
| 45 |
|
| 46 |
let x = Some(Foo { lorem: "Hello World!", ipsum: 42, dolor: Ok("hey".to_string())}); |
| 47 |
let y = Some(Foo { lorem: "Hello Wrold!", ipsum: 42, dolor: Ok("hey ho!".to_string())}); |
| 48 |
|
| 49 |
assert_eq!(x, y); |
| 50 |
} |
| 51 |
``` |
| 52 |
|
| 53 |
</details> |
| 54 |
|
| 55 |
## Semantic Versioning |
| 56 |
|
| 57 |
The exact output of assertions is **not guaranteed** to be consistent over time, and may change between minor versions. |
| 58 |
The output of this crate is designed to be read by a human. It is not suitable for exact comparison, for example in snapshot testing. |
| 59 |
|
| 60 |
This crate adheres to semantic versioning for publically exported crate items, **except** the `private` module, which may change between any version. |
| 61 |
|
| 62 |
## Tip |
| 63 |
|
| 64 |
Specify it as [`[dev-dependencies]`](http://doc.crates.io/specifying-dependencies.html#development-dependencies) |
| 65 |
and it will only be used for compiling tests, examples, and benchmarks. |
| 66 |
This way the compile time of `cargo build` won't be affected! |
| 67 |
|
| 68 |
Also add `#[cfg(test)]` to your `use` statements, like this: |
| 69 |
|
| 70 |
```rust,ignore |
| 71 |
#[cfg(test)] |
| 72 |
use pretty_assertions::{assert_eq, assert_ne}; |
| 73 |
``` |
| 74 |
|
| 75 |
## Notes |
| 76 |
|
| 77 |
- Since `Rust 2018` edition, you need to declare |
| 78 |
`use pretty_assertions::{assert_eq, assert_ne};` per module. |
| 79 |
Before you would write `#[macro_use] extern crate pretty_assertions;`. |
| 80 |
- The replacement is only effective in your own crate, not in other libraries |
| 81 |
you include. |
| 82 |
- `assert_ne` is also switched to multi-line presentation, but does _not_ show |
| 83 |
a diff. |
| 84 |
- Under Windows, the terminal state is modified to properly handle VT100 |
| 85 |
escape sequences, which may break display for certain use cases. |
| 86 |
- The minimum supported rust version (MSRV) is 1.35.0 |
| 87 |
|
| 88 |
### `no_std` support |
| 89 |
|
| 90 |
For `no_std` support, disable the `std` feature and enable the `alloc` feature: |
| 91 |
|
| 92 |
```toml |
| 93 |
# Cargo.toml |
| 94 |
pretty_assertions = { version= "...", default-features = false, features = ["alloc"] } |
| 95 |
``` |
| 96 |
|
| 97 |
## License |
| 98 |
|
| 99 |
Licensed under either of |
| 100 |
|
| 101 |
- Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or <http://www.apache.org/licenses/LICENSE-2.0>) |
| 102 |
- MIT license ([LICENSE-MIT](LICENSE-MIT) or <http://opensource.org/licenses/MIT>) |
| 103 |
|
| 104 |
at your option. |
| 105 |
|
| 106 |
### Contribution |
| 107 |
|
| 108 |
Unless you explicitly state otherwise, any contribution intentionally |
| 109 |
submitted for inclusion in the work by you, as defined in the Apache-2.0 |
| 110 |
license, shall be dual licensed as above, without any additional terms or |
| 111 |
conditions. |
| 112 |
|
| 113 |
## Development |
| 114 |
|
| 115 |
- Cut a new release by creating a GitHub release with tag. Crate will be built and uploaded to crates.io by GHA. |