]> git.proxmox.com Git - rustc.git/blob - vendor/syn/tests/macros/mod.rs
New upstream version 1.41.1+dfsg1
[rustc.git] / vendor / syn / tests / macros / mod.rs
1 #[path = "../debug/mod.rs"]
2 pub mod debug;
3
4 use syn;
5 use syn::parse::{Parse, Result};
6
7 #[macro_export]
8 macro_rules! errorf {
9 ($($tt:tt)*) => {{
10 use ::std::io::Write;
11 let stderr = ::std::io::stderr();
12 write!(stderr.lock(), $($tt)*).unwrap();
13 }};
14 }
15
16 #[macro_export]
17 macro_rules! punctuated {
18 ($($e:expr,)+) => {{
19 let mut seq = ::syn::punctuated::Punctuated::new();
20 $(
21 seq.push($e);
22 )+
23 seq
24 }};
25
26 ($($e:expr),+) => {
27 punctuated!($($e,)+)
28 };
29 }
30
31 #[macro_export]
32 macro_rules! snapshot {
33 ($($args:tt)*) => {
34 snapshot_impl!(() $($args)*)
35 };
36 }
37
38 #[macro_export]
39 macro_rules! snapshot_impl {
40 (($expr:ident) as $t:ty, @$snapshot:literal) => {
41 let $expr = crate::macros::Tokens::parse::<$t>($expr).unwrap();
42 let debug = crate::macros::debug::Lite(&$expr);
43 insta::assert_debug_snapshot!(debug, @$snapshot);
44 };
45 (($($expr:tt)*) as $t:ty, @$snapshot:literal) => {{
46 let syntax_tree = crate::macros::Tokens::parse::<$t>($($expr)*).unwrap();
47 let debug = crate::macros::debug::Lite(&syntax_tree);
48 insta::assert_debug_snapshot!(debug, @$snapshot);
49 syntax_tree
50 }};
51 (($($expr:tt)*) , @$snapshot:literal) => {{
52 let syntax_tree = $($expr)*;
53 let debug = crate::macros::debug::Lite(&syntax_tree);
54 insta::assert_debug_snapshot!(debug, @$snapshot);
55 syntax_tree
56 }};
57 (($($expr:tt)*) $next:tt $($rest:tt)*) => {
58 snapshot_impl!(($($expr)* $next) $($rest)*)
59 };
60 }
61
62 pub trait Tokens {
63 fn parse<T: Parse>(self) -> Result<T>;
64 }
65
66 impl<'a> Tokens for &'a str {
67 fn parse<T: Parse>(self) -> Result<T> {
68 syn::parse_str(self)
69 }
70 }
71
72 impl Tokens for proc_macro2::TokenStream {
73 fn parse<T: Parse>(self) -> Result<T> {
74 syn::parse2(self)
75 }
76 }