Skip to content

Commit

Permalink
fix(assert): Merge _eq and _matches via Data::raw
Browse files Browse the repository at this point in the history
With the new `eq_` variants (to not break compatibility), we switched
param order as part of the path to assert-rs#226

Cherry pick 1762764 (assert-rs#291)
  • Loading branch information
epage committed May 17, 2024
1 parent 922b77b commit 0e2b7a8
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 2 deletions.
46 changes: 45 additions & 1 deletion crates/snapbox/src/assert/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub use error::Result;
/// # use snapbox::Assert;
/// # use snapbox::file;
/// let actual = "something";
/// Assert::new().matches(file!["output.txt"], actual);
/// Assert::new().eq_(actual, file!["output.txt"]);
/// ```
#[derive(Clone, Debug)]
pub struct Assert {
Expand All @@ -42,6 +42,42 @@ impl Assert {
Default::default()
}

/// 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::raw] on `expected`.
///
/// # Examples
///
/// ```rust
/// # use snapbox::Assert;
/// let actual = "something";
/// let expected = "so[..]g";
/// Assert::new().eq_(actual, expected);
/// ```
///
/// Can combine this with [`file!`][crate::file]
/// ```rust,no_run
/// # use snapbox::Assert;
/// # use snapbox::file;
/// let actual = "something";
/// Assert::new().eq_(actual, file!["output.txt"]);
/// ```
#[track_caller]
pub fn eq_(&self, actual: impl Into<crate::Data>, expected: impl Into<crate::Data>) {
let expected = expected.into();
let actual = actual.into();
if let Err(err) = self.try_eq(Some(&"In-memory"), actual, expected) {
err.panic();
}
}

/// Check if a value is the same as an expected value
///
/// When the content is text, newlines are normalized.
Expand All @@ -61,6 +97,10 @@ impl Assert {
/// Assert::new().eq(file!["output.txt"], actual);
/// ```
#[track_caller]
#[deprecated(
since = "0.5.11",
note = "Replaced with `Assert::eq_(actual, expected.raw())`"
)]
pub fn eq(&self, expected: impl Into<crate::Data>, actual: impl Into<crate::Data>) {
let expected = expected.into();
let actual = actual.into();
Expand Down Expand Up @@ -95,6 +135,10 @@ impl Assert {
/// Assert::new().matches(file!["output.txt"], actual);
/// ```
#[track_caller]
#[deprecated(
since = "0.5.11",
note = "Replaced with `Assert::eq_(actual, expected)`"
)]
pub fn matches(&self, pattern: impl Into<crate::Data>, actual: impl Into<crate::Data>) {
let pattern = pattern.into();
let actual = actual.into();
Expand Down
100 changes: 100 additions & 0 deletions crates/snapbox/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,48 @@ impl OutputAssert {
self
}

/// Ensure the command wrote the expected data to `stdout`.
///
/// 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::raw] on `expected`.
///
/// # Examples
///
/// ```rust,no_run
/// use snapbox::cmd::Command;
/// use snapbox::cmd::cargo_bin;
///
/// let assert = Command::new(cargo_bin("snap-fixture"))
/// .env("stdout", "hello")
/// .env("stderr", "world")
/// .assert()
/// .stdout_eq_("he[..]o");
/// ```
///
/// Can combine this with [`file!`][crate::file]
/// ```rust,no_run
/// use snapbox::cmd::Command;
/// use snapbox::cmd::cargo_bin;
/// use snapbox::file;
///
/// let assert = Command::new(cargo_bin("snap-fixture"))
/// .env("stdout", "hello")
/// .env("stderr", "world")
/// .assert()
/// .stdout_eq_(file!["stdout.log"]);
/// ```
#[track_caller]
pub fn stdout_eq_(self, expected: impl Into<crate::Data>) -> Self {
let expected = expected.into();
self.stdout_eq_inner(expected)
}

/// Ensure the command wrote the expected data to `stdout`.
///
/// ```rust,no_run
Expand All @@ -612,6 +654,10 @@ impl OutputAssert {
/// .stdout_eq(file!["stdout.log"]);
/// ```
#[track_caller]
#[deprecated(
since = "0.5.11",
note = "Replaced with `OutputAssert::stdout_eq_(expected.raw())`"
)]
pub fn stdout_eq(self, expected: impl Into<crate::Data>) -> Self {
let expected = expected.into();
self.stdout_eq_inner(expected.raw())
Expand Down Expand Up @@ -643,6 +689,10 @@ impl OutputAssert {
/// .stdout_matches(file!["stdout.log"]);
/// ```
#[track_caller]
#[deprecated(
since = "0.5.11",
note = "Replaced with `OutputAssert::stdout_eq_(expected)`"
)]
pub fn stdout_matches(self, expected: impl Into<crate::Data>) -> Self {
let expected = expected.into();
self.stdout_eq_inner(expected)
Expand All @@ -658,6 +708,48 @@ impl OutputAssert {
self
}

/// Ensure the command wrote the expected data to `stderr`.
///
/// 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::raw] on `expected`.
///
/// # Examples
///
/// ```rust,no_run
/// use snapbox::cmd::Command;
/// use snapbox::cmd::cargo_bin;
///
/// let assert = Command::new(cargo_bin("snap-fixture"))
/// .env("stdout", "hello")
/// .env("stderr", "world")
/// .assert()
/// .stderr_eq_("wo[..]d");
/// ```
///
/// Can combine this with [`file!`][crate::file]
/// ```rust,no_run
/// use snapbox::cmd::Command;
/// use snapbox::cmd::cargo_bin;
/// use snapbox::file;
///
/// let assert = Command::new(cargo_bin("snap-fixture"))
/// .env("stdout", "hello")
/// .env("stderr", "world")
/// .assert()
/// .stderr_eq_(file!["stderr.log"]);
/// ```
#[track_caller]
pub fn stderr_eq_(self, expected: impl Into<crate::Data>) -> Self {
let expected = expected.into();
self.stderr_eq_inner(expected)
}

/// Ensure the command wrote the expected data to `stderr`.
///
/// ```rust,no_run
Expand All @@ -684,6 +776,10 @@ impl OutputAssert {
/// .stderr_eq(file!["stderr.log"]);
/// ```
#[track_caller]
#[deprecated(
since = "0.5.11",
note = "Replaced with `OutputAssert::stderr_eq_(expected.raw())`"
)]
pub fn stderr_eq(self, expected: impl Into<crate::Data>) -> Self {
let expected = expected.into();
self.stderr_eq_inner(expected.raw())
Expand Down Expand Up @@ -715,6 +811,10 @@ impl OutputAssert {
/// .stderr_matches(file!["stderr.log"]);
/// ```
#[track_caller]
#[deprecated(
since = "0.5.11",
note = "Replaced with `OutputAssert::stderr_eq_(expected)`"
)]
pub fn stderr_matches(self, expected: impl Into<crate::Data>) -> Self {
let expected = expected.into();
self.stderr_eq_inner(expected)
Expand Down
2 changes: 1 addition & 1 deletion crates/snapbox/src/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ impl Data {
}

/// Remove default [`filters`][crate::filters] from this `expected` result
pub(crate) fn raw(mut self) -> Self {
pub fn raw(mut self) -> Self {
self.filters = FilterSet::empty().newlines();
self
}
Expand Down
7 changes: 7 additions & 0 deletions crates/snapbox/src/data/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ impl Inline {
data.coerce_to(format)
}

/// Remove default [`filters`][crate::filters] from this `expected` result
pub fn raw(self) -> super::Data {
let mut data: super::Data = self.into();
data.filters = super::FilterSet::empty().newlines();
data
}

fn trimmed(&self) -> String {
let mut data = self.data;
if data.contains('\n') {
Expand Down

0 comments on commit 0e2b7a8

Please sign in to comment.