Skip to content

Commit

Permalink
feat(assert): Add assert_data_eq!
Browse files Browse the repository at this point in the history
This leaves us room to evaluate assert-rs#223

Fixes assert-rs#226

Cherry pick 2a1a25f (assert-rs#296)
  • Loading branch information
epage committed May 17, 2024
1 parent 490dc89 commit 87bf050
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
13 changes: 9 additions & 4 deletions crates/snapbox/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
//! ## Getting Started
//!
//! Testing Functions:
//! - [`assert_eq`][crate::assert_eq()] and [`assert_matches`] for reusing diffing / pattern matching for non-snapshot testing
//! - [`assert_data_eq!`] for quick and dirty snapshotting
//! - [`harness::Harness`] for discovering test inputs and asserting against snapshot files:
//!
//! Testing Commands:
Expand All @@ -44,17 +44,17 @@
//!
//! # Examples
//!
//! [`assert_matches`]
//! [`assert_data_eq!`]
//! ```rust
//! snapbox::assert_matches("Hello [..] people!", "Hello many people!");
//! snapbox::assert_data_eq!("Hello many people!", "Hello [..] people!");
//! ```
//!
//! [`Assert`]
//! ```rust,no_run
//! let actual = "...";
//! snapbox::Assert::new()
//! .action_env("SNAPSHOTS")
//! .matches(snapbox::file!["help_output_is_clean.txt"], actual);
//! .eq_(actual, snapbox::file!["help_output_is_clean.txt"]);
//! ```
//!
//! [`harness::Harness`]
Expand Down Expand Up @@ -160,6 +160,10 @@ pub mod prelude {
/// assert_eq(file!["output.txt"], actual);
/// ```
#[track_caller]
#[deprecated(
since = "0.5.11",
note = "Replaced with `assert_data_eq!(actual, expected.raw())`"
)]
pub fn assert_eq(expected: impl Into<crate::Data>, actual: impl Into<crate::Data>) {
Assert::new()
.action_env(assert::DEFAULT_ACTION_ENV)
Expand Down Expand Up @@ -192,6 +196,7 @@ pub fn assert_eq(expected: impl Into<crate::Data>, actual: impl Into<crate::Data
/// assert_matches(file!["output.txt"], actual);
/// ```
#[track_caller]
#[deprecated(since = "0.5.11", note = "Replaced with `assert_data_eq!(actual)`")]
pub fn assert_matches(pattern: impl Into<crate::Data>, actual: impl Into<crate::Data>) {
Assert::new()
.action_env(assert::DEFAULT_ACTION_ENV)
Expand Down
47 changes: 47 additions & 0 deletions crates/snapbox/src/macros.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,50 @@
/// Check if a value is the same as an expected value
///
/// By default [`filters`][crate::filter] are applied, including:
/// - `...` is a line-wildcard when on a line by itself
/// - `[..]` is a character-wildcard when inside a line
/// - `[EXE]` matches `.exe` on Windows
/// - `\` to `/`
/// - Newlines
///
/// To limit this to newline normalization for text, call [`Data::raw`][crate::Data] on `expected`.
///
/// # Effective signature
///
/// ```rust
/// # use snapbox::IntoData;
/// fn assert_data_eq(actual: impl IntoData, expected: impl IntoData) {
/// // ...
/// }
/// ```
///
/// # Examples
///
/// ```rust
/// # use snapbox::assert_data_eq;
/// let output = "something";
/// let expected = "so[..]g";
/// assert_data_eq!(output, expected);
/// ```
///
/// Can combine this with [`file!`]
/// ```rust,no_run
/// # use snapbox::assert_data_eq;
/// # use snapbox::file;
/// let actual = "something";
/// assert_data_eq!(actual, file!["output.txt"]);
/// ```
#[macro_export]
macro_rules! assert_data_eq {
($actual: expr, $expected: expr $(,)?) => {{
let actual = $crate::IntoData::into_data($actual);
let expected = $crate::IntoData::into_data($expected);
$crate::Assert::new()
.action_env($crate::assert::DEFAULT_ACTION_ENV)
.eq(expected, actual);
}};
}

/// Find the directory for your source file
#[doc(hidden)] // forced to be visible in intended location
#[macro_export]
Expand Down

0 comments on commit 87bf050

Please sign in to comment.