Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(data): Add IntoJson #320

Merged
merged 1 commit into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading