Skip to content

Commit

Permalink
refactor(snap): Generalize inner checks
Browse files Browse the repository at this point in the history
Cherry pick 3aaeaea (assert-rs#276)
  • Loading branch information
epage committed May 16, 2024
1 parent 29d8572 commit 9548044
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions crates/snapbox/src/assert/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,17 @@ impl Assert {
pub fn eq(&self, expected: impl Into<crate::Data>, actual: impl Into<crate::Data>) {
let expected = expected.into();
let actual = actual.into();
if let Err(err) = self.eq_inner(expected, actual) {
if let Err(err) = self.eq_inner(expected, actual, Some(&"In-memory")) {
err.panic();
}
}

fn eq_inner(&self, expected: crate::Data, actual: crate::Data) -> Result<()> {
fn eq_inner(
&self,
expected: crate::Data,
actual: crate::Data,
actual_name: Option<&dyn std::fmt::Display>,
) -> Result<()> {
if expected.source().is_none() && actual.source().is_some() {
panic!("received `(actual, expected)`, expected `(expected, actual)`");
}
Expand All @@ -82,7 +87,7 @@ impl Assert {

let (expected, actual) = self.normalize_eq(expected, actual);

self.do_action(expected, actual, Some(&"In-memory"))
self.do_action(expected, actual, actual_name)
}

/// Check if a value matches a pattern
Expand Down Expand Up @@ -114,12 +119,17 @@ impl Assert {
pub fn matches(&self, pattern: impl Into<crate::Data>, actual: impl Into<crate::Data>) {
let pattern = pattern.into();
let actual = actual.into();
if let Err(err) = self.matches_inner(pattern, actual) {
if let Err(err) = self.matches_inner(pattern, actual, Some(&"In-memory")) {
err.panic();
}
}

fn matches_inner(&self, pattern: crate::Data, actual: crate::Data) -> Result<()> {
fn matches_inner(
&self,
pattern: crate::Data,
actual: crate::Data,
actual_name: Option<&dyn std::fmt::Display>,
) -> Result<()> {
if pattern.source().is_none() && actual.source().is_some() {
panic!("received `(actual, expected)`, expected `(expected, actual)`");
}
Expand All @@ -132,7 +142,7 @@ impl Assert {

let (expected, actual) = self.normalize_match(pattern, actual);

self.do_action(expected, actual, Some(&"In-memory"))
self.do_action(expected, actual, actual_name)
}

pub(crate) fn normalize_eq(
Expand Down

0 comments on commit 9548044

Please sign in to comment.