| 1 |
//! # interpolate_name |
| 2 |
//! |
| 3 |
//! `interpolate_name` consists in a set of procedural macro attributes |
| 4 |
//! geared towards reduce the boilerplate while writing repetitive tests. |
| 5 |
//! |
| 6 |
//! - `interpolate_test`: a quick way to test the same function by passing specific |
| 7 |
//! arguments and have a test entry for each of them. |
| 8 |
//! - `interpolate_name`: a simple function renamer that can be combined |
| 9 |
//! with macros to support more complex patterns. |
| 10 |
|
| 11 |
extern crate proc_macro; |
| 12 |
|
| 13 |
use syn::{parse_macro_input, Attribute, ItemFn}; |
| 14 |
|
| 15 |
use proc_macro2::{Ident, Span, TokenStream, TokenTree}; |
| 16 |
|
| 17 |
use quote::{quote_spanned, ToTokens}; |
| 18 |
|
| 19 |
fn fn_name(item: TokenStream) -> Ident { |
| 20 |
let mut tokens = item.into_iter(); |
| 21 |
let found = tokens |
| 22 |
.find(|tok| { |
| 23 |
if let TokenTree::Ident(word) = tok { |
| 24 |
if word == "fn" { |
| 25 |
true |
| 26 |
} else { |
| 27 |
false |
| 28 |
} |
| 29 |
} else { |
| 30 |
false |
| 31 |
} |
| 32 |
}) |
| 33 |
.is_some(); |
| 34 |
|
| 35 |
if !found { |
| 36 |
panic!("the macro attribute applies only to functions") |
| 37 |
} |
| 38 |
|
| 39 |
match tokens.next() { |
| 40 |
Some(TokenTree::Ident(word)) => word, |
| 41 |
_ => panic!("failed to find function name"), |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
fn fn_attrs_name(item: TokenStream) -> (TokenStream, Ident) { |
| 46 |
let mut tokens = item.into_iter(); |
| 47 |
|
| 48 |
let attrs = tokens |
| 49 |
.by_ref() |
| 50 |
.take_while(|tok| { |
| 51 |
if let TokenTree::Ident(word) = tok { |
| 52 |
if word == "fn" { |
| 53 |
false |
| 54 |
} else { |
| 55 |
true |
| 56 |
} |
| 57 |
} else { |
| 58 |
true |
| 59 |
} |
| 60 |
}) |
| 61 |
.collect::<Vec<_>>(); |
| 62 |
|
| 63 |
let name = match tokens.next() { |
| 64 |
Some(TokenTree::Ident(word)) => word, |
| 65 |
_ => panic!("the macro attribute applies only to functions"), |
| 66 |
}; |
| 67 |
|
| 68 |
(TokenStream::from_iter(attrs.into_iter()), name) |
| 69 |
} |
| 70 |
|
| 71 |
/// Rename the decorated function by appending `_` and the provided `specifier`. |
| 72 |
/// |
| 73 |
/// ``` no_run |
| 74 |
/// use interpolate_name::interpolate_name; |
| 75 |
/// |
| 76 |
/// #[interpolate_name(spec)] |
| 77 |
/// fn foo() {} |
| 78 |
/// ``` |
| 79 |
/// |
| 80 |
/// produces: |
| 81 |
/// ``` no_run |
| 82 |
/// fn foo_spec() {} |
| 83 |
/// ``` |
| 84 |
#[proc_macro_attribute] |
| 85 |
pub fn interpolate_name( |
| 86 |
attr: proc_macro::TokenStream, |
| 87 |
item: proc_macro::TokenStream, |
| 88 |
) -> proc_macro::TokenStream { |
| 89 |
let tokens = TokenStream::from(attr).into_iter().collect::<Vec<_>>(); |
| 90 |
if tokens.len() != 1 { |
| 91 |
panic!("expected #[interpolate_name(specifier)]"); |
| 92 |
} |
| 93 |
|
| 94 |
let specifier = match &tokens[0] { |
| 95 |
TokenTree::Literal(tt) => tt.to_string(), |
| 96 |
TokenTree::Ident(tt) => tt.to_string(), |
| 97 |
_ => panic!("expected #[interpolate_name(specifier)]"), |
| 98 |
}; |
| 99 |
|
| 100 |
let item = TokenStream::from(item); |
| 101 |
let (attrs, name) = fn_attrs_name(item.clone()); |
| 102 |
let interpolated_name = Ident::new( |
| 103 |
&format!("{}_{}", name.to_string(), specifier), |
| 104 |
Span::call_site(), |
| 105 |
); |
| 106 |
|
| 107 |
let ret: TokenStream = quote_spanned! { |
| 108 |
proc_macro2::Span::call_site() => |
| 109 |
#attrs |
| 110 |
fn #interpolated_name() { |
| 111 |
#item |
| 112 |
|
| 113 |
return #name (); |
| 114 |
} |
| 115 |
} |
| 116 |
.into(); |
| 117 |
|
| 118 |
ret.into() |
| 119 |
} |
| 120 |
|
| 121 |
use std::iter::FromIterator; |
| 122 |
|
| 123 |
/// Generate a new test that calls the decorated function with the provided arguments. |
| 124 |
/// |
| 125 |
/// The test function name is the same as the called plus `_` and `specifier`. |
| 126 |
/// Can decorate the same function multiple times. |
| 127 |
/// |
| 128 |
/// ``` no_run |
| 129 |
/// use interpolate_name::interpolate_test; |
| 130 |
/// |
| 131 |
/// #[interpolate_test(some, "some", "arguments", 1)] |
| 132 |
/// #[interpolate_test(name, "other", "arguments", 42)] |
| 133 |
/// fn foo(a: &str, b: &str, c: usize) { |
| 134 |
/// println!("{} {} {}", a, b,c); |
| 135 |
/// } |
| 136 |
/// ``` |
| 137 |
/// |
| 138 |
/// produces: |
| 139 |
/// ``` no_run |
| 140 |
/// #[test] |
| 141 |
/// fn foo_some() { foo("some", "arguments", 1); } |
| 142 |
/// #[test] |
| 143 |
/// fn foo_name() { foo("other", "arguments", 42); } |
| 144 |
/// ``` |
| 145 |
#[proc_macro_attribute] |
| 146 |
pub fn interpolate_test( |
| 147 |
attr: proc_macro::TokenStream, |
| 148 |
item: proc_macro::TokenStream, |
| 149 |
) -> proc_macro::TokenStream { |
| 150 |
let tokens = TokenStream::from(attr).into_iter().collect::<Vec<_>>(); |
| 151 |
if tokens.len() < 3 { |
| 152 |
panic!("expected #[interpolate_test(specifier, arguments)]"); |
| 153 |
} |
| 154 |
|
| 155 |
let specifier = match &tokens[0] { |
| 156 |
TokenTree::Literal(tt) => tt.to_string(), |
| 157 |
TokenTree::Ident(tt) => tt.to_string(), |
| 158 |
_ => panic!("expected #[interpolate_test(specifier, arguments)]"), |
| 159 |
}; |
| 160 |
|
| 161 |
match &tokens[1] { |
| 162 |
TokenTree::Punct(p) if p.as_char() == ',' => {} |
| 163 |
_ => panic!("expected #[interpolate_test(specifier, arguments)]"), |
| 164 |
} |
| 165 |
|
| 166 |
let args = TokenStream::from_iter(tokens.into_iter().skip(2)); |
| 167 |
|
| 168 |
let mut syn_item = parse_macro_input!(item as ItemFn); |
| 169 |
let (mut interpolate_attrs, attrs): (Vec<Attribute>, Vec<Attribute>) = |
| 170 |
syn_item.attrs.iter().cloned().partition(|attr| { |
| 171 |
let found = if let Some(seg) = attr.path().segments.last() { |
| 172 |
seg.ident == "interpolate_test" |
| 173 |
} else { |
| 174 |
false |
| 175 |
}; |
| 176 |
found |
| 177 |
}); |
| 178 |
let append_attrs = !interpolate_attrs.is_empty(); |
| 179 |
if append_attrs { |
| 180 |
interpolate_attrs.extend(attrs.iter().cloned()); |
| 181 |
} |
| 182 |
syn_item.attrs = interpolate_attrs; |
| 183 |
let attrs = TokenStream::from_iter(attrs.iter().map(|it| it.into_token_stream())); |
| 184 |
let item = TokenStream::from(syn_item.into_token_stream()); |
| 185 |
let name = fn_name(item.clone()); |
| 186 |
let interpolated_name = Ident::new( |
| 187 |
&format!("{}_{}", name.to_string(), specifier), |
| 188 |
Span::call_site(), |
| 189 |
); |
| 190 |
|
| 191 |
let ret: TokenStream = quote_spanned! { |
| 192 |
proc_macro2::Span::call_site() => |
| 193 |
#item |
| 194 |
|
| 195 |
#[test] |
| 196 |
#attrs |
| 197 |
fn #interpolated_name() { |
| 198 |
return #name (#args); |
| 199 |
} |
| 200 |
} |
| 201 |
.into(); |
| 202 |
|
| 203 |
ret.into() |
| 204 |
} |