diff --git a/crates/snapbox/src/assert.rs b/crates/snapbox/src/assert.rs index dae21180..bf60cd70 100644 --- a/crates/snapbox/src/assert.rs +++ b/crates/snapbox/src/assert.rs @@ -18,7 +18,7 @@ use crate::Action; /// # use snapbox::Assert; /// # use snapbox::file; /// let actual = "something"; -/// Assert::new().matches(file!["output.txt"], actual); +/// Assert::new().eq(file!["output.txt"], actual); /// ``` #[derive(Clone, Debug)] pub struct Assert { @@ -37,47 +37,20 @@ impl Assert { /// Check if a value is the same as an expected value /// - /// When the content is text, newlines are normalized. - /// - /// ```rust - /// # use snapbox::Assert; - /// let actual = "something"; - /// let expected = "something"; - /// Assert::new().eq(expected, actual); - /// ``` - /// - /// Can combine this with [`file!`][crate::file] - /// ```rust,no_run - /// # use snapbox::Assert; - /// # use snapbox::file; - /// let actual = "something"; - /// Assert::new().eq(file!["output.txt"], actual); - /// ``` - #[track_caller] - pub fn eq(&self, expected: impl Into, actual: impl Into) { - let expected = expected.into(); - let actual = actual.into(); - if let Err(err) = self.try_eq(expected.raw(), actual, Some(&"In-memory")) { - err.panic(); - } - } - - /// Check if a value matches a pattern - /// - /// Pattern syntax: + /// 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 - /// - /// Normalization: - /// - Newlines /// - `\` to `/` + /// - Newlines + /// + /// To limit this to newline normalization for text, call [`Data::raw`][crate::Data::raw] on `expected`. /// /// ```rust /// # use snapbox::Assert; /// let actual = "something"; /// let expected = "so[..]g"; - /// Assert::new().matches(expected, actual); + /// Assert::new().eq(expected, actual); /// ``` /// /// Can combine this with [`file!`][crate::file] @@ -85,13 +58,13 @@ impl Assert { /// # use snapbox::Assert; /// # use snapbox::file; /// let actual = "something"; - /// Assert::new().matches(file!["output.txt"], actual); + /// Assert::new().eq(file!["output.txt"], actual); /// ``` #[track_caller] - pub fn matches(&self, pattern: impl Into, actual: impl Into) { - let pattern = pattern.into(); + pub fn eq(&self, expected: impl Into, actual: impl Into) { + let expected = expected.into(); let actual = actual.into(); - if let Err(err) = self.try_eq(pattern, actual, Some(&"In-memory")) { + if let Err(err) = self.try_eq(expected, actual, Some(&"In-memory")) { err.panic(); } } diff --git a/crates/snapbox/src/cmd.rs b/crates/snapbox/src/cmd.rs index 75d8b45a..1df82fbb 100644 --- a/crates/snapbox/src/cmd.rs +++ b/crates/snapbox/src/cmd.rs @@ -588,6 +588,15 @@ impl OutputAssert { /// 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`. + /// /// ```rust,no_run /// use snapbox::cmd::Command; /// use snapbox::cmd::cargo_bin; @@ -596,7 +605,7 @@ impl OutputAssert { /// .env("stdout", "hello") /// .env("stderr", "world") /// .assert() - /// .stdout_eq("hello"); + /// .stdout_eq("he[..]o"); /// ``` /// /// Can combine this with [`file!`][crate::file] @@ -619,47 +628,6 @@ 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.raw(), actual, Some(&"stdout")) { - err.panic(); - } - - self - } - - /// Ensure the command wrote the expected data to `stdout`. - /// - /// ```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_matches("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_matches(file!["stdout.log"]); - /// ``` - #[track_caller] - pub fn stdout_matches(self, expected: impl Into) -> Self { - let expected = expected.into(); - self.stdout_matches_inner(expected) - } - - #[track_caller] - fn stdout_matches_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")) { err.panic(); @@ -670,6 +638,15 @@ impl OutputAssert { /// 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`. + /// /// ```rust,no_run /// use snapbox::cmd::Command; /// use snapbox::cmd::cargo_bin; @@ -678,7 +655,7 @@ impl OutputAssert { /// .env("stdout", "hello") /// .env("stderr", "world") /// .assert() - /// .stderr_eq("world"); + /// .stderr_eq("wo[..]d"); /// ``` /// /// Can combine this with [`file!`][crate::file] @@ -701,47 +678,6 @@ 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.raw(), actual, Some(&"stderr")) { - err.panic(); - } - - self - } - - /// Ensure the command wrote the expected data to `stderr`. - /// - /// ```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_matches("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_matches(file!["stderr.log"]); - /// ``` - #[track_caller] - pub fn stderr_matches(self, expected: impl Into) -> Self { - let expected = expected.into(); - self.stderr_matches_inner(expected) - } - - #[track_caller] - fn stderr_matches_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")) { err.panic(); diff --git a/crates/snapbox/src/data/mod.rs b/crates/snapbox/src/data/mod.rs index 234c1253..6120acea 100644 --- a/crates/snapbox/src/data/mod.rs +++ b/crates/snapbox/src/data/mod.rs @@ -183,7 +183,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 } diff --git a/crates/snapbox/src/data/runtime.rs b/crates/snapbox/src/data/runtime.rs index d4f53d05..a0208da4 100644 --- a/crates/snapbox/src/data/runtime.rs +++ b/crates/snapbox/src/data/runtime.rs @@ -368,10 +368,10 @@ world ); let patch = format_patch(r"hello\tworld"); - assert_eq(str![[r##"[r#"hello\tworld"#]"##]], patch); + assert_eq(str![[r##"[r#"hello\tworld"#]"##]].raw(), patch); let patch = format_patch("{\"foo\": 42}"); - assert_eq(str![[r##"[r#"{"foo": 42}"#]"##]], patch); + assert_eq(str![[r##"[r#"{"foo": 42}"#]"##]].raw(), patch); } #[test] @@ -400,7 +400,8 @@ Patchwork { ], } -"#]], +"#]] + .raw(), patchwork.to_debug(), ); } diff --git a/crates/snapbox/src/data/source.rs b/crates/snapbox/src/data/source.rs index 066ec7d5..26ad94c7 100644 --- a/crates/snapbox/src/data/source.rs +++ b/crates/snapbox/src/data/source.rs @@ -97,6 +97,13 @@ impl Inline { data.is(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') { diff --git a/crates/snapbox/src/lib.rs b/crates/snapbox/src/lib.rs index cea2b307..d9248896 100644 --- a/crates/snapbox/src/lib.rs +++ b/crates/snapbox/src/lib.rs @@ -25,7 +25,7 @@ //! ## Getting Started //! //! Testing Functions: -//! - [`assert_eq`][crate::assert_eq()] and [`assert_matches`] for reusing diffing / pattern matching for non-snapshot testing +//! - [`assert_eq`][crate::assert_eq()] for quick and dirty snapshotting //! - [`harness::Harness`] for discovering test inputs and asserting against snapshot files: //! //! Testing Commands: @@ -44,9 +44,9 @@ //! //! # Examples //! -//! [`assert_matches`] +//! [`assert_eq`][crate::assert_eq()] //! ```rust -//! snapbox::assert_matches("Hello [..] people!", "Hello many people!"); +//! snapbox::assert_eq("Hello [..] people!", "Hello many people!"); //! ``` //! //! [`Assert`] @@ -54,7 +54,7 @@ //! let actual = "..."; //! snapbox::Assert::new() //! .action_env("SNAPSHOTS") -//! .matches(snapbox::file!["help_output_is_clean.txt"], actual); +//! .eq(snapbox::file!["help_output_is_clean.txt"], actual); //! ``` //! //! [`harness::Harness`] @@ -121,12 +121,19 @@ pub type Result = std::result::Result; /// Check if a value is the same as an expected value /// -/// When the content is text, newlines are normalized. +/// 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 = "something"; +/// let expected = "so[..]g"; /// assert_eq(expected, output); /// ``` /// @@ -144,38 +151,6 @@ pub fn assert_eq(expected: impl Into, actual: impl Into, actual: impl Into) { - Assert::new() - .action_env(DEFAULT_ACTION_ENV) - .matches(pattern, actual); -} - /// Check if a path matches the content of another path, recursively /// /// When the content is text, newlines are normalized.