diff --git a/Cargo.lock b/Cargo.lock index 9bb4eba7..450d44fb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -187,7 +187,7 @@ dependencies = [ "proc-macro-error", "proc-macro2", "quote", - "syn", + "syn 1.0.99", ] [[package]] @@ -634,7 +634,7 @@ dependencies = [ "proc-macro-error-attr", "proc-macro2", "quote", - "syn", + "syn 1.0.99", "version_check", ] @@ -651,18 +651,18 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.43" +version = "1.0.81" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0a2ca2c61bc9f3d74d2886294ab7b9853abd9c1ad903a3ac7815c58989bb7bab" +checksum = "3d1597b0c024618f09a9c3b8655b7e430397a36d23fdafec26d6965e9eec3eba" dependencies = [ "unicode-ident", ] [[package]] name = "quote" -version = "1.0.21" +version = "1.0.36" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" dependencies = [ "proc-macro2", ] @@ -800,7 +800,7 @@ dependencies = [ "proc-macro2", "quote", "serde_derive_internals", - "syn", + "syn 1.0.99", ] [[package]] @@ -811,22 +811,22 @@ checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" [[package]] name = "serde" -version = "1.0.145" +version = "1.0.198" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "728eb6351430bccb993660dfffc5a72f91ccc1295abaa8ce19b27ebe4f75568b" +checksum = "9846a40c979031340571da2545a4e5b7c4163bdae79b301d5f86d03979451fcc" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.145" +version = "1.0.198" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "81fa1584d3d1bcacd84c277a0dfe21f5b0f6accf4a23d04d4c6d61f1af522b4c" +checksum = "e88edab869b01783ba905e7d0153f9fc1a6505a96e4ad3018011eedb838566d9" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.60", ] [[package]] @@ -837,7 +837,7 @@ checksum = "85bf8229e7920a9f636479437026331ce11aa132b4dde37d121944a44d6e5f3c" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.99", ] [[package]] @@ -891,6 +891,7 @@ dependencies = [ "normalize-line-endings", "os_pipe", "regex", + "serde", "serde_json", "similar", "snapbox-macros", @@ -924,6 +925,17 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "syn" +version = "2.0.60" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "909518bc7b1c9b779f1bbf07f2929d35af9f0f37e47c6e9ef7f9dddc1e1821f3" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + [[package]] name = "tempfile" version = "3.3.0" diff --git a/crates/snapbox/Cargo.toml b/crates/snapbox/Cargo.toml index 954330c3..a12a73e5 100644 --- a/crates/snapbox/Cargo.toml +++ b/crates/snapbox/Cargo.toml @@ -46,7 +46,7 @@ examples = ["dep:escargot"] regex = ["dep:regex"] ## Snapshotting of json -json = ["structured-data", "dep:serde_json"] +json = ["structured-data", "dep:serde_json", "dep:serde"] ## Snapshotting of term styling term-svg = ["structured-data", "dep:anstyle-svg"] ## Snapshotting of structured data @@ -97,6 +97,7 @@ document-features = { version = "0.2.6", optional = true } serde_json = { version = "1.0.85", optional = true} anstyle-svg = { version = "0.1.3", optional = true } regex = { version = "1.10.4", optional = true, default-features = false, features = ["std"] } +serde = { version = "1.0.198", optional = true } [target.'cfg(windows)'.dependencies] windows-sys = { version = "0.52.0", features = ["Win32_Foundation"], optional = true } diff --git a/crates/snapbox/src/data/mod.rs b/crates/snapbox/src/data/mod.rs index c41f1e4a..bc67e650 100644 --- a/crates/snapbox/src/data/mod.rs +++ b/crates/snapbox/src/data/mod.rs @@ -42,6 +42,35 @@ impl ToDebug for D { } } +/// Capture the serde representation of a value +/// +/// ```rust,no_run +/// use snapbox::IntoJson as _; +/// +/// fn some_function() -> usize { +/// // ... +/// # 5 +/// } +/// +/// let actual = some_function(); +/// let expected = snapbox::str![["5"]]; +/// snapbox::assert_eq(actual.into_json(), expected); +/// ``` +#[cfg(feature = "json")] +pub trait IntoJson { + fn into_json(self) -> Data; +} + +#[cfg(feature = "json")] +impl IntoJson for S { + fn into_json(self) -> Data { + match serde_json::to_value(self) { + Ok(value) => Data::json(value), + Err(err) => Data::error(err.to_string(), DataFormat::Json), + } + } +} + /// Convert to [`Data`] with modifiers for `expected` data pub trait IntoData: Sized { /// Remove default [`filters`][crate::filters] from this `expected` result @@ -215,7 +244,9 @@ pub(crate) enum DataInner { /// See also /// - [`str!`] for inline snapshots /// - [`file!`] for external snapshots +/// - [`ToString`] for verifying a `Display` representation /// - [`ToDebug`] for verifying a debug representation +/// - [`IntoJson`] for verifying the serde representation /// - [`IntoData`] for modifying `expected` impl Data { /// Mark the data as binary (no post-processing) diff --git a/crates/snapbox/src/lib.rs b/crates/snapbox/src/lib.rs index ef6f6ce8..4dcbeecb 100644 --- a/crates/snapbox/src/lib.rs +++ b/crates/snapbox/src/lib.rs @@ -113,6 +113,8 @@ pub use action::DEFAULT_ACTION_ENV; pub use assert::Assert; pub use data::Data; pub use data::IntoData; +#[cfg(feature = "json")] +pub use data::IntoJson; pub use data::ToDebug; pub use error::Error; pub use filters::Redactions; @@ -123,6 +125,8 @@ pub type Result = std::result::Result; /// Easier access to common traits pub mod prelude { pub use crate::IntoData; + #[cfg(feature = "json")] + pub use crate::IntoJson; pub use crate::ToDebug; }