1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
/// Allows simple unit testing of proc macro implementations.
///
/// This macro only works with functions taking [`proc_macro2::TokenStream`] due
/// to the [`proc_macro`] api not being available in unit tests. This can be
/// achieved either by manually creating a seperate function:
/// ```ignore
/// use proc_macro::TokenStream;
/// use proc_macro2::TokenStream as TokenStream2;
/// #[proc_macro]
/// pub fn actual_macro(input: TokenStream) -> TokenStream {
/// macro_impl(input.into()).into()
/// }
/// fn macro_impl(input: TokenStream2) -> TokenStream2 {
/// // ...
/// }
/// ```
/// or use a crate like [`manyhow`](https://docs.rs/manyhow/):
/// ```ignore
/// use proc_macro2::TokenStream as TokenStream2;
/// #[manyhow(impl_fn)] // generates `fn actual_macro_impl`
/// pub fn actual_macro(input: TokenStream2) -> TokenStream2 {
/// // ...
/// }
/// ```
///
/// # Function like macros
/// ```
/// # use proc_macro_utils::assert_expansion;
/// # use proc_macro2::TokenStream;
/// # use quote::quote;
/// // Dummy attribute macro impl
/// fn macro_impl(input: TokenStream) -> TokenStream {
/// quote!(#input)
/// }
/// fn macro_impl_result(input: TokenStream) -> Result<TokenStream, ()> {
/// Ok(quote!(#input))
/// }
/// assert_expansion!(
/// macro_impl!(something test),
/// { something test }
/// );
/// assert_expansion!(
/// macro_impl![1, 2, 3],
/// { 1, 2, 3 }
/// );
/// assert_expansion!(
/// macro_impl!{ braced },
/// { braced }
/// );
/// // adding a single function call (without arguments) is also allowed e.g. `unwrap()`
/// assert_expansion!(
/// macro_impl_result!(result).unwrap(),
/// { result }
/// );
/// ```
///
/// # Derive macros
/// ```
/// # use proc_macro_utils::assert_expansion;
/// # use proc_macro2::TokenStream;
/// # use quote::quote;
/// // Dummy derive macro impl
/// fn macro_impl(item: TokenStream) -> TokenStream {
/// quote!(#item)
/// }
/// fn macro_impl_result(item: TokenStream) -> Result<TokenStream, ()> {
/// Ok(quote!(#item))
/// }
/// assert_expansion!(
/// #[derive(macro_impl)]
/// struct A; // the comma after items is optional
/// { struct A; }
/// );
/// assert_expansion!(
/// #[derive(macro_impl)]
/// struct A {}
/// { struct A {} }
/// );
/// // adding a single function call (without arguments) is also allowed e.g. `unwrap()`
/// assert_expansion!(
/// #[derive(macro_impl_result)]
/// struct A {}.unwrap()
/// { struct A {} }
/// );
/// // alternatively the proc_macro syntax is compatible
/// assert_expansion!(
/// macro_impl!{ struct A {} },
/// { struct A {} }
/// );
/// ```
///
/// # Attribute macros
/// ```
/// # use proc_macro_utils::assert_expansion;
/// # use proc_macro2::TokenStream;
/// # use quote::quote;
/// // Dummy attribute macro impl
/// fn macro_impl(input: TokenStream, item: TokenStream) -> TokenStream {
/// quote!(#input, #item)
/// }
/// fn macro_impl_result(input: TokenStream, item: TokenStream) -> Result<TokenStream, ()> {
/// Ok(quote!(#input, #item))
/// }
/// assert_expansion!(
/// #[macro_impl]
/// struct A;
/// { , struct A; }
/// );
/// assert_expansion!(
/// #[macro_impl = "hello"]
/// fn test() { }, // the comma after items is optional
/// { "hello", fn test() {} }
/// );
/// assert_expansion!(
/// #[macro_impl(a = 10)]
/// impl Hello for World {},
/// { a = 10, impl Hello for World {} }
/// );
/// // adding a single function call (without arguments) is also allowed e.g. `unwrap()`
/// assert_expansion!(
/// #[macro_impl_result(a = 10)]
/// impl Hello for World {}.unwrap(),
/// { a = 10, impl Hello for World {} }
/// );
/// ```
///
/// # Generic usage
/// On top of the normal macro inputs a generic input is also supported.
/// ```
/// # use proc_macro_utils::assert_expansion;
/// # use proc_macro2::TokenStream;
/// # use quote::quote;
/// fn macro_impl(first: TokenStream, second: TokenStream, third: TokenStream) -> TokenStream {
/// quote!(#first, #second, #third)
/// }
/// fn macro_impl_result(first: TokenStream, second: TokenStream, third: TokenStream) -> Result<TokenStream, ()> {
/// Ok(quote!(#first, #second, #third))
/// }
/// assert_expansion!(
/// macro_impl({ 1 }, { something }, { ":)" }),
/// { 1, something, ":)" }
/// );
/// // adding a single function call (without arguments) is also allowed e.g. `unwrap()`
/// assert_expansion!(
/// macro_impl_result({ 1 }, { something }, { ":)" }).unwrap(),
/// { 1, something, ":)" }
/// );
/// ```
#[macro_export]
#[allow(clippy::module_name_repetitions)]
macro_rules! assert_expansion {
($macro:ident!($($input:tt)*)$(.$fn:ident())?, { $($rhs:tt)* }) => {
$crate::assert_expansion!($macro({$($input)*})$(.$fn())?, { $($rhs)* })
};
($macro:ident![$($input:tt)*]$(.$fn:ident())?, { $($rhs:tt)* }) => {
$crate::assert_expansion!($macro({$($input)*})$(.$fn())?, { $($rhs)* })
};
($macro:ident!{$($input:tt)*}$(.$fn:ident())?, { $($rhs:tt)* }) => {
$crate::assert_expansion!($macro({$($input)*})$(.$fn())?, { $($rhs)* })
};
(#[derive($macro:ident)]$item:item$(.$fn:ident())?$(,)? { $($rhs:tt)* }) => {
$crate::assert_expansion!($macro({$item})$(.$fn())?, { $($rhs)* })
};
(#[$macro:ident]$item:item$(.$fn:ident())?$(,)? { $($rhs:tt)* }) => {
$crate::assert_expansion!($macro({}, {$item})$(.$fn())?, { $($rhs)* })
};
(#[$macro:ident = $input:expr]$item:item$(.$fn:ident())?$(,)? { $($rhs:tt)* }) => {
$crate::assert_expansion!($macro({$input}, {$item})$(.$fn())?, { $($rhs)* })
};
(#[$macro:ident($($input:tt)*)]$item:item$(.$fn:ident())?$(,)? { $($rhs:tt)* }) => {
$crate::assert_expansion!($macro({$($input)*}, {$item})$(.$fn())?, { $($rhs)* })
};
($macro:ident($({$($input:tt)*}),+$(,)?)$(.$fn:ident())?, {$($rhs:tt)*}) => {
$crate::assert_tokens!(
$crate::__private::proc_macro2::TokenStream::from($macro(
$(<$crate::__private::proc_macro2::TokenStream as ::core::str::FromStr>
::from_str(::core::stringify!($($input)*)).unwrap().into()),+
)$(.$fn())?),
{ $($rhs)* }
)
};
}
/// Asserts that the `lhs` matches the tokens wrapped in braces on the `rhs`.
///
/// `lhs` needs to be an expression implementing `IntoIterator<Item=TokenTree>`
/// e.g. [`TokenStream`](proc_macro2::TokenStream) or
/// [`TokenParser`](crate::TokenParser).
/// ```
/// # use quote::quote;
/// # use proc_macro_utils::assert_tokens;
/// let some_tokens = quote! { ident, { group } };
///
/// assert_tokens! {some_tokens, { ident, {group} }};
/// ```
#[macro_export]
#[cfg(doc)]
macro_rules! assert_tokens {
($lhs:expr, { $($rhs:tt)* }) => {};
}
#[macro_export]
#[cfg(not(doc))]
#[doc(hidden)]
#[allow(clippy::module_name_repetitions)]
macro_rules! assert_tokens {
($lhs:expr, {$($rhs:tt)*}) => {{
let mut lhs = $crate::TokenParser::new_generic::<3, _, _>($lhs);
$crate::assert_tokens!(@O lhs, "", $($rhs)*);
}};
(@E $prefix:expr, $expected:tt, $found:tt) => {
panic!("expected\n {}\nfound\n {}\nat\n {} {}", stringify!$expected, $found, $prefix, $found);
};
(@E $prefix:expr, $expected:tt) => {
panic!("unexpected end, expected\n {}\nafter\n {}", stringify!$expected, $prefix);
};
(@G $lhs:ident, $fn:ident, $aggr:expr, $sym:literal, $group:tt, {$($inner:tt)*}, $($rhs:tt)*) => {
if let Some(lhs) = $lhs.$fn() {
let mut lhs = $crate::TokenParser::<_, 3>::from($crate::__private::proc_macro2::Group::stream(&lhs));
$crate::assert_tokens!(@O lhs, concat!($aggr, ' ', $sym), $($inner)*);
} else if let Some(lhs) = $lhs.next() {
$crate::assert_tokens!(@E $aggr, ($group), lhs);
} else {
$crate::assert_tokens!(@E $aggr, ($group));
}
$crate::assert_tokens!(@O $lhs, $crate::assert_tokens!(@C $aggr, $group), $($rhs)*);
};
// These don't add a whitespace in front
(@C $lhs:expr, ,) => {
concat!($lhs, ',')
};
(@C $lhs:expr, :) => {
concat!($lhs, ':')
};
(@C $lhs:expr, ;) => {
concat!($lhs, ';')
};
(@C $lhs:expr, .) => {
concat!($lhs, '.')
};
// All other tokens do
(@C $lhs:expr, $rhs:tt) => {
concat!($lhs, ' ', stringify!($rhs))
};
(@O $lhs:ident, $aggr:expr,) => { assert!($lhs.is_empty(), "unexpected left over tokens `{}`", $lhs.into_token_stream()); };
(@O $lhs:ident, $aggr:expr, ( $($inner:tt)* ) $($rhs:tt)*) => {
$crate::assert_tokens!(@G $lhs, next_parenthesized, $aggr, '(', { $($inner)* }, { $($inner)* }, $($rhs)*);
};
(@O $lhs:ident, $aggr:expr, { $($inner:tt)* } $($rhs:tt)*) => {
$crate::assert_tokens!(@G $lhs, next_braced, $aggr, '{', { $($inner)* }, { $($inner)* }, $($rhs)*);
};
(@O $lhs:ident, $aggr:expr, [ $($inner:tt)* ] $($rhs:tt)*) => {
$crate::assert_tokens!(@G $lhs, next_bracketed, $aggr, '[', [ $($inner)* ], { $($inner)* }, $($rhs)*);
};
(@O $lhs:ident, $aggr:expr, $token:tt $($rhs:tt)*) => {
if let Some(lhs) = $lhs.next_punctuation_group().map(|t|t.to_string()).or_else(|| $lhs.next().map(|t|t.to_string())) {
if(lhs != stringify!($token)) {
$crate::assert_tokens!(@E $aggr, ($token), lhs);
}
} else {
$crate::assert_tokens!(@E $aggr, ($token));
}
$crate::assert_tokens!(@O $lhs, $crate::assert_tokens!(@C $aggr, $token), $($rhs)*);
};
}
#[test]
fn test() {
// TODO testing with quote is incomplete `":::"` can be joint joint alone if
// produced directly not with quote.
use quote::quote;
assert_tokens!(quote!(ident ident, { group/test, vec![a, (a + b)] }, "literal" $), {
ident ident, { group /test, vec![a,(a+b)] }, "literal" $
});
assert_tokens!(quote!(:::), {
:::
});
assert_tokens!(quote!(more:::test::test:: hello :-D $$$ It should just work), {
more ::: test ::test:: hello :-D $$$ It should just work
});
}