Skip to content

feat(assert): Add assert_data_eq! #296

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

Merged
merged 2 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
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
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
38 changes: 3 additions & 35 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 Expand Up @@ -89,38 +89,6 @@ pub mod prelude {
pub use crate::ToDebug;
}

/// Check if a value is the same as an expected value
///
/// By default [`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`] on `expected`.
///
/// ```rust
/// # use snapbox::assert_eq;
/// let output = "something";
/// let expected = "so[..]g";
/// assert_eq(expected, output);
/// ```
///
/// Can combine this with [`file!`]
/// ```rust,no_run
/// # use snapbox::assert_eq;
/// # use snapbox::file;
/// let actual = "something";
/// assert_eq(file!["output.txt"], actual);
/// ```
#[track_caller]
pub fn assert_eq(expected: impl IntoData, actual: impl IntoData) {
Assert::new()
.action_env(assert::DEFAULT_ACTION_ENV)
.eq(expected, actual);
}

/// Check if a path matches the content of another path, recursively
///
/// When the content is text, newlines are normalized.
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"])
}
Loading