Skip to content

Commit

Permalink
Merge pull request #320 from epage/into
Browse files Browse the repository at this point in the history
feat(data): Add IntoJson
  • Loading branch information
epage authored May 17, 2024
2 parents 6d07438 + aa9e2aa commit 490dc89
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion crates/snapbox/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ cmd = ["dep:os_pipe", "dep:wait-timeout", "dep:libc", "dep:windows-sys"]
examples = ["dep:escargot"]

## 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
Expand Down Expand Up @@ -96,6 +96,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 }
serde = { version = "1.0.198", optional = true }

[target.'cfg(windows)'.dependencies]
windows-sys = { version = "0.52.0", features = ["Win32_Foundation"], optional = true }
Expand Down
32 changes: 32 additions & 0 deletions crates/snapbox/src/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,37 @@ impl<D: std::fmt::Debug> ToDebug for D {
}
}

/// Capture the serde representation of a value
///
/// # Examples
///
/// ```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<S: serde::Serialize> 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::filter] from this `expected` result
Expand Down Expand Up @@ -189,6 +220,7 @@ pub(crate) enum DataInner {
/// - [`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)
Expand Down
4 changes: 4 additions & 0 deletions crates/snapbox/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ pub use assert::Action;
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 filter::RedactedValue;
pub use filter::Redactions;
Expand All @@ -134,6 +136,8 @@ pub type Error = assert::Error;
/// Easier access to common traits
pub mod prelude {
pub use crate::IntoData;
#[cfg(feature = "json")]
pub use crate::IntoJson;
pub use crate::ToDebug;
}

Expand Down

0 comments on commit 490dc89

Please sign in to comment.