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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#![deny(unused_must_use)]
extern crate proc_macro;
#[macro_use]
extern crate syn;
#[macro_use]
extern crate quote;
extern crate proc_macro2;
use proc_macro2::{Span, TokenStream};
use std::collections::HashMap;
use syn::parse::{Parse, ParseStream, Result as ParseResult};
use syn::punctuated::Punctuated;
use syn::spanned::Spanned;
use syn::token::Comma;
use syn::{ArgCaptured, FnArg, Ident, ItemFn, Pat};
type Error = syn::parse::Error;
struct TemplateArg {
ident: syn::Ident,
is_pattern: bool,
ignore_fn: Option<syn::Path>,
value: syn::LitStr,
}
impl Parse for TemplateArg {
fn parse(input: ParseStream) -> ParseResult<Self> {
let mut ignore_fn = None;
let ident = input.parse::<syn::Ident>()?;
let is_pattern = if input.peek(syn::token::In) {
let _in = input.parse::<syn::token::In>()?;
true
} else {
let _eq = input.parse::<syn::token::Eq>()?;
false
};
let value = input.parse::<syn::LitStr>()?;
if is_pattern && input.peek(syn::token::If) {
let _if = input.parse::<syn::token::If>()?;
let _not = input.parse::<syn::token::Bang>()?;
ignore_fn = Some(input.parse::<syn::Path>()?);
}
Ok(Self {
ident,
is_pattern,
ignore_fn,
value,
})
}
}
struct FilesTestArgs {
root: String,
args: HashMap<Ident, TemplateArg>,
}
impl Parse for FilesTestArgs {
fn parse(input: ParseStream) -> ParseResult<Self> {
let root = input.parse::<syn::LitStr>()?;
let _comma = input.parse::<syn::token::Comma>()?;
let content;
let _brace_token = braced!(content in input);
let args: Punctuated<TemplateArg, Comma> = content.parse_terminated(TemplateArg::parse)?;
let args = args
.into_pairs()
.map(|p| {
let value = p.into_value();
(value.ident.clone(), value)
})
.collect();
Ok(Self {
root: root.value(),
args,
})
}
}
#[proc_macro_attribute]
#[allow(clippy::needless_pass_by_value)]
pub fn files(
args: proc_macro::TokenStream,
func: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
let mut func_item = parse_macro_input!(func as ItemFn);
let args: FilesTestArgs = parse_macro_input!(args as FilesTestArgs);
let func_name_str = func_item.ident.to_string();
let desc_ident = Ident::new(
&format!("__TEST_{}", func_item.ident),
func_item.ident.span(),
);
let trampoline_func_ident = Ident::new(
&format!("__TEST_TRAMPOLINE_{}", func_item.ident),
func_item.ident.span(),
);
let ignore = handle_common_attrs(&mut func_item);
let root = args.root;
let mut pattern_idx = None;
let mut params: Vec<String> = Vec::new();
let mut invoke_args: Vec<TokenStream> = Vec::new();
let mut ignore_fn = None;
for (idx, arg) in func_item.decl.inputs.iter().enumerate() {
match arg {
FnArg::Captured(ArgCaptured {
pat: Pat::Ident(pat_ident),
ty,
..
}) => {
if let Some(arg) = args.args.get(&pat_ident.ident) {
if arg.is_pattern {
if pattern_idx.is_some() {
return Error::new(arg.ident.span(), "two patterns are not allowed!")
.to_compile_error()
.into();
}
pattern_idx = Some(idx);
ignore_fn = arg.ignore_fn.clone();
}
params.push(arg.value.value());
invoke_args.push(quote! {
::datatest::TakeArg::take(&mut <#ty as ::datatest::DeriveArg>::derive(&paths_arg[#idx]))
})
} else {
return Error::new(pat_ident.span(), "mapping is not defined for the argument")
.to_compile_error()
.into();
}
}
_ => {
return Error::new(
arg.span(),
"unexpected argument; only simple argument types are allowed (`&str`, `String`, `&[u8]`, `Vec<u8>`, `&Path`, etc)",
).to_compile_error().into();
}
}
}
let ignore_func_ref = if let Some(ignore_fn) = ignore_fn {
quote!(Some(#ignore_fn))
} else {
quote!(None)
};
if pattern_idx.is_none() {
return Error::new(
Span::call_site(),
"must have exactly one pattern mapping defined via `pattern in r#\"<regular expression>\"`",
)
.to_compile_error()
.into();
}
let orig_func_name = &func_item.ident;
let output = quote! {
#[test_case]
static #desc_ident: ::datatest::FilesTestDesc = ::datatest::FilesTestDesc {
name: concat!(module_path!(), "::", #func_name_str),
ignore: #ignore,
root: #root,
params: &[#(#params),*],
pattern: #pattern_idx,
ignorefn: #ignore_func_ref,
testfn: #trampoline_func_ident,
};
fn #trampoline_func_ident(paths_arg: &[::std::path::PathBuf]) {
let result = #orig_func_name(#(#invoke_args),*);
datatest::assert_test_result(result);
}
#func_item
};
output.into()
}
fn handle_common_attrs(func: &mut ItemFn) -> bool {
let pos = func
.attrs
.iter()
.position(|attr| attr.path.is_ident("test"));
if let Some(pos) = pos {
func.attrs.remove(pos);
}
let ignore_pos = func
.attrs
.iter()
.position(|attr| attr.path.is_ident("ignore"));
if let Some(pos) = ignore_pos {
func.attrs.remove(pos);
}
ignore_pos.is_some()
}
#[proc_macro_attribute]
#[allow(clippy::needless_pass_by_value)]
pub fn data(
args: proc_macro::TokenStream,
func: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
let mut func_item = parse_macro_input!(func as ItemFn);
let root: syn::LitStr = parse_macro_input!(args as syn::LitStr);
let func_name_str = func_item.ident.to_string();
let desc_ident = Ident::new(
&format!("__TEST_{}", func_item.ident),
func_item.ident.span(),
);
let describe_func_ident = Ident::new(
&format!("__TEST_DESCRIBE_{}", func_item.ident),
func_item.ident.span(),
);
let trampoline_func_ident = Ident::new(
&format!("__TEST_TRAMPOLINE_{}", func_item.ident),
func_item.ident.span(),
);
let ignore = handle_common_attrs(&mut func_item);
let orig_func_ident = &func_item.ident;
let arg = func_item.decl.inputs.iter().next();
let ty = match arg {
Some(FnArg::Captured(ArgCaptured { ty, .. })) => Some(ty),
_ => None,
};
let output = quote! {
#[test_case]
static #desc_ident: ::datatest::DataTestDesc = ::datatest::DataTestDesc {
name: concat!(module_path!(), "::", #func_name_str),
ignore: #ignore,
root: #root,
describefn: #describe_func_ident,
};
fn #trampoline_func_ident(arg: #ty) {
let result = #orig_func_ident(arg);
datatest::assert_test_result(result);
}
fn #describe_func_ident(input: &str) -> Vec<::datatest::DataTestCase> {
::datatest::describe(input, #trampoline_func_ident as fn(#ty))
}
#func_item
};
output.into()
}