Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
max-sixty committed Sep 18, 2023
1 parent cd7735e commit 9048af9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
41 changes: 27 additions & 14 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,16 +216,16 @@ macro_rules! assert_compact_json_snapshot {
#[doc(hidden)]
#[macro_export]
macro_rules! _assert_serialized_snapshot {
// Convert redactions expressions to a function call for inline snapshots,
// pass to `_assert_snapshot_base`
// If there's an inline snapshot, convert redactions expressions to a
// function call and pass to `_assert_snapshot_base`
(format=$format:ident, $value:expr, {$($k:expr => $v:expr),*$(,)?}, @$snapshot:literal) => {{
let transform = |value| {
let (_, value) = $crate::_prepare_snapshot_for_redaction!(value, {$($k => $v),*}, $format, Inline);
value
};
$crate::_assert_snapshot_base!(transform=transform, $value, stringify!($value), @$snapshot);
}};
// Add a auto-generated name if there's no name
// If there's no name, add a auto-generated name, call self
(format=$format:ident, $value:expr, {$($k:expr => $v:expr),*$(,)?}) => {{
$crate::_assert_serialized_snapshot!(format=$format, $crate::_macro_support::AutoName, $value, {$($k => $v),*});
}};
Expand All @@ -237,14 +237,25 @@ macro_rules! _assert_serialized_snapshot {
};
$crate::_assert_snapshot_base!(transform=transform, $name, $value, stringify!($value));
}};
// Capture serialization function and pass to `_assert_snapshot_base`
(format=$format:ident, $($arg:tt)*) => {{
// If there's an inline snapshot, capture serialization function and pass to
// `_assert_snapshot_base`, specifying `Inline`
(format=$format:ident $(, $arg:expr)*, @$snapshot:literal $(,)?) => {{
let transform = |value| {$crate::_macro_support::serialize_value(
&value,
$crate::_macro_support::SerializationFormat::$format,
$crate::_macro_support::SnapshotLocation::Inline
)};
$crate::_assert_snapshot_base!(transform = transform $(, $arg)*, @$snapshot);
}};
// Capture serialization function and pass to `_assert_snapshot_base`,
// specifing `File`
(format=$format:ident $(, $arg:expr)* $(,)?) => {{
let transform = |value| {$crate::_macro_support::serialize_value(
&value,
$crate::_macro_support::SerializationFormat::$format,
$crate::_macro_support::SnapshotLocation::File
)};
$crate::_assert_snapshot_base!(transform = transform, $($arg)*);
$crate::_assert_snapshot_base!(transform = transform $(, $arg)*);
}};
}

Expand Down Expand Up @@ -299,7 +310,8 @@ macro_rules! assert_debug_snapshot {
#[doc(hidden)]
#[macro_export]
macro_rules! _assert_snapshot_base {
// If there's an inline literal, wrap that in a `ReferenceValue::Inline`.
// If there's an inline literal, wrap that in a `ReferenceValue::Inline`,
// call self.
(transform=$transform:expr, $value:expr, @$snapshot:literal) => {
$crate::_assert_snapshot_base!(
transform = $transform,
Expand All @@ -309,7 +321,8 @@ macro_rules! _assert_snapshot_base {
stringify!($value)
)
};
// If there's an inline literal with a debug_expr, wrap the literal in a `ReferenceValue::Inline`.
// If there's an inline literal with a debug_expr, wrap the literal in a
// `ReferenceValue::Inline`, call self.
(transform=$transform:expr, $value:expr, $debug_expr:expr, @$snapshot:literal) => {
$crate::_assert_snapshot_base!(
transform = $transform,
Expand All @@ -319,11 +332,11 @@ macro_rules! _assert_snapshot_base {
$debug_expr
)
};
// If there's no debug_expr, use the stringified value in its place.
// If there's no debug_expr, use the stringified value, call self.
(transform=$transform:expr, $name:expr, $value:expr) => {
$crate::_assert_snapshot_base!(transform = $transform, $name, $value, stringify!($value))
};
// If there's no name, auto generate the name in its place.
// If there's no name, auto generate the name in its place, call self.
(transform=$transform:expr, $value:expr) => {
$crate::_assert_snapshot_base!(
transform = $transform,
Expand Down Expand Up @@ -362,16 +375,16 @@ macro_rules! assert_display_snapshot {

/// Asserts a string snapshot.
///
/// This is the simplest of all assertion methods. It accepts any value
/// that implements `fmt::Display`.
/// This is the simplest of all assertion methods. It accepts any value that
/// implements `fmt::Display`.
///
/// ```no_run
/// # use insta::*;
/// assert_snapshot!("reference value to snapshot");
/// ```
///
/// Optionally a third argument can be given as expression which will be
/// stringified as debug expression. For more information on this, check out
/// Optionally a third argument can be given as an expression to be stringified
/// as the debug expression. For more information on this, check out
/// https://insta.rs/docs/snapshot-types/.
#[macro_export]
macro_rules! assert_snapshot {
Expand Down
3 changes: 2 additions & 1 deletion src/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub enum SerializationFormat {
JsonCompact,
}

#[derive(Debug)]
pub enum SnapshotLocation {
Inline,
File,
Expand All @@ -42,7 +43,7 @@ pub fn serialize_content(
match format {
SerializationFormat::Yaml => {
let serialized = yaml::to_string(&content);
match location {
match dbg!(location) {
SnapshotLocation::Inline => serialized,
SnapshotLocation::File => serialized[4..].to_string(),
}
Expand Down

0 comments on commit 9048af9

Please sign in to comment.