#[doc(hidden)]
#[macro_export]
macro_rules! _function_name {
() => {{
fn f() {}
fn type_name_of_val<T>(_: T) -> &'static str {
std::any::type_name::<T>()
}
let mut name = type_name_of_val(f).strip_suffix("::f").unwrap_or("");
while let Some(rest) = name.strip_suffix("::{{closure}}") {
name = rest;
}
name
}};
}
#[cfg(feature = "csv")]
#[cfg_attr(docsrs, doc(cfg(feature = "csv")))]
#[macro_export]
macro_rules! assert_csv_snapshot {
($($arg:tt)*) => {
$crate::_assert_serialized_snapshot!(format=Csv, $($arg)*);
};
}
#[cfg(feature = "toml")]
#[cfg_attr(docsrs, doc(cfg(feature = "toml")))]
#[macro_export]
macro_rules! assert_toml_snapshot {
($($arg:tt)*) => {
$crate::_assert_serialized_snapshot!(format=Toml, $($arg)*);
};
}
#[cfg_attr(feature = "redactions", doc = " ```no_run")]
#[cfg_attr(not(feature = "redactions"), doc = " ```ignore")]
#[cfg(feature = "yaml")]
#[cfg_attr(docsrs, doc(cfg(feature = "yaml")))]
#[macro_export]
macro_rules! assert_yaml_snapshot {
($($arg:tt)*) => {
$crate::_assert_serialized_snapshot!(format=Yaml, $($arg)*);
};
}
#[cfg(feature = "ron")]
#[cfg_attr(docsrs, doc(cfg(feature = "ron")))]
#[macro_export]
macro_rules! assert_ron_snapshot {
($($arg:tt)*) => {
$crate::_assert_serialized_snapshot!(format=Ron, $($arg)*);
};
}
#[cfg(feature = "json")]
#[cfg_attr(docsrs, doc(cfg(feature = "json")))]
#[macro_export]
macro_rules! assert_json_snapshot {
($($arg:tt)*) => {
$crate::_assert_serialized_snapshot!(format=Json, $($arg)*);
};
}
#[cfg(feature = "json")]
#[cfg_attr(docsrs, doc(cfg(feature = "json")))]
#[macro_export]
macro_rules! assert_compact_json_snapshot {
($($arg:tt)*) => {
$crate::_assert_serialized_snapshot!(format=JsonCompact, $($arg)*);
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! _assert_serialized_snapshot {
(format=$format:ident, $value:expr, $(match ..)? {$($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, @$snapshot);
}};
(format=$format:ident, $value:expr, $(match ..)? {$($k:expr => $v:expr),* $(,)?} $(,)?) => {{
$crate::_assert_serialized_snapshot!(format=$format, $crate::_macro_support::AutoName, $value, {$($k => $v),*});
}};
(format=$format:ident, $name:expr, $value:expr, $(match ..)? {$($k:expr => $v:expr),* $(,)?} $(,)?) => {{
let transform = |value| {
let (_, value) = $crate::_prepare_snapshot_for_redaction!(value, {$($k => $v),*}, $format, File);
value
};
$crate::_assert_snapshot_base!(transform=transform, $name, $value);
}};
(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);
}};
(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),*);
}};
}
#[cfg(feature = "redactions")]
#[doc(hidden)]
#[macro_export]
macro_rules! _prepare_snapshot_for_redaction {
($value:expr, {$($k:expr => $v:expr),*}, $format:ident, $location:ident) => {
{
let vec = std::vec![
$((
$crate::_macro_support::Selector::parse($k).unwrap(),
$crate::_macro_support::Redaction::from($v)
),)*
];
let value = $crate::_macro_support::serialize_value_redacted(
&$value,
&vec,
$crate::_macro_support::SerializationFormat::$format,
$crate::_macro_support::SnapshotLocation::$location
);
(vec, value)
}
}
}
#[cfg(not(feature = "redactions"))]
#[doc(hidden)]
#[macro_export]
macro_rules! _prepare_snapshot_for_redaction {
($value:expr, {$($k:expr => $v:expr),*}, $format:ident, $location:ident) => {
compile_error!("insta was compiled without redaction support.");
};
}
#[macro_export]
macro_rules! assert_debug_snapshot {
($($arg:tt)*) => {
$crate::_assert_snapshot_base!(transform=|v| std::format!("{:#?}", v), $($arg)*)
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! _assert_snapshot_base {
(transform=$transform:expr, $($arg:expr),*, @$snapshot:literal $(,)?) => {
$crate::_assert_snapshot_base!(
transform = $transform,
#[allow(clippy::needless_raw_string_hashes)]
$crate::_macro_support::ReferenceValue::Inline($snapshot),
$($arg),*
)
};
(transform=$transform:expr, $name:expr, $value:expr $(,)?) => {
$crate::_assert_snapshot_base!(transform = $transform, $name, $value, stringify!($value))
};
(transform=$transform:expr, $value:expr $(,)?) => {
$crate::_assert_snapshot_base!(
transform = $transform,
$crate::_macro_support::AutoName,
$value
)
};
(transform=$transform:expr, $name:expr, $value:expr, $debug_expr:expr $(,)?) => {
$crate::_macro_support::assert_snapshot(
$name.into(),
#[allow(clippy::redundant_closure_call)]
&$transform(&$value),
env!("CARGO_MANIFEST_DIR"),
$crate::_function_name!(),
module_path!(),
file!(),
line!(),
$debug_expr,
)
.unwrap()
};
}
#[macro_export]
#[deprecated = "use assert_snapshot!() instead"]
macro_rules! assert_display_snapshot {
($($arg:tt)*) => {
$crate::assert_snapshot!($($arg)*)
};
}
#[macro_export]
macro_rules! assert_snapshot {
($($arg:tt)*) => {
$crate::_assert_snapshot_base!(transform=|v| std::format!("{}", v), $($arg)*)
};
}
#[macro_export]
macro_rules! with_settings {
({$($k:ident => $v:expr),*$(,)?}, $body:block) => {{
let mut settings = $crate::Settings::clone_current();
$(
settings._private_inner_mut().$k($v);
)*
settings.bind(|| $body)
}}
}
#[cfg(feature = "glob")]
#[cfg_attr(docsrs, doc(cfg(feature = "glob")))]
#[macro_export]
macro_rules! glob {
($base_path:expr, $glob:expr, $closure:expr) => {{
use std::path::Path;
let base = $crate::_macro_support::get_cargo_workspace(env!("CARGO_MANIFEST_DIR"))
.join(Path::new(file!()).parent().unwrap())
.join($base_path)
.to_path_buf();
let base = base.canonicalize().unwrap_or_else(|_| base);
$crate::_macro_support::glob_exec(env!("CARGO_MANIFEST_DIR"), &base, $glob, $closure);
}};
($glob:expr, $closure:expr) => {{
insta::glob!(".", $glob, $closure)
}};
}
#[macro_export]
macro_rules! allow_duplicates {
($($x:tt)*) => {
$crate::_macro_support::with_allow_duplicates(|| {
$($x)*
})
}
}