| 1 |
#![allow(unused_macros, unused_macro_rules)] |
| 2 |
|
| 3 |
use std::str::FromStr; |
| 4 |
use syn::parse::Result; |
| 5 |
|
| 6 |
macro_rules! snapshot { |
| 7 |
($($args:tt)*) => { |
| 8 |
snapshot_impl!(() $($args)*) |
| 9 |
}; |
| 10 |
} |
| 11 |
|
| 12 |
macro_rules! snapshot_impl { |
| 13 |
(($expr:ident) as $t:ty, @$snapshot:literal) => { |
| 14 |
let tokens = crate::snapshot::TryIntoTokens::try_into_tokens($expr).unwrap(); |
| 15 |
let $expr: $t = syn::parse_quote!(#tokens); |
| 16 |
let debug = crate::debug::Lite(&$expr); |
| 17 |
if !cfg!(miri) { |
| 18 |
#[allow(clippy::needless_raw_string_hashes)] // https://github.com/mitsuhiko/insta/issues/389 |
| 19 |
{ |
| 20 |
insta::assert_debug_snapshot!(debug, @$snapshot); |
| 21 |
} |
| 22 |
} |
| 23 |
}; |
| 24 |
(($($expr:tt)*) as $t:ty, @$snapshot:literal) => {{ |
| 25 |
let tokens = crate::snapshot::TryIntoTokens::try_into_tokens($($expr)*).unwrap(); |
| 26 |
let syntax_tree: $t = syn::parse_quote!(#tokens); |
| 27 |
let debug = crate::debug::Lite(&syntax_tree); |
| 28 |
if !cfg!(miri) { |
| 29 |
#[allow(clippy::needless_raw_string_hashes)] |
| 30 |
{ |
| 31 |
insta::assert_debug_snapshot!(debug, @$snapshot); |
| 32 |
} |
| 33 |
} |
| 34 |
syntax_tree |
| 35 |
}}; |
| 36 |
(($($expr:tt)*) , @$snapshot:literal) => {{ |
| 37 |
let syntax_tree = $($expr)*; |
| 38 |
let debug = crate::debug::Lite(&syntax_tree); |
| 39 |
if !cfg!(miri) { |
| 40 |
#[allow(clippy::needless_raw_string_hashes)] |
| 41 |
{ |
| 42 |
insta::assert_debug_snapshot!(debug, @$snapshot); |
| 43 |
} |
| 44 |
} |
| 45 |
syntax_tree |
| 46 |
}}; |
| 47 |
(($($expr:tt)*) $next:tt $($rest:tt)*) => { |
| 48 |
snapshot_impl!(($($expr)* $next) $($rest)*) |
| 49 |
}; |
| 50 |
} |
| 51 |
|
| 52 |
pub trait TryIntoTokens { |
| 53 |
#[allow(dead_code)] |
| 54 |
fn try_into_tokens(self) -> Result<proc_macro2::TokenStream>; |
| 55 |
} |
| 56 |
|
| 57 |
impl TryIntoTokens for &str { |
| 58 |
fn try_into_tokens(self) -> Result<proc_macro2::TokenStream> { |
| 59 |
let tokens = proc_macro2::TokenStream::from_str(self)?; |
| 60 |
Ok(tokens) |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
impl TryIntoTokens for proc_macro2::TokenStream { |
| 65 |
fn try_into_tokens(self) -> Result<proc_macro2::TokenStream> { |
| 66 |
Ok(self) |
| 67 |
} |
| 68 |
} |