diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:18:25 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:18:25 +0000 |
commit | 5363f350887b1e5b5dd21a86f88c8af9d7fea6da (patch) | |
tree | 35ca005eb6e0e9a1ba3bb5dbc033209ad445dc17 /vendor/pretty_assertions | |
parent | Adding debian version 1.66.0+dfsg1-1. (diff) | |
download | rustc-5363f350887b1e5b5dd21a86f88c8af9d7fea6da.tar.xz rustc-5363f350887b1e5b5dd21a86f88c8af9d7fea6da.zip |
Merging upstream version 1.67.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/pretty_assertions')
-rw-r--r-- | vendor/pretty_assertions/.cargo-checksum.json | 2 | ||||
-rw-r--r-- | vendor/pretty_assertions/CHANGELOG.md | 38 | ||||
-rw-r--r-- | vendor/pretty_assertions/Cargo.lock | 19 | ||||
-rw-r--r-- | vendor/pretty_assertions/Cargo.toml | 42 | ||||
-rw-r--r-- | vendor/pretty_assertions/README.md | 32 | ||||
-rw-r--r-- | vendor/pretty_assertions/examples/standard_assertion.rs | 19 | ||||
-rwxr-xr-x | vendor/pretty_assertions/scripts/check | 9 | ||||
-rwxr-xr-x | vendor/pretty_assertions/scripts/install | 6 | ||||
-rwxr-xr-x | vendor/pretty_assertions/scripts/publish | 11 | ||||
-rw-r--r-- | vendor/pretty_assertions/src/lib.rs | 515 | ||||
-rw-r--r-- | vendor/pretty_assertions/src/printer.rs | 164 | ||||
-rw-r--r-- | vendor/pretty_assertions/tests/macros.rs | 392 |
12 files changed, 865 insertions, 384 deletions
diff --git a/vendor/pretty_assertions/.cargo-checksum.json b/vendor/pretty_assertions/.cargo-checksum.json index 74431486b..18577e97d 100644 --- a/vendor/pretty_assertions/.cargo-checksum.json +++ b/vendor/pretty_assertions/.cargo-checksum.json @@ -1 +1 @@ -{"files":{"CHANGELOG.md":"b330969d16e38104c91ad0ff518af97b1b58137f40a6e834f6da3018d35ae988","Cargo.lock":"b154588bf85189f112a7c1506448f8813316772fe17550a847183db274e4a8b3","Cargo.toml":"35d70dee35b4d888e622d0f770231055a8780e95463b32473fda8023ff7ca047","LICENSE-APACHE":"c6596eb7be8581c18be736c846fb9173b69eccf6ef94c5135893ec56bd92ba08","LICENSE-MIT":"8c9612877aacfa1b42f5e80b679e6c3c93a7ba2bb99dfaf6e87e200dca4b4e6c","README.md":"6988c526027f6920587923a870b6b5f6d816bb9114abbd9c41f251af986eb11e","examples/pretty_assertion.png":"a9a43d098b548cd222056e8f7391cc071ccd07372e9c44d10e254c0a683c7562","examples/pretty_assertion.rs":"41d0527d286baebe09149e33a9359c3e30c7fbfddd41995db4facdee291fcd2c","examples/pretty_assertion_v0_6_1.png":"c07a7a8cd56ebeb840ae8095c024957ddac8a1d011288acf7928f79d85b39e3a","examples/standard_assertion.png":"be6f676deb51d06bcb33f36d194c82228d5585d91f58af590d9040012e38a0a8","examples/standard_assertion.rs":"b520df96e468ab2a6f8b692d12a548a5a6dcbb8a7fb02a91aaad06ec7f1f3a60","scripts/check":"f323d7c808c8d2cfd8d57ff00b453a9b7c7c95dc0f1a7dfcf3c4d2ead3243c72","scripts/install":"311dd0a011d7e91f9e7df497b2d3b8d77ab2c77b136b7168ad104c82794c5d69","scripts/publish":"bae69134256065f3cd35478c67873ae99af680fc7d2ce4506bd391beed8e35ae","src/lib.rs":"b9fc488ea96d9aa3a902e0bb10d7ca0f83d817b06bb8735f901044126835506a","src/printer.rs":"10cb18c1451c6101e778f7d8e1a51a1ec0208c0ef06cf67dbe30d4805cf02a45"},"package":"1cab0e7c02cf376875e9335e0ba1da535775beb5450d21e1dffca068818ed98b"}
\ No newline at end of file +{"files":{"Cargo.lock":"dc0a795e41222bca707fa04af294f8f8184a13bf9689a5160fa347c246484560","Cargo.toml":"89a0781c41f5e1da912bb4c3c617fe9eadc2ef4c326eeef9fd629b86071d3886","LICENSE-APACHE":"c6596eb7be8581c18be736c846fb9173b69eccf6ef94c5135893ec56bd92ba08","LICENSE-MIT":"8c9612877aacfa1b42f5e80b679e6c3c93a7ba2bb99dfaf6e87e200dca4b4e6c","README.md":"151448d7f24d09a9615d1d9cf38b50e6f0c2c1d8273a447d88d2b70f37a6617f","examples/pretty_assertion.png":"a9a43d098b548cd222056e8f7391cc071ccd07372e9c44d10e254c0a683c7562","examples/pretty_assertion.rs":"41d0527d286baebe09149e33a9359c3e30c7fbfddd41995db4facdee291fcd2c","examples/pretty_assertion_v0_6_1.png":"c07a7a8cd56ebeb840ae8095c024957ddac8a1d011288acf7928f79d85b39e3a","examples/standard_assertion.png":"be6f676deb51d06bcb33f36d194c82228d5585d91f58af590d9040012e38a0a8","examples/standard_assertion.rs":"70180f493795da15c82c0002c26249ef2a1b9fa81f6d0c9a8e0bf4e09121e041","src/lib.rs":"f2362e53eda818ad2fc970ed9803099f07b6c27622ac2154871549a991cdc3bc","src/printer.rs":"3ea14e1542a47c9909bc06a7b413262a6d86dfd22ca15b220d6f6ea48ad35c8e","tests/macros.rs":"cadb699c6d1bab26bb7ba044b1094fdc8a1f4f670d70dc38b7defea1a4dd41fb"},"package":"a25e9bcb20aa780fd0bb16b72403a9064d6b3f22f026946029acb941a50af755"}
\ No newline at end of file diff --git a/vendor/pretty_assertions/CHANGELOG.md b/vendor/pretty_assertions/CHANGELOG.md deleted file mode 100644 index a5c81b75b..000000000 --- a/vendor/pretty_assertions/CHANGELOG.md +++ /dev/null @@ -1,38 +0,0 @@ -# Unreleased - -# v0.7.2 - -- Fix macro hygiene for expansion in a `no_implicit_prelude` context ([#70](https://github.com/colin-kiegel/rust-pretty-assertions/issues/70), [@tommilligan](https://github.com/tommilligan)) - -# v0.7.1 - -- Fix a bug where multiline changes showed an unhelpful inline diff ([#66](https://github.com/colin-kiegel/rust-pretty-assertions/issues/66), [@tommilligan](https://github.com/tommilligan)) - -# v0.7.0 - -## Changed - -- Move from `difference` to `diff` for calculating diffs. The exact assertion messages generated may differ from previous versions. ([#52](https://github.com/colin-kiegel/rust-pretty-assertions/issues/52), [@tommilligan](https://github.com/tommilligan)) - -For example, the following assertion message from `v0.7.0`: - -![pretty assertion](https://raw.githubusercontent.com/colin-kiegel/rust-pretty-assertions/2d2357ff56d22c51a86b2f1cfe6efcee9f5a8081/examples/pretty_assertion.png) - -Was previously rendered like this in `v0.6.1`: - -![pretty assertion](https://raw.githubusercontent.com/colin-kiegel/rust-pretty-assertions/2d2357ff56d22c51a86b2f1cfe6efcee9f5a8081/examples/pretty_assertion_v0_6_1.png) - -## Added - -- Support for unsized values ([#42](https://github.com/colin-kiegel/rust-pretty-assertions/issues/42), [@stanislav-tkach](https://github.com/stanislav-tkach)) -- Document the `Comparison` struct, which was previously hidden. This can be used to generate a pretty diff of two values without panicking. ([#52](https://github.com/colin-kiegel/rust-pretty-assertions/issues/52), [@tommilligan](https://github.com/tommilligan)) - -## Fixed - -- Fix some unhygenic macro expansions ([#41](https://github.com/colin-kiegel/rust-pretty-assertions/issues/41), [@tommilligan](https://github.com/tommilligan)) - -## Internal - -- Test Windows targets in CI ([#46](https://github.com/colin-kiegel/rust-pretty-assertions/issues/46), [@tommilligan](https://github.com/tommilligan)) -- Bump `ansi_term` version to 0.12 ([#34](https://github.com/colin-kiegel/rust-pretty-assertions/issues/34), [@waywardmonkeys](https://github.com/waywardmonkeys)) -- Code health improvements ([#34](https://github.com/colin-kiegel/rust-pretty-assertions/issues/34), [@waywardmonkeys](https://github.com/waywardmonkeys)) diff --git a/vendor/pretty_assertions/Cargo.lock b/vendor/pretty_assertions/Cargo.lock index 8e433f023..baac97af0 100644 --- a/vendor/pretty_assertions/Cargo.lock +++ b/vendor/pretty_assertions/Cargo.lock @@ -1,15 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. [[package]] -name = "ansi_term" -version = "0.12.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2" -dependencies = [ - "winapi", -] - -[[package]] name = "ctor" version = "0.1.19" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -36,12 +27,12 @@ dependencies = [ [[package]] name = "pretty_assertions" -version = "0.7.2" +version = "1.3.0" dependencies = [ - "ansi_term", "ctor", "diff", "output_vt100", + "yansi", ] [[package]] @@ -100,3 +91,9 @@ name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "yansi" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09041cd90cf85f7f8b2df60c646f853b7f535ce68f85244eb6731cf89fa498ec" diff --git a/vendor/pretty_assertions/Cargo.toml b/vendor/pretty_assertions/Cargo.toml index 3f4660d5a..f8370f7c3 100644 --- a/vendor/pretty_assertions/Cargo.toml +++ b/vendor/pretty_assertions/Cargo.toml @@ -3,34 +3,48 @@ # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies -# to registry (e.g., crates.io) dependencies +# to registry (e.g., crates.io) dependencies. # -# If you believe there's an error in this file please file an -# issue against the rust-lang/cargo repository. If you're -# editing this file be aware that the upstream Cargo.toml -# will likely look very different (and much more reasonable) +# If you are reading this file be aware that the original Cargo.toml +# will likely look very different (and much more reasonable). +# See Cargo.toml.orig for the original contents. [package] edition = "2018" name = "pretty_assertions" -version = "0.7.2" -authors = ["Colin Kiegel <kiegel@gmx.de>", "Florent Fayolle <florent.fayolle69@gmail.com>", "Tom Milligan <code@tommilligan.net>"] +version = "1.3.0" +authors = [ + "Colin Kiegel <kiegel@gmx.de>", + "Florent Fayolle <florent.fayolle69@gmail.com>", + "Tom Milligan <code@tommilligan.net>", +] description = "Overwrite `assert_eq!` and `assert_ne!` with drop-in replacements, adding colorful diffs." documentation = "https://docs.rs/pretty_assertions" readme = "README.md" -keywords = ["assert", "diff", "pretty", "color"] +keywords = [ + "assert", + "diff", + "pretty", + "color", +] categories = ["development-tools"] -license = "MIT/Apache-2.0" -repository = "https://github.com/colin-kiegel/rust-pretty-assertions" -[dependencies.ansi_term] -version = "0.12.1" +license = "MIT OR Apache-2.0" +repository = "https://github.com/rust-pretty-assertions/rust-pretty-assertions" [dependencies.diff] version = "0.1.12" + +[dependencies.yansi] +version = "0.5" + +[features] +alloc = [] +default = ["std"] +std = [] +unstable = [] + [target."cfg(windows)".dependencies.ctor] version = "0.1.9" [target."cfg(windows)".dependencies.output_vt100] version = "0.1.2" -[badges.travis-ci] -repository = "colin-kiegel/rust-pretty-assertions" diff --git a/vendor/pretty_assertions/README.md b/vendor/pretty_assertions/README.md index cc9a6ef74..d9a5739b7 100644 --- a/vendor/pretty_assertions/README.md +++ b/vendor/pretty_assertions/README.md @@ -1,9 +1,13 @@ -[![Build status](https://travis-ci.org/colin-kiegel/rust-pretty-assertions.svg?branch=master)](https://travis-ci.org/colin-kiegel/rust-pretty-assertions) +# Pretty Assertions + [![Latest version](https://img.shields.io/crates/v/pretty-assertions.svg)](https://crates.io/crates/pretty-assertions) -[![All downloads](https://img.shields.io/crates/d/pretty-assertions.svg)](https://crates.io/crates/pretty-assertions) +[![docs.rs](https://img.shields.io/docsrs/pretty_assertions)](https://docs.rs/pretty_assertions) [![Downloads of latest version](https://img.shields.io/crates/dv/pretty-assertions.svg)](https://crates.io/crates/pretty-assertions) +[![All downloads](https://img.shields.io/crates/d/pretty-assertions.svg)](https://crates.io/crates/pretty-assertions) -# Pretty Assertions +Overwrite `assert_eq!` with a drop-in replacement, adding a colorful diff. + +## Usage When writing tests in Rust, you'll probably use `assert_eq!(a, b)` _a lot_. @@ -11,11 +15,11 @@ If such a test fails, it will present all the details of `a` and `b`. But you have to spot the differences yourself, which is not always straightforward, like here: -![standard assertion](https://raw.githubusercontent.com/colin-kiegel/rust-pretty-assertions/2d2357ff56d22c51a86b2f1cfe6efcee9f5a8081/examples/standard_assertion.png) +![standard assertion](https://raw.githubusercontent.com/rust-pretty-assertions/rust-pretty-assertions/2d2357ff56d22c51a86b2f1cfe6efcee9f5a8081/examples/standard_assertion.png) Wouldn't that task be _much_ easier with a colorful diff? -![pretty assertion](https://raw.githubusercontent.com/colin-kiegel/rust-pretty-assertions/2d2357ff56d22c51a86b2f1cfe6efcee9f5a8081/examples/pretty_assertion.png) +![pretty assertion](https://raw.githubusercontent.com/rust-pretty-assertions/rust-pretty-assertions/2d2357ff56d22c51a86b2f1cfe6efcee9f5a8081/examples/pretty_assertion.png) Yep — and you only need **one line of code** to make it happen: @@ -48,6 +52,13 @@ fn main() { </details> +## Semantic Versioning + +The exact output of assertions is **not guaranteed** to be consistent over time, and may change between minor versions. +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. + +This crate adheres to semantic versioning for publically exported crate items, **except** the `private` module, which may change between any version. + ## Tip Specify it as [`[dev-dependencies]`](http://doc.crates.io/specifying-dependencies.html#development-dependencies) @@ -61,7 +72,7 @@ Also add `#[cfg(test)]` to your `use` statements, like this: use pretty_assertions::{assert_eq, assert_ne}; ``` -## Note +## Notes - Since `Rust 2018` edition, you need to declare `use pretty_assertions::{assert_eq, assert_ne};` per module. @@ -74,6 +85,15 @@ use pretty_assertions::{assert_eq, assert_ne}; escape sequences, which may break display for certain use cases. - The minimum supported rust version (MSRV) is 1.35.0 +### `no_std` support + +For `no_std` support, disable the `std` feature and enable the `alloc` feature: + +```toml +# Cargo.toml +pretty_assertions = { version= "...", default-features = false, features = ["alloc"] } +``` + ## License Licensed under either of diff --git a/vendor/pretty_assertions/examples/standard_assertion.rs b/vendor/pretty_assertions/examples/standard_assertion.rs index fddd512f4..0a612c82e 100644 --- a/vendor/pretty_assertions/examples/standard_assertion.rs +++ b/vendor/pretty_assertions/examples/standard_assertion.rs @@ -1,11 +1,11 @@ -fn main() { - #[derive(Debug, PartialEq)] - struct Foo { - lorem: &'static str, - ipsum: u32, - dolor: Result<String, String>, - } +#[derive(Debug, PartialEq)] +struct Foo { + lorem: &'static str, + ipsum: u32, + dolor: Result<String, String>, +} +fn compare() { let x = Some(Foo { lorem: "Hello World!", ipsum: 42, @@ -19,3 +19,8 @@ fn main() { assert_eq!(x, y); } + +fn main() { + let result = std::panic::catch_unwind(compare); + assert!(result.is_err(), "example did not panic"); +} diff --git a/vendor/pretty_assertions/scripts/check b/vendor/pretty_assertions/scripts/check deleted file mode 100755 index 43fb3a9b3..000000000 --- a/vendor/pretty_assertions/scripts/check +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/bash - -set -exuo pipefail - -cargo fmt -- --check -cargo audit --deny warnings -cargo clippy --all-targets -- -D warnings -cargo test -cargo doc --no-deps diff --git a/vendor/pretty_assertions/scripts/install b/vendor/pretty_assertions/scripts/install deleted file mode 100755 index 92577640c..000000000 --- a/vendor/pretty_assertions/scripts/install +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/bash - -set -exuo pipefail - -rustup component add rustfmt clippy -cargo install cargo-audit cargo-tarpaulin diff --git a/vendor/pretty_assertions/scripts/publish b/vendor/pretty_assertions/scripts/publish deleted file mode 100755 index e904f03eb..000000000 --- a/vendor/pretty_assertions/scripts/publish +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/bash - -set -euxo pipefail - -# Don't log the cargo login token while authenticating -set +x -echo "cargo login ***********************************" -cargo login "${CARGO_LOGIN_TOKEN}" -set -x - -cargo publish diff --git a/vendor/pretty_assertions/src/lib.rs b/vendor/pretty_assertions/src/lib.rs index 5c1787533..aebe08776 100644 --- a/vendor/pretty_assertions/src/lib.rs +++ b/vendor/pretty_assertions/src/lib.rs @@ -6,11 +6,11 @@ //! But you have to spot the differences yourself, which is not always straightforward, //! like here: //! -//! ![standard assertion](https://raw.githubusercontent.com/colin-kiegel/rust-pretty-assertions/2d2357ff56d22c51a86b2f1cfe6efcee9f5a8081/examples/standard_assertion.png) +//! ![standard assertion](https://raw.githubusercontent.com/rust-pretty-assertions/rust-pretty-assertions/2d2357ff56d22c51a86b2f1cfe6efcee9f5a8081/examples/standard_assertion.png) //! //! Wouldn't that task be _much_ easier with a colorful diff? //! -//! ![pretty assertion](https://raw.githubusercontent.com/colin-kiegel/rust-pretty-assertions/2d2357ff56d22c51a86b2f1cfe6efcee9f5a8081/examples/pretty_assertion.png) +//! ![pretty assertion](https://raw.githubusercontent.com/rust-pretty-assertions/rust-pretty-assertions/2d2357ff56d22c51a86b2f1cfe6efcee9f5a8081/examples/pretty_assertion.png) //! //! Yep — and you only need **one line of code** to make it happen: //! @@ -62,11 +62,25 @@ //! you include. //! * `assert_ne` is also switched to multi-line presentation, but does _not_ show //! a diff. - +//! +//! ## Features +//! +//! Features provided by the crate are: +//! +//! - `std`: Use the Rust standard library. Enabled by default. +//! Exactly one of `std` and `alloc` is required. +//! - `alloc`: Use the `alloc` crate. +//! Exactly one of `std` and `alloc` is required. +//! - `unstable`: opt-in to unstable features that may not follow Semantic Versioning. +//! Implmenetion behind this feature is subject to change without warning between patch versions. + +#![cfg_attr(not(feature = "std"), no_std)] #![deny(clippy::all, missing_docs, unsafe_code)] -pub use ansi_term::Style; -use std::fmt::{self, Debug, Display}; +#[cfg(feature = "alloc")] +#[macro_use] +extern crate alloc; +use core::fmt::{self, Debug, Display}; mod printer; @@ -126,12 +140,82 @@ where } } +/// A comparison of two strings. +/// +/// In contrast to [`Comparison`], which uses the [`core::fmt::Debug`] representation, +/// `StrComparison` uses the string values directly, resulting in multi-line output for multiline strings. +/// +/// ``` +/// use pretty_assertions::StrComparison; +/// +/// print!("{}", StrComparison::new("foo\nbar", "foo\nbaz")); +/// ``` +/// +/// ## Value type bounds +/// +/// Any value that can be referenced as a [`str`] via [`AsRef`] may be used: +/// +/// ``` +/// use pretty_assertions::StrComparison; +/// +/// #[derive(PartialEq)] +/// struct MyString(String); +/// +/// impl AsRef<str> for MyString { +/// fn as_ref(&self) -> &str { +/// &self.0 +/// } +/// } +/// +/// print!( +/// "{}", +/// StrComparison::new( +/// &MyString("foo\nbar".to_owned()), +/// &MyString("foo\nbaz".to_owned()), +/// ), +/// ); +/// ``` +/// +/// The values may have different types, although in practice they are usually the same. +pub struct StrComparison<'a, TLeft, TRight> +where + TLeft: ?Sized, + TRight: ?Sized, +{ + left: &'a TLeft, + right: &'a TRight, +} + +impl<'a, TLeft, TRight> StrComparison<'a, TLeft, TRight> +where + TLeft: AsRef<str> + ?Sized, + TRight: AsRef<str> + ?Sized, +{ + /// Store two values to be compared in future. + /// + /// Expensive diffing is deferred until calling `Debug::fmt`. + pub fn new(left: &'a TLeft, right: &'a TRight) -> StrComparison<'a, TLeft, TRight> { + StrComparison { left, right } + } +} + +impl<'a, TLeft, TRight> Display for StrComparison<'a, TLeft, TRight> +where + TLeft: AsRef<str> + ?Sized, + TRight: AsRef<str> + ?Sized, +{ + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + printer::write_header(f)?; + printer::write_lines(f, self.left.as_ref(), self.right.as_ref()) + } +} + /// Asserts that two expressions are equal to each other (using [`PartialEq`]). /// /// On panic, this macro will print a diff derived from [`Debug`] representation of /// each value. /// -/// This is a drop in replacement for [`std::assert_eq!`]. +/// This is a drop in replacement for [`core::assert_eq!`]. /// You can provide a custom panic message if desired. /// /// # Examples @@ -147,32 +231,70 @@ where /// ``` #[macro_export] macro_rules! assert_eq { - ($left:expr , $right:expr,) => ({ - $crate::assert_eq!($left, $right) + ($left:expr, $right:expr$(,)?) => ({ + $crate::assert_eq!(@ $left, $right, "", ""); + }); + ($left:expr, $right:expr, $($arg:tt)*) => ({ + $crate::assert_eq!(@ $left, $right, ": ", $($arg)+); }); - ($left:expr , $right:expr) => ({ + (@ $left:expr, $right:expr, $maybe_colon:expr, $($arg:tt)*) => ({ match (&($left), &($right)) { (left_val, right_val) => { if !(*left_val == *right_val) { - ::std::panic!("assertion failed: `(left == right)`\ - \n\ - \n{}\ - \n", - $crate::Comparison::new(left_val, right_val)) + use $crate::private::CreateComparison; + ::core::panic!("assertion failed: `(left == right)`{}{}\ + \n\ + \n{}\ + \n", + $maybe_colon, + format_args!($($arg)*), + (left_val, right_val).create_comparison() + ) } } } }); - ($left:expr , $right:expr, $($arg:tt)*) => ({ +} + +/// Asserts that two expressions are equal to each other (using [`PartialEq`]). +/// +/// On panic, this macro will print a diff derived from each value's [`str`] representation. +/// See [`StrComparison`] for further details. +/// +/// This is a drop in replacement for [`core::assert_eq!`]. +/// You can provide a custom panic message if desired. +/// +/// # Examples +/// +/// ``` +/// use pretty_assertions::assert_str_eq; +/// +/// let a = "foo\nbar"; +/// let b = ["foo", "bar"].join("\n"); +/// assert_str_eq!(a, b); +/// +/// assert_str_eq!(a, b, "we are testing concatenation with {} and {}", a, b); +/// ``` +#[macro_export] +macro_rules! assert_str_eq { + ($left:expr, $right:expr$(,)?) => ({ + $crate::assert_str_eq!(@ $left, $right, "", ""); + }); + ($left:expr, $right:expr, $($arg:tt)*) => ({ + $crate::assert_str_eq!(@ $left, $right, ": ", $($arg)+); + }); + (@ $left:expr, $right:expr, $maybe_colon:expr, $($arg:tt)*) => ({ match (&($left), &($right)) { (left_val, right_val) => { if !(*left_val == *right_val) { - ::std::panic!("assertion failed: `(left == right)`: {}\ - \n\ - \n{}\ - \n", - format_args!($($arg)*), - $crate::Comparison::new(left_val, right_val)) + ::core::panic!("assertion failed: `(left == right)`{}{}\ + \n\ + \n{}\ + \n", + $maybe_colon, + format_args!($($arg)*), + $crate::StrComparison::new(left_val, right_val) + ) } } } @@ -184,7 +306,7 @@ macro_rules! assert_eq { /// On panic, this macro will print the values of the expressions with their /// [`Debug`] representations. /// -/// This is a drop in replacement for [`std::assert_ne!`]. +/// This is a drop in replacement for [`core::assert_ne!`]. /// You can provide a custom panic message if desired. /// /// # Examples @@ -200,276 +322,145 @@ macro_rules! assert_eq { /// ``` #[macro_export] macro_rules! assert_ne { - ($left:expr, $right:expr) => ({ - $crate::assert_ne!(@ $left, $right, "", ""); - }); - ($left:expr, $right:expr,) => ({ + ($left:expr, $right:expr$(,)?) => ({ $crate::assert_ne!(@ $left, $right, "", ""); }); ($left:expr, $right:expr, $($arg:tt)+) => ({ $crate::assert_ne!(@ $left, $right, ": ", $($arg)+); }); - (@ $left:expr, $right:expr, $maybe_semicolon:expr, $($arg:tt)+) => ({ + (@ $left:expr, $right:expr, $maybe_colon:expr, $($arg:tt)+) => ({ match (&($left), &($right)) { (left_val, right_val) => { if *left_val == *right_val { - let left_dbg = ::std::format!("{:?}", &*left_val); - let right_dbg = ::std::format!("{:?}", &*right_val); - if left_dbg != right_dbg { - - ::std::panic!("assertion failed: `(left != right)`{}{}\ - \n\ - \n{}\ - \n{}: According to the `PartialEq` implementation, both of the values \ - are partially equivalent, even if the `Debug` outputs differ.\ - \n\ - \n", - $maybe_semicolon, - format_args!($($arg)+), - $crate::Comparison::new(left_val, right_val), - $crate::Style::new() - .bold() - .underline() - .paint("Note")) - } - - ::std::panic!("assertion failed: `(left != right)`{}{}\ + ::core::panic!("assertion failed: `(left != right)`{}{}\ \n\ - \n{}:\ + \nBoth sides:\ \n{:#?}\ \n\ \n", - $maybe_semicolon, - format_args!($($arg)+), - $crate::Style::new().bold().paint("Both sides"), - left_val) + $maybe_colon, + format_args!($($arg)+), + left_val + ) } } } }); } -#[cfg(test)] -#[allow(clippy::eq_op)] -#[no_implicit_prelude] -mod test { - mod assert_eq { - use ::std::string::{String, ToString}; - - #[test] - fn passes() { - let a = "some value"; - crate::assert_eq!(a, a); - } - - #[test] - fn passes_unsized() { - let a: &[u8] = b"e"; - crate::assert_eq!(*a, *a); - } - - #[test] - fn passes_comparable_types() { - let s0: &'static str = "foo"; - let s1: String = "foo".to_string(); - crate::assert_eq!(s0, s1); - } - - #[test] - #[should_panic(expected = r#"assertion failed: `(left == right)` - -[1mDiff[0m [31m< left[0m / [32mright >[0m : -[31m<[0m[1;48;5;52;31m666[0m -[32m>[0m[1;48;5;22;32m999[0m - -"#)] - fn fails() { - crate::assert_eq!(666, 999); - } - - #[test] - #[should_panic(expected = r#"assertion failed: `(left == right)` - -[1mDiff[0m [31m< left[0m / [32mright >[0m : -[31m<[0m[1;48;5;52;31m666[0m -[32m>[0m[1;48;5;22;32m999[0m - -"#)] - fn fails_trailing_comma() { - crate::assert_eq!(666, 999,); +/// Asserts that a value matches a pattern. +/// +/// On panic, this macro will print a diff derived from [`Debug`] representation of +/// the value, and a string representation of the pattern. +/// +/// This is a drop in replacement for [`core::assert_matches::assert_matches!`]. +/// You can provide a custom panic message if desired. +/// +/// # Examples +/// +/// ``` +/// use pretty_assertions::assert_matches; +/// +/// let a = Some(3); +/// assert_matches!(a, Some(_)); +/// +/// assert_matches!(a, Some(value) if value > 2, "we are testing {:?} with a pattern", a); +/// ``` +/// +/// # Features +/// +/// Requires the `unstable` feature to be enabled. +/// +/// **Please note:** implementation under the `unstable` feature may be changed between +/// patch versions without warning. +#[cfg(feature = "unstable")] +#[macro_export] +macro_rules! assert_matches { + ($left:expr, $( $pattern:pat )|+ $( if $guard: expr )? $(,)?) => ({ + match $left { + $( $pattern )|+ $( if $guard )? => {} + ref left_val => { + $crate::assert_matches!( + @ + left_val, + ::core::stringify!($($pattern)|+ $(if $guard)?), + "", + "" + ); + } } - - #[test] - #[should_panic(expected = r#"assertion failed: `(left == right)` - -[1mDiff[0m [31m< left[0m / [32mright >[0m : - [ - 101, -[32m> 101,[0m - ] - -"#)] - fn fails_unsized() { - let a: &[u8] = b"e"; - let b: &[u8] = b"ee"; - crate::assert_eq!(*a, *b); + }); + ($left:expr, $( $pattern:pat )|+ $( if $guard: expr )?, $($arg:tt)+) => ({ + match $left { + $( $pattern )|+ $( if $guard )? => {} + ref left_val => { + $crate::assert_matches!( + @ + left_val, + ::core::stringify!($($pattern)|+ $(if $guard)?), + ": ", + $($arg)+ + ); + } } - #[test] - #[should_panic( - expected = r#"assertion failed: `(left == right)`: custom panic message - -[1mDiff[0m [31m< left[0m / [32mright >[0m : -[31m<[0m[1;48;5;52;31m666[0m -[32m>[0m[1;48;5;22;32m999[0m + }); + (@ $left:expr, $right:expr, $maybe_colon:expr, $($arg:tt)*) => ({ + match (&($left), &($right)) { + (left_val, right_val) => { + // Use the Display implementation to display the pattern, + // as using Debug would add another layer of quotes to the output. + struct Pattern<'a>(&'a str); + impl ::core::fmt::Debug for Pattern<'_> { + fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result { + ::core::fmt::Display::fmt(self.0, f) + } + } -"# - )] - fn fails_custom() { - crate::assert_eq!(666, 999, "custom panic message"); + ::core::panic!("assertion failed: `(left matches right)`{}{}\ + \n\ + \n{}\ + \n", + $maybe_colon, + format_args!($($arg)*), + $crate::Comparison::new(left_val, &Pattern(right_val)) + ) + } } + }); +} - #[test] - #[should_panic( - expected = r#"assertion failed: `(left == right)`: custom panic message +// Not public API. Used by the expansion of this crate's assert macros. +#[doc(hidden)] +pub mod private { + #[cfg(feature = "alloc")] + use alloc::string::String; -[1mDiff[0m [31m< left[0m / [32mright >[0m : -[31m<[0m[1;48;5;52;31m666[0m -[32m>[0m[1;48;5;22;32m999[0m + pub trait CompareAsStrByDefault: AsRef<str> {} + impl CompareAsStrByDefault for str {} + impl CompareAsStrByDefault for String {} + impl<T: CompareAsStrByDefault + ?Sized> CompareAsStrByDefault for &T {} -"# - )] - fn fails_custom_trailing_comma() { - crate::assert_eq!(666, 999, "custom panic message",); - } + pub trait CreateComparison { + type Comparison; + fn create_comparison(self) -> Self::Comparison; } - mod assert_ne { - use ::std::string::{String, ToString}; - - #[test] - fn passes() { - let a = "a"; - let b = "b"; - crate::assert_ne!(a, b); + impl<'a, T, U> CreateComparison for &'a (T, U) { + type Comparison = crate::Comparison<'a, T, U>; + fn create_comparison(self) -> Self::Comparison { + crate::Comparison::new(&self.0, &self.1) } + } - #[test] - fn passes_unsized() { - let a: &[u8] = b"e"; - let b: &[u8] = b"ee"; - crate::assert_ne!(*a, *b); - } - - #[test] - fn passes_comparable_types() { - let s0: &'static str = "foo"; - let s1: String = "bar".to_string(); - crate::assert_ne!(s0, s1); - } - - #[test] - #[should_panic(expected = r#"assertion failed: `(left != right)` - -[1mBoth sides[0m: -666 -"#)] - fn fails() { - crate::assert_ne!(666, 666); - } - - #[test] - #[should_panic(expected = r#"assertion failed: `(left != right)` - -[1mBoth sides[0m: -666 -"#)] - fn fails_trailing_comma() { - crate::assert_ne!(666, 666,); - } - - #[test] - #[should_panic(expected = r#"assertion failed: `(left != right)` - -[1mBoth sides[0m: -[ - 101, -] - -"#)] - fn fails_unsized() { - let a: &[u8] = b"e"; - crate::assert_ne!(*a, *a); - } - - #[test] - #[should_panic( - expected = r#"assertion failed: `(left != right)`: custom panic message - -[1mBoth sides[0m: -666 -"# - )] - fn fails_custom() { - crate::assert_ne!(666, 666, "custom panic message"); - } - - #[test] - #[should_panic( - expected = r#"assertion failed: `(left != right)`: custom panic message - -[1mBoth sides[0m: -666 -"# - )] - fn fails_custom_trailing_comma() { - crate::assert_ne!(666, 666, "custom panic message",); - } - - // If the values are equal but their debug outputs are not - // show a specific warning - - #[test] - #[should_panic(expected = r#"assertion failed: `(left != right)` - -[1mDiff[0m [31m< left[0m / [32mright >[0m : -[31m<[0m[1;48;5;52;31m-[0m[31m0.0[0m -[32m>0.0[0m - -[1;4mNote[0m: According to the `PartialEq` implementation, both of the values are partially equivalent, even if the `Debug` outputs differ. - -"#)] - fn assert_ne_partial() { - // Workaround for https://github.com/rust-lang/rust/issues/47619 - // can be removed, when we require rust 1.25 or higher - struct Foo(f32); - - use ::std::fmt; - impl fmt::Debug for Foo { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - ::std::write!(f, "{:.1?}", self.0) - } - } - - impl ::std::cmp::PartialEq for Foo { - fn eq(&self, other: &Self) -> bool { - self.0 == other.0 - } - } - - crate::assert_ne!(Foo(-0.0), Foo(0.0)); - } - - // Regression tests - - #[test] - #[should_panic] - fn assert_ne_non_empty_return() { - fn not_zero(x: u32) -> u32 { - crate::assert_ne!(x, 0); - x - } - not_zero(0); + impl<'a, T, U> CreateComparison for (&'a T, &'a U) + where + T: CompareAsStrByDefault + ?Sized, + U: CompareAsStrByDefault + ?Sized, + { + type Comparison = crate::StrComparison<'a, T, U>; + fn create_comparison(self) -> Self::Comparison { + crate::StrComparison::new(self.0, self.1) } } } diff --git a/vendor/pretty_assertions/src/printer.rs b/vendor/pretty_assertions/src/printer.rs index 172b1ed6c..24c25f0b7 100644 --- a/vendor/pretty_assertions/src/printer.rs +++ b/vendor/pretty_assertions/src/printer.rs @@ -1,8 +1,10 @@ -use ansi_term::{ - Colour::{Fixed, Green, Red}, +#[cfg(feature = "alloc")] +use alloc::format; +use core::fmt; +use yansi::{ + Color::{Fixed, Green, Red, Unset}, Style, }; -use std::fmt; macro_rules! paint { ($f:expr, $colour:expr, $fmt:expr, $($args:tt)*) => ( @@ -18,7 +20,7 @@ pub(crate) fn write_header(f: &mut fmt::Formatter) -> fmt::Result { writeln!( f, "{} {} / {} :", - Style::new().bold().paint("Diff"), + Style::new(Unset).bold().paint("Diff"), Red.paint(format!("{} left", SIGN_LEFT)), Green.paint(format!("right {}", SIGN_RIGHT)) ) @@ -27,7 +29,7 @@ pub(crate) fn write_header(f: &mut fmt::Formatter) -> fmt::Result { /// Delay formatting this deleted chunk until later. /// /// It can be formatted as a whole chunk by calling `flush`, or the inner value -/// obtained with `take` for further processing. +/// obtained with `take` for further processing (such as an inline diff). #[derive(Default)] struct LatentDeletion<'a> { // The most recent deleted line we've seen @@ -45,7 +47,7 @@ impl<'a> LatentDeletion<'a> { /// Take the underlying chunk value, if it's suitable for inline diffing. /// - /// If there is no value of we've seen more than one line, return `None`. + /// If there is no value or we've seen more than one line, return `None`. fn take(&mut self) -> Option<&'a str> { if self.count == 1 { self.value.take() @@ -98,10 +100,6 @@ pub(crate) fn write_lines<TWrite: fmt::Write>( previous_deletion.flush(f)?; previous_deletion.set(deleted); } - // Underlying diff library should never return this sequence - (::diff::Result::Right(_), Some(::diff::Result::Left(_))) => { - panic!("insertion followed by deletion"); - } // If we're being followed by more insertions, don't inline diff (::diff::Result::Right(inserted), Some(::diff::Result::Right(_))) => { previous_deletion.flush(f)?; @@ -141,7 +139,7 @@ where fn new(f: &'a mut Writer) -> Self { InlineWriter { f, - style: Style::new(), + style: Style::new(Unset), } } @@ -152,10 +150,11 @@ where write!(self.f, "{}", c)?; } else { // Close out previous style - write!(self.f, "{}", self.style.suffix())?; + self.style.fmt_suffix(self.f)?; // Store new style and start writing it - write!(self.f, "{}{}", style.prefix(), c)?; + style.fmt_prefix(self.f)?; + write!(self.f, "{}", c)?; self.style = style; } Ok(()) @@ -164,8 +163,9 @@ where /// Finish any existing style and reset to default state. fn finish(&mut self) -> fmt::Result { // Close out previous style - writeln!(self.f, "{}", self.style.suffix())?; - self.style = Default::default(); + self.style.fmt_suffix(self.f)?; + writeln!(self.f)?; + self.style = Style::new(Unset); Ok(()) } } @@ -180,8 +180,8 @@ fn write_inline_diff<TWrite: fmt::Write>(f: &mut TWrite, left: &str, right: &str let mut writer = InlineWriter::new(f); // Print the left string on one line, with differences highlighted - let light = Red.into(); - let heavy = Red.on(Fixed(52)).bold(); + let light = Style::new(Red); + let heavy = Style::new(Red).bg(Fixed(52)).bold(); writer.write_with_style(&SIGN_LEFT, light)?; for change in diff.iter() { match change { @@ -193,8 +193,8 @@ fn write_inline_diff<TWrite: fmt::Write>(f: &mut TWrite, left: &str, right: &str writer.finish()?; // Print the right string on one line, with differences highlighted - let light = Green.into(); - let heavy = Green.on(Fixed(22)).bold(); + let light = Style::new(Green); + let heavy = Style::new(Green).bg(Fixed(22)).bold(); writer.write_with_style(&SIGN_RIGHT, light)?; for change in diff.iter() { match change { @@ -210,6 +210,9 @@ fn write_inline_diff<TWrite: fmt::Write>(f: &mut TWrite, left: &str, right: &str mod test { use super::*; + #[cfg(feature = "alloc")] + use alloc::string::String; + // ANSI terminal codes used in our outputs. // // Interpolate these into test strings to make expected values easier to read. @@ -230,6 +233,8 @@ mod test { let mut actual = String::new(); printer(&mut actual, left, right).expect("printer function failed"); + // Cannot use IO without stdlib + #[cfg(feature = "std")] println!( "## left ##\n\ {}\n\ @@ -471,4 +476,125 @@ Cabbage"#; check_printer(write_lines, left, right, &expected); } + + mod write_lines_edge_newlines { + use super::*; + + #[test] + fn both_trailing() { + let left = "fan\n"; + let right = "mug\n"; + // Note the additional space at the bottom is caused by a trailing newline + // adding an additional line with zero content to both sides of the diff + let expected = format!( + r#"{red_light}<{reset}{red_heavy}fan{reset} +{green_light}>{reset}{green_heavy}mug{reset} + +"#, + red_light = RED_LIGHT, + red_heavy = RED_HEAVY, + green_light = GREEN_LIGHT, + green_heavy = GREEN_HEAVY, + reset = RESET, + ); + + check_printer(write_lines, left, right, &expected); + } + + #[test] + fn both_leading() { + let left = "\nfan"; + let right = "\nmug"; + // Note the additional space at the top is caused by a leading newline + // adding an additional line with zero content to both sides of the diff + let expected = format!( + r#" +{red_light}<{reset}{red_heavy}fan{reset} +{green_light}>{reset}{green_heavy}mug{reset} +"#, + red_light = RED_LIGHT, + red_heavy = RED_HEAVY, + green_light = GREEN_LIGHT, + green_heavy = GREEN_HEAVY, + reset = RESET, + ); + + check_printer(write_lines, left, right, &expected); + } + + #[test] + fn leading_added() { + let left = "fan"; + let right = "\nmug"; + let expected = format!( + r#"{red_light}<fan{reset} +{green_light}>{reset} +{green_light}>mug{reset} +"#, + red_light = RED_LIGHT, + green_light = GREEN_LIGHT, + reset = RESET, + ); + + check_printer(write_lines, left, right, &expected); + } + + #[test] + fn leading_deleted() { + let left = "\nfan"; + let right = "mug"; + let expected = format!( + r#"{red_light}<{reset} +{red_light}<fan{reset} +{green_light}>mug{reset} +"#, + red_light = RED_LIGHT, + green_light = GREEN_LIGHT, + reset = RESET, + ); + + check_printer(write_lines, left, right, &expected); + } + + #[test] + fn trailing_added() { + let left = "fan"; + let right = "mug\n"; + let expected = format!( + r#"{red_light}<fan{reset} +{green_light}>mug{reset} +{green_light}>{reset} +"#, + red_light = RED_LIGHT, + green_light = GREEN_LIGHT, + reset = RESET, + ); + + check_printer(write_lines, left, right, &expected); + } + + /// Regression test for double abort + /// + /// See: https://github.com/rust-pretty-assertions/rust-pretty-assertions/issues/96 + #[test] + fn trailing_deleted() { + // The below inputs caused an abort via double panic + // we panicked at 'insertion followed by deletion' + let left = "fan\n"; + let right = "mug"; + let expected = format!( + r#"{red_light}<{reset}{red_heavy}fan{reset} +{green_light}>{reset}{green_heavy}mug{reset} +{red_light}<{reset} +"#, + red_light = RED_LIGHT, + red_heavy = RED_HEAVY, + green_light = GREEN_LIGHT, + green_heavy = GREEN_HEAVY, + reset = RESET, + ); + + check_printer(write_lines, left, right, &expected); + } + } } diff --git a/vendor/pretty_assertions/tests/macros.rs b/vendor/pretty_assertions/tests/macros.rs new file mode 100644 index 000000000..0ef251f14 --- /dev/null +++ b/vendor/pretty_assertions/tests/macros.rs @@ -0,0 +1,392 @@ +#![cfg_attr(not(feature = "std"), no_std)] +#![no_implicit_prelude] + +#[cfg(feature = "alloc")] +extern crate alloc; + +#[allow(clippy::eq_op)] +mod assert_str_eq { + use ::core::{cmp::PartialEq, convert::AsRef}; + + #[cfg(feature = "alloc")] + use ::alloc::string::{String, ToString}; + #[cfg(feature = "std")] + use ::std::string::{String, ToString}; + + #[test] + fn passes_str() { + let a = "some value"; + ::pretty_assertions::assert_str_eq!(a, a); + } + + #[test] + fn passes_string() { + let a: String = "some value".to_string(); + ::pretty_assertions::assert_str_eq!(a, a); + } + + #[test] + fn passes_comparable_types() { + let s0: &'static str = "foo"; + let s1: String = "foo".to_string(); + ::pretty_assertions::assert_str_eq!(s0, s1); + } + + #[derive(PartialEq)] + struct MyString(String); + + impl AsRef<str> for MyString { + fn as_ref(&self) -> &str { + &self.0 + } + } + + impl PartialEq<String> for MyString { + fn eq(&self, other: &String) -> bool { + &self.0 == other + } + } + + #[test] + fn passes_as_ref_types() { + let s0 = MyString("foo".to_string()); + let s1 = "foo".to_string(); + ::pretty_assertions::assert_str_eq!(s0, s1); + } + + #[test] + #[should_panic(expected = r#"assertion failed: `(left == right)` + +[1mDiff[0m [31m< left[0m / [32mright >[0m : + foo +[31m<ba[0m[1;48;5;52;31mr[0m +[32m>ba[0m[1;48;5;22;32mz[0m + +"#)] + fn fails_as_ref_types() { + let s0 = MyString("foo\nbar".to_string()); + let s1 = "foo\nbaz".to_string(); + ::pretty_assertions::assert_str_eq!(s0, s1); + } + + #[test] + #[should_panic(expected = r#"assertion failed: `(left == right)` + +[1mDiff[0m [31m< left[0m / [32mright >[0m : + foo +[31m<ba[0m[1;48;5;52;31mr[0m +[32m>ba[0m[1;48;5;22;32mz[0m + +"#)] + fn fails_foo() { + ::pretty_assertions::assert_str_eq!("foo\nbar", "foo\nbaz"); + } +} + +#[allow(clippy::eq_op)] +mod assert_eq { + #[cfg(feature = "alloc")] + use ::alloc::string::{String, ToString}; + #[cfg(feature = "std")] + use ::std::string::{String, ToString}; + + #[test] + fn passes() { + let a = "some value"; + ::pretty_assertions::assert_eq!(a, a); + } + + #[test] + fn passes_unsized() { + let a: &[u8] = b"e"; + ::pretty_assertions::assert_eq!(*a, *a); + } + + #[test] + fn passes_comparable_types() { + let s0: &'static str = "foo"; + let s1: String = "foo".to_string(); + ::pretty_assertions::assert_eq!(s0, s1); + } + + #[test] + #[should_panic(expected = r#"assertion failed: `(left == right)` + +[1mDiff[0m [31m< left[0m / [32mright >[0m : +[31m<[0m[1;48;5;52;31m666[0m +[32m>[0m[1;48;5;22;32m999[0m + +"#)] + fn fails() { + ::pretty_assertions::assert_eq!(666, 999); + } + + #[test] + #[should_panic(expected = r#"assertion failed: `(left == right)` + +[1mDiff[0m [31m< left[0m / [32mright >[0m : +[31m<[0m[1;48;5;52;31m666[0m +[32m>[0m[1;48;5;22;32m999[0m + +"#)] + fn fails_trailing_comma() { + ::pretty_assertions::assert_eq!(666, 999,); + } + + #[test] + #[should_panic(expected = r#"assertion failed: `(left == right)` + +[1mDiff[0m [31m< left[0m / [32mright >[0m : + [ + 101, +[32m> 101,[0m + ] + +"#)] + fn fails_unsized() { + let a: &[u8] = b"e"; + let b: &[u8] = b"ee"; + ::pretty_assertions::assert_eq!(*a, *b); + } + + #[test] + #[should_panic( + expected = r#"assertion failed: `(left == right)`: custom panic message + +[1mDiff[0m [31m< left[0m / [32mright >[0m : +[31m<[0m[1;48;5;52;31m666[0m +[32m>[0m[1;48;5;22;32m999[0m + +"# + )] + fn fails_custom() { + ::pretty_assertions::assert_eq!(666, 999, "custom panic message"); + } + + #[test] + #[should_panic( + expected = r#"assertion failed: `(left == right)`: custom panic message + +[1mDiff[0m [31m< left[0m / [32mright >[0m : +[31m<[0m[1;48;5;52;31m666[0m +[32m>[0m[1;48;5;22;32m999[0m + +"# + )] + fn fails_custom_trailing_comma() { + ::pretty_assertions::assert_eq!(666, 999, "custom panic message",); + } + + #[test] + #[should_panic(expected = r#"assertion failed: `(left == right)` + +[1mDiff[0m [31m< left[0m / [32mright >[0m : + foo +[31m<ba[0m[1;48;5;52;31mr[0m +[32m>ba[0m[1;48;5;22;32mz[0m + +"#)] + fn fails_str() { + ::pretty_assertions::assert_eq!("foo\nbar", "foo\nbaz"); + } + + #[test] + #[should_panic(expected = r#"assertion failed: `(left == right)` + +[1mDiff[0m [31m< left[0m / [32mright >[0m : + foo +[31m<ba[0m[1;48;5;52;31mr[0m +[32m>ba[0m[1;48;5;22;32mz[0m + +"#)] + fn fails_string() { + ::pretty_assertions::assert_eq!("foo\nbar".to_string(), "foo\nbaz".to_string()); + } +} + +mod assert_ne { + #[cfg(feature = "alloc")] + use ::alloc::string::{String, ToString}; + #[cfg(feature = "std")] + use ::std::string::{String, ToString}; + + #[test] + fn passes() { + let a = "a"; + let b = "b"; + ::pretty_assertions::assert_ne!(a, b); + } + + #[test] + fn passes_unsized() { + let a: &[u8] = b"e"; + let b: &[u8] = b"ee"; + ::pretty_assertions::assert_ne!(*a, *b); + } + + #[test] + fn passes_comparable_types() { + let s0: &'static str = "foo"; + let s1: String = "bar".to_string(); + ::pretty_assertions::assert_ne!(s0, s1); + } + + #[test] + #[should_panic(expected = r#"assertion failed: `(left != right)` + +Both sides: +666 +"#)] + fn fails() { + ::pretty_assertions::assert_ne!(666, 666); + } + + #[test] + #[should_panic(expected = r#"assertion failed: `(left != right)` + +Both sides: +666 +"#)] + fn fails_trailing_comma() { + ::pretty_assertions::assert_ne!(666, 666,); + } + + #[test] + #[should_panic(expected = r#"assertion failed: `(left != right)` + +Both sides: +[ + 101, +] + +"#)] + fn fails_unsized() { + let a: &[u8] = b"e"; + ::pretty_assertions::assert_ne!(*a, *a); + } + + #[test] + #[should_panic( + expected = r#"assertion failed: `(left != right)`: custom panic message + +Both sides: +666 +"# + )] + fn fails_custom() { + ::pretty_assertions::assert_ne!(666, 666, "custom panic message"); + } + + #[test] + #[should_panic( + expected = r#"assertion failed: `(left != right)`: custom panic message + +Both sides: +666 +"# + )] + fn fails_custom_trailing_comma() { + ::pretty_assertions::assert_ne!(666, 666, "custom panic message",); + } + + // If the values are equal but their debug outputs are not + // show a specific warning + + // Regression tests + + #[test] + #[should_panic] + fn assert_ne_non_empty_return() { + fn not_zero(x: u32) -> u32 { + ::pretty_assertions::assert_ne!(x, 0); + x + } + not_zero(0); + } +} + +#[cfg(feature = "unstable")] +mod assert_matches { + use ::core::option::Option::{None, Some}; + + #[test] + fn passes() { + let a = Some("some value"); + ::pretty_assertions::assert_matches!(a, Some(_)); + } + + #[test] + fn passes_unsized() { + let a: &[u8] = b"e"; + ::pretty_assertions::assert_matches!(*a, _); + } + + #[test] + #[should_panic(expected = r#"assertion failed: `(left matches right)` + +[1mDiff[0m [31m< left[0m / [32mright >[0m : +[31m<[0m[1;48;5;52;31mN[0m[31mo[0m[1;48;5;52;31mn[0m[31me[0m +[32m>[0m[1;48;5;22;32mS[0m[32mo[0m[1;48;5;22;32mm[0m[32me[0m[1;48;5;22;32m(_)[0m + +"#)] + fn fails() { + ::pretty_assertions::assert_matches!(None::<usize>, Some(_)); + } + + #[test] + #[should_panic(expected = r#"assertion failed: `(left matches right)` + +[1mDiff[0m [31m< left[0m / [32mright >[0m : +[31m<Some([0m +[31m< 3,[0m +[31m<)[0m +[32m>Some(3) if 0 > 0[0m + +"#)] + fn fails_guard() { + ::pretty_assertions::assert_matches!(Some(3), Some(3) if 0 > 0,); + } + + #[test] + #[should_panic(expected = r#"assertion failed: `(left matches right)` + +[1mDiff[0m [31m< left[0m / [32mright >[0m : +[31m<[[0m +[31m< 101,[0m +[31m<][0m +[32m>ref b if b == b"ee"[0m + +"#)] + fn fails_unsized() { + let a: &[u8] = b"e"; + ::pretty_assertions::assert_matches!(*a, ref b if b == b"ee"); + } + + #[test] + #[should_panic( + expected = r#"assertion failed: `(left matches right)`: custom panic message + +[1mDiff[0m [31m< left[0m / [32mright >[0m : +[31m<[0m[1;48;5;52;31m666[0m +[32m>[0m[1;48;5;22;32m999[0m + +"# + )] + fn fails_custom() { + ::pretty_assertions::assert_matches!(666, 999, "custom panic message"); + } + + #[test] + #[should_panic( + expected = r#"assertion failed: `(left matches right)`: custom panic message + +[1mDiff[0m [31m< left[0m / [32mright >[0m : +[31m<[0m[1;48;5;52;31m666[0m +[32m>[0m[1;48;5;22;32m999[0m + +"# + )] + fn fails_custom_trailing_comma() { + ::pretty_assertions::assert_matches!(666, 999, "custom panic message",); + } +} |