Skip to content

Commit

Permalink
refactor(assert): Re-order params to (actual, expected)
Browse files Browse the repository at this point in the history
Prep for assert-rs#226
  • Loading branch information
epage committed May 17, 2024
1 parent 97b5b88 commit 922b77b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
28 changes: 14 additions & 14 deletions crates/snapbox/src/assert/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ 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.try_eq(expected.raw(), actual, Some(&"In-memory")) {
if let Err(err) = self.try_eq(Some(&"In-memory"), actual, expected.raw()) {
err.panic();
}
}
Expand Down Expand Up @@ -98,16 +98,16 @@ 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.try_eq(pattern, actual, Some(&"In-memory")) {
if let Err(err) = self.try_eq(Some(&"In-memory"), actual, pattern) {
err.panic();
}
}

pub(crate) fn try_eq(
&self,
expected: crate::Data,
actual: crate::Data,
actual_name: Option<&dyn std::fmt::Display>,
actual: crate::Data,
expected: crate::Data,
) -> Result<()> {
if expected.source().is_none() && actual.source().is_some() {
panic!("received `(actual, expected)`, expected `(expected, actual)`");
Expand All @@ -119,15 +119,15 @@ impl Assert {
Action::Ignore | Action::Verify | Action::Overwrite => {}
}

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

self.do_action(expected, actual, actual_name)
self.do_action(actual_name, actual, expected)
}

fn normalize(
&self,
mut expected: crate::Data,
mut actual: crate::Data,
mut expected: crate::Data,
) -> (crate::Data, crate::Data) {
if expected.filters.is_newlines_set() {
expected = FilterNewlines.filter(expected);
Expand All @@ -147,16 +147,16 @@ impl Assert {
actual = FilterRedactions::new(&self.substitutions, &expected).filter(actual);
}

(expected, actual)
(actual, expected)
}

fn do_action(
&self,
expected: crate::Data,
actual: crate::Data,
actual_name: Option<&dyn std::fmt::Display>,
actual: crate::Data,
expected: crate::Data,
) -> Result<()> {
let result = self.try_verify(&expected, &actual, actual_name);
let result = self.try_verify(actual_name, &actual, &expected);
let Err(err) = result else {
return Ok(());
};
Expand Down Expand Up @@ -200,11 +200,11 @@ impl Assert {

fn try_verify(
&self,
expected: &crate::Data,
actual: &crate::Data,
actual_name: Option<&dyn std::fmt::Display>,
actual: &crate::Data,
expected: &crate::Data,
) -> crate::assert::Result<()> {
if expected != actual {
if actual != expected {
let mut buf = String::new();
crate::report::write_diff(
&mut buf,
Expand Down
4 changes: 2 additions & 2 deletions crates/snapbox/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ impl OutputAssert {
#[track_caller]
fn stdout_eq_inner(self, expected: crate::Data) -> Self {
let actual = crate::Data::from(self.output.stdout.as_slice());
if let Err(err) = self.config.try_eq(expected, actual, Some(&"stdout")) {
if let Err(err) = self.config.try_eq(Some(&"stdout"), actual, expected) {
err.panic();
}

Expand Down Expand Up @@ -723,7 +723,7 @@ impl OutputAssert {
#[track_caller]
fn stderr_eq_inner(self, expected: crate::Data) -> Self {
let actual = crate::Data::from(self.output.stderr.as_slice());
if let Err(err) = self.config.try_eq(expected, actual, Some(&"stderr")) {
if let Err(err) = self.config.try_eq(Some(&"stderr"), actual, expected) {
err.panic();
}

Expand Down
2 changes: 1 addition & 1 deletion crates/snapbox/src/harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ where
let actual = (test)(&case.fixture)?;
let actual = actual.to_string();
let actual = crate::Data::text(actual);
config.try_eq(expected.raw(), actual, Some(&case.name))?;
config.try_eq(Some(&case.name), actual, expected.raw())?;
Ok(())
})
.with_ignored_flag(shared_config.action == Action::Ignore)
Expand Down

0 comments on commit 922b77b

Please sign in to comment.