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

Add a json_pretty macro #287

Closed
wants to merge 1 commit into from
Closed
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
46 changes: 46 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ macro_rules! assert_ron_snapshot {
}

/// Asserts a `Serialize` snapshot in JSON format.
/// See also [`assert_json_pretty_snapshot`].
///
/// **Feature:** `json` (to be disabled by default)
///
Expand Down Expand Up @@ -258,6 +259,51 @@ macro_rules! assert_json_snapshot {
}};
}

/// Asserts a `Serialize` snapshot in JSON pretty format.
/// See also [`assert_json_snapshot`].
///
/// **Feature:** `json` (to be disabled by default)
///
/// This works exactly like [`assert_yaml_snapshot!`] but serializes in JSON format.
/// This is normally not recommended because it makes diffs less reliable, but it can
/// be useful for certain specialized situations.
///
/// Example:
///
/// ```no_run
/// # use insta::*;
/// assert_json_snapshot!(vec![1, 2, 3]);
/// ```
///
/// The third argument to the macro can be an object expression for redaction.
/// It's in the form `{ selector => replacement }`. For more information
/// about redactions refer to the [redactions feature in the guide](https://insta.rs/docs/redactions/).
///
/// The snapshot name is optional but can be provided as first argument.
#[cfg(feature = "json")]
#[cfg_attr(docsrs, doc(cfg(feature = "json")))]
#[macro_export]
macro_rules! assert_json_pretty_snapshot {
($value:expr, @$snapshot:literal) => {{
$crate::_assert_serialized_snapshot!($value, JsonPretty, @$snapshot);
}};
($value:expr, {$($k:expr => $v:expr),*$(,)?}, @$snapshot:literal) => {{
$crate::_assert_serialized_snapshot!($value, {$($k => $v),*}, JsonPretty, @$snapshot);
}};
($value:expr, {$($k:expr => $v:expr),*$(,)?}) => {{
$crate::_assert_serialized_snapshot!($crate::_macro_support::AutoName, $value, {$($k => $v),*}, JsonPretty);
}};
($name:expr, $value:expr) => {{
$crate::_assert_serialized_snapshot!(Some($name), $value, JsonPretty);
}};
($name:expr, $value:expr, {$($k:expr => $v:expr),*$(,)?}) => {{
$crate::_assert_serialized_snapshot!(Some($name), $value, {$($k => $v),*}, JsonPretty);
}};
($value:expr) => {{
$crate::_assert_serialized_snapshot!($crate::_macro_support::AutoName, $value, JsonPretty);
}};
}

#[doc(hidden)]
#[macro_export]
macro_rules! _assert_serialized_snapshot {
Expand Down
4 changes: 3 additions & 1 deletion src/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub enum SerializationFormat {
Toml,
Yaml,
Json,
JsonPretty,
}

pub enum SnapshotLocation {
Expand Down Expand Up @@ -46,7 +47,8 @@ pub fn serialize_content(
SnapshotLocation::File => serialized[4..].to_string(),
}
}
SerializationFormat::Json => json::to_string_pretty(&content),
SerializationFormat::Json => json::to_string(&content),
SerializationFormat::JsonPretty => json::to_string_pretty(&content),
#[cfg(feature = "csv")]
SerializationFormat::Csv => {
let mut buf = Vec::with_capacity(128);
Expand Down
9 changes: 9 additions & 0 deletions tests/snapshots/test_basic__json_pretty_vector.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
source: tests/test_basic.rs
expression: "vec![1, 2, 3]"
---
[
1,
2,
3
]
6 changes: 1 addition & 5 deletions tests/snapshots/test_basic__json_vector.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,4 @@
source: tests/test_basic.rs
expression: "vec![1, 2, 3]"
---
[
1,
2,
3
]
[1,2,3]
10 changes: 10 additions & 0 deletions tests/snapshots/test_basic__unnamed_json_pretty_vector-2.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
source: tests/test_basic.rs
expression: "vec![1, 2, 3, 4]"
---
[
1,
2,
3,
4
]
11 changes: 11 additions & 0 deletions tests/snapshots/test_basic__unnamed_json_pretty_vector-3.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
source: tests/test_basic.rs
expression: "vec![1, 2, 3, 4, 5]"
---
[
1,
2,
3,
4,
5
]
9 changes: 9 additions & 0 deletions tests/snapshots/test_basic__unnamed_json_pretty_vector.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
source: tests/test_basic.rs
expression: "vec![1, 2, 3]"
---
[
1,
2,
3
]
7 changes: 1 addition & 6 deletions tests/snapshots/test_basic__unnamed_json_vector-2.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,4 @@
source: tests/test_basic.rs
expression: "vec![1, 2, 3, 4]"
---
[
1,
2,
3,
4
]
[1,2,3,4]
8 changes: 1 addition & 7 deletions tests/snapshots/test_basic__unnamed_json_vector-3.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,4 @@
source: tests/test_basic.rs
expression: "vec![1, 2, 3, 4, 5]"
---
[
1,
2,
3,
4,
5
]
[1,2,3,4,5]
6 changes: 1 addition & 5 deletions tests/snapshots/test_basic__unnamed_json_vector.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,4 @@
source: tests/test_basic.rs
expression: "vec![1, 2, 3]"
---
[
1,
2,
3
]
[1,2,3]
18 changes: 16 additions & 2 deletions tests/test_basic.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#[cfg(feature = "json")]
use insta::assert_json_snapshot;
#[cfg(feature = "yaml")]
use insta::assert_yaml_snapshot;
use insta::{assert_debug_snapshot, assert_display_snapshot};
#[cfg(feature = "json")]
use insta::{assert_json_pretty_snapshot, assert_json_snapshot};
use std::fmt;

#[test]
Expand Down Expand Up @@ -55,6 +55,20 @@ fn test_unnamed_json_vector() {
assert_json_snapshot!(vec![1, 2, 3, 4, 5]);
}

#[cfg(feature = "json")]
#[test]
fn test_json_pretty_vector() {
assert_json_pretty_snapshot!("json_pretty_vector", vec![1, 2, 3]);
}

#[cfg(feature = "json")]
#[test]
fn test_unnamed_json_pretty_vector() {
assert_json_pretty_snapshot!(vec![1, 2, 3]);
assert_json_pretty_snapshot!(vec![1, 2, 3, 4]);
assert_json_pretty_snapshot!(vec![1, 2, 3, 4, 5]);
}

mod nested {
#[test]
fn test_nested_module() {
Expand Down
7 changes: 1 addition & 6 deletions tests/test_inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,7 @@ fn test_toml_inline() {
#[cfg(feature = "json")]
#[test]
fn test_json_inline() {
assert_json_snapshot!(vec!["foo", "bar"], @r###"
[
"foo",
"bar"
]
"###);
assert_json_snapshot!(vec!["foo", "bar"], @r###"["foo","bar"]"###);
}

#[cfg(feature = "yaml")]
Expand Down