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
  • Loading branch information
epage committed Apr 23, 2024
1 parent 0a9e06b commit 2a1a25f
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 20 deletions.
4 changes: 2 additions & 2 deletions crates/snapbox/src/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use filters::FilterSet;
///
/// let actual = some_function();
/// let expected = snapbox::str![["5"]];
/// snapbox::assert_eq(actual.to_debug(), expected);
/// snapbox::assert_data_eq!(actual.to_debug(), expected);
/// ```
pub trait ToDebug {
fn to_debug(&self) -> Data;
Expand All @@ -54,7 +54,7 @@ impl<D: std::fmt::Debug> ToDebug for D {
///
/// let actual = some_function();
/// let expected = snapbox::str![["5"]];
/// snapbox::assert_eq(actual.into_json(), expected);
/// snapbox::assert_data_eq!(actual.into_json(), expected);
/// ```
#[cfg(feature = "json")]
pub trait IntoJson {
Expand Down
14 changes: 7 additions & 7 deletions crates/snapbox/src/data/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,30 +348,30 @@ impl PathRuntime {
#[cfg(test)]
mod tests {
use super::*;
use crate::assert_eq;
use crate::assert_data_eq;
use crate::prelude::*;
use crate::str;

#[test]
fn test_format_patch() {
let patch = format_patch("hello\nworld\n");

assert_eq(
assert_data_eq!(
patch,
str![[r##"
[r#"
hello
world
"#]
"##]],
patch,
);

let patch = format_patch(r"hello\tworld");
assert_eq(str![[r##"[r#"hello\tworld"#]"##]].raw(), patch);
assert_data_eq!(patch, str![[r##"[r#"hello\tworld"#]"##]].raw());

let patch = format_patch("{\"foo\": 42}");
assert_eq(str![[r##"[r#"{"foo": 42}"#]"##]].raw(), patch);
assert_data_eq!(patch, str![[r##"[r#"{"foo": 42}"#]"##]].raw());
}

#[test]
Expand All @@ -380,7 +380,8 @@ world
patchwork.patch(4..7, "zwei");
patchwork.patch(0..3, "один");
patchwork.patch(8..13, "3");
assert_eq(
assert_data_eq!(
patchwork.to_debug(),
str![[r#"
Patchwork {
text: "один zwei 3",
Expand All @@ -402,7 +403,6 @@ Patchwork {
"#]]
.raw(),
patchwork.to_debug(),
);
}

Expand Down
6 changes: 3 additions & 3 deletions crates/snapbox/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
//! ## Getting Started
//!
//! Testing Functions:
//! - [`assert_eq`][crate::assert_eq()] for quick and dirty snapshotting
//! - [`assert_data_eq`] for quick and dirty snapshotting
//!
//! Testing Commands:
//! - [`cmd::Command`]: Process spawning for testing of non-interactive commands
Expand All @@ -44,9 +44,9 @@
//!
//! # Examples
//!
//! [`assert_eq`][crate::assert_eq()]
//! [`assert_data_eq`]
//! ```rust
//! snapbox::assert_eq("Hello [..] people!", "Hello many people!");
//! snapbox::assert_data_eq!("Hello many people!", "Hello [..] people!");
//! ```
//!
//! [`Assert`]
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::filters] 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
16 changes: 8 additions & 8 deletions crates/snapbox/tests/assert.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
use snapbox::assert_eq;
use snapbox::assert_data_eq;
use snapbox::file;
use snapbox::str;

#[test]
fn test_trivial_assert() {
assert_eq(str!["5"], "5");
assert_data_eq!("5", str!["5"]);
}

#[test]
fn smoke_test_indent() {
assert_eq(
assert_data_eq!(
"\
line1
line2
",
str![[r#"
line1
line2
"#]],
"\
line1
line2
",
);
}

#[test]
fn test_expect_file() {
assert_eq(file!["../README.md"], include_str!("../README.md"))
assert_data_eq!(include_str!("../README.md"), file!["../README.md"])
}

0 comments on commit 2a1a25f

Please sign in to comment.