diff --git a/crates/snapbox/src/assert/mod.rs b/crates/snapbox/src/assert/mod.rs index 3bb46d22..460bd933 100644 --- a/crates/snapbox/src/assert/mod.rs +++ b/crates/snapbox/src/assert/mod.rs @@ -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 { @@ -42,6 +42,42 @@ impl Assert { Default::default() } + /// Check if a value is the same as an expected value + /// + /// By default [`filters`][crate::filter] 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, expected: impl Into) { + 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. @@ -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, actual: impl Into) { let expected = expected.into(); let actual = actual.into(); @@ -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, actual: impl Into) { let pattern = pattern.into(); let actual = actual.into(); diff --git a/crates/snapbox/src/cmd.rs b/crates/snapbox/src/cmd.rs index e9b82664..f84c4dc7 100644 --- a/crates/snapbox/src/cmd.rs +++ b/crates/snapbox/src/cmd.rs @@ -586,6 +586,48 @@ impl OutputAssert { self } + /// Ensure the command wrote the expected data to `stdout`. + /// + /// By default [`filters`][crate::filter] 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) -> Self { + let expected = expected.into(); + self.stdout_eq_inner(expected) + } + /// Ensure the command wrote the expected data to `stdout`. /// /// ```rust,no_run @@ -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) -> Self { let expected = expected.into(); self.stdout_eq_inner(expected.raw()) @@ -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) -> Self { let expected = expected.into(); self.stdout_eq_inner(expected) @@ -658,6 +708,48 @@ impl OutputAssert { self } + /// Ensure the command wrote the expected data to `stderr`. + /// + /// By default [`filters`][crate::filter] 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) -> Self { + let expected = expected.into(); + self.stderr_eq_inner(expected) + } + /// Ensure the command wrote the expected data to `stderr`. /// /// ```rust,no_run @@ -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) -> Self { let expected = expected.into(); self.stderr_eq_inner(expected.raw()) @@ -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) -> Self { let expected = expected.into(); self.stderr_eq_inner(expected) diff --git a/crates/snapbox/src/data/mod.rs b/crates/snapbox/src/data/mod.rs index 7701313e..5056c777 100644 --- a/crates/snapbox/src/data/mod.rs +++ b/crates/snapbox/src/data/mod.rs @@ -184,8 +184,8 @@ impl Data { } } - /// Remove default [`filters`][crate::filters] from this `expected` result - pub(crate) fn raw(mut self) -> Self { + /// Remove default [`filters`][crate::filter] from this `expected` result + pub fn raw(mut self) -> Self { self.filters = FilterSet::empty().newlines(); self } diff --git a/crates/snapbox/src/data/source.rs b/crates/snapbox/src/data/source.rs index b48873c1..15dd77ea 100644 --- a/crates/snapbox/src/data/source.rs +++ b/crates/snapbox/src/data/source.rs @@ -112,6 +112,13 @@ impl Inline { data.coerce_to(format) } + /// Remove default [`filters`][crate::filter] 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') {