@@ -9,7 +9,7 @@ use syn::parse::{Parse, ParseStream, Result as ParseResult};
9
9
use syn:: punctuated:: Punctuated ;
10
10
use syn:: spanned:: Spanned ;
11
11
use syn:: token:: Comma ;
12
- use syn:: { braced, parse_macro_input, ArgCaptured , FnArg , Ident , ItemFn , Pat } ;
12
+ use syn:: { braced, parse_macro_input, FnArg , Ident , ItemFn , Pat , PatIdent , PatType , Type } ;
13
13
14
14
type Error = syn:: parse:: Error ;
15
15
@@ -154,22 +154,17 @@ fn files_internal(
154
154
func : proc_macro:: TokenStream ,
155
155
channel : Channel ,
156
156
) -> proc_macro:: TokenStream {
157
- let mut func_item = parse_macro_input ! ( func as ItemFn ) ;
157
+ let mut func_item: ItemFn = parse_macro_input ! ( func as ItemFn ) ;
158
158
let args: FilesTestArgs = parse_macro_input ! ( args as FilesTestArgs ) ;
159
-
160
- let func_name_str = func_item. ident . to_string ( ) ;
161
- let desc_ident = Ident :: new (
162
- & format ! ( "__TEST_{}" , func_item. ident) ,
163
- func_item. ident . span ( ) ,
164
- ) ;
159
+ let info = handle_common_attrs ( & mut func_item, false ) ;
160
+ let func_ident = & func_item. sig . ident ;
161
+ let func_name_str = func_ident. to_string ( ) ;
162
+ let desc_ident = Ident :: new ( & format ! ( "__TEST_{}" , func_ident) , func_ident. span ( ) ) ;
165
163
let trampoline_func_ident = Ident :: new (
166
- & format ! ( "__TEST_TRAMPOLINE_{}" , func_item . ident ) ,
167
- func_item . ident . span ( ) ,
164
+ & format ! ( "__TEST_TRAMPOLINE_{}" , func_ident ) ,
165
+ func_ident . span ( ) ,
168
166
) ;
169
-
170
- let info = handle_common_attrs ( & mut func_item, false ) ;
171
167
let ignore = info. ignore ;
172
-
173
168
let root = args. root ;
174
169
let mut pattern_idx = None ;
175
170
let mut params: Vec < String > = Vec :: new ( ) ;
@@ -182,13 +177,9 @@ fn files_internal(
182
177
// 2. For each argument we collect piece of code to create argument from the `&[PathBuf]` slice
183
178
// given to us by the test runner.
184
179
// 3. Capture the index of the argument corresponding to the "pattern" mapping
185
- for ( mut idx, arg) in func_item. decl . inputs . iter ( ) . enumerate ( ) {
186
- match arg {
187
- FnArg :: Captured ( ArgCaptured {
188
- pat : Pat :: Ident ( pat_ident) ,
189
- ty,
190
- ..
191
- } ) => {
180
+ for ( mut idx, arg) in func_item. sig . inputs . iter ( ) . enumerate ( ) {
181
+ match match_arg ( arg) {
182
+ Some ( ( pat_ident, ty) ) => {
192
183
if info. bench {
193
184
if idx == 0 {
194
185
// FIXME: verify is Bencher!
@@ -220,7 +211,7 @@ fn files_internal(
220
211
. into ( ) ;
221
212
}
222
213
}
223
- _ => {
214
+ None => {
224
215
return Error :: new (
225
216
arg. span ( ) ,
226
217
"unexpected argument; only simple argument types are allowed (`&str`, `String`, `&[u8]`, `Vec<u8>`, `&Path`, etc)" ,
@@ -244,9 +235,6 @@ fn files_internal(
244
235
. into ( ) ;
245
236
}
246
237
247
- // So we can invoke original function from the trampoline function
248
- let orig_func_name = & func_item. ident ;
249
-
250
238
let ( kind, bencher_param) = if info. bench {
251
239
(
252
240
quote ! ( BenchFn ) ,
@@ -274,7 +262,7 @@ fn files_internal(
274
262
#[ automatically_derived]
275
263
#[ allow( non_snake_case) ]
276
264
fn #trampoline_func_ident( #bencher_param paths_arg: & [ :: std:: path:: PathBuf ] ) {
277
- let result = #orig_func_name ( #( #invoke_args) , * ) ;
265
+ let result = #func_ident ( #( #invoke_args) , * ) ;
278
266
:: datatest:: __internal:: assert_test_result( result) ;
279
267
}
280
268
@@ -283,6 +271,15 @@ fn files_internal(
283
271
output. into ( )
284
272
}
285
273
274
+ fn match_arg ( arg : & FnArg ) -> Option < ( & PatIdent , & Type ) > {
275
+ if let FnArg :: Typed ( PatType { pat, ty, .. } ) = arg {
276
+ if let Pat :: Ident ( pat_ident) = pat. as_ref ( ) {
277
+ return Some ( ( pat_ident, ty) ) ;
278
+ }
279
+ }
280
+ None
281
+ }
282
+
286
283
enum ShouldPanic {
287
284
No ,
288
285
Yes ,
@@ -354,7 +351,7 @@ fn parse_should_panic(attr: &syn::Attribute) -> ShouldPanic {
354
351
for item in list. nested {
355
352
match item {
356
353
syn:: NestedMeta :: Meta ( syn:: Meta :: NameValue ( ref nv) )
357
- if nv. ident == "expected" =>
354
+ if nv. path . is_ident ( "expected" ) =>
358
355
{
359
356
if let syn:: Lit :: Str ( ref value) = nv. lit {
360
357
return ShouldPanic :: YesWithMessage ( value. value ( ) ) ;
@@ -413,32 +410,27 @@ fn data_internal(
413
410
) -> proc_macro:: TokenStream {
414
411
let mut func_item = parse_macro_input ! ( func as ItemFn ) ;
415
412
let cases: DataTestArgs = parse_macro_input ! ( args as DataTestArgs ) ;
413
+ let info = handle_common_attrs ( & mut func_item, false ) ;
416
414
let cases = match cases {
417
415
DataTestArgs :: Literal ( path) => quote ! ( datatest:: yaml( #path) ) ,
418
416
DataTestArgs :: Expression ( expr) => quote ! ( #expr) ,
419
417
} ;
418
+ let func_ident = & func_item. sig . ident ;
420
419
421
- let func_name_str = func_item. ident . to_string ( ) ;
422
- let desc_ident = Ident :: new (
423
- & format ! ( "__TEST_{}" , func_item. ident) ,
424
- func_item. ident . span ( ) ,
425
- ) ;
420
+ let func_name_str = func_ident. to_string ( ) ;
421
+ let desc_ident = Ident :: new ( & format ! ( "__TEST_{}" , func_ident) , func_ident. span ( ) ) ;
426
422
let describe_func_ident = Ident :: new (
427
- & format ! ( "__TEST_DESCRIBE_{}" , func_item . ident ) ,
428
- func_item . ident . span ( ) ,
423
+ & format ! ( "__TEST_DESCRIBE_{}" , func_ident ) ,
424
+ func_ident . span ( ) ,
429
425
) ;
430
426
let trampoline_func_ident = Ident :: new (
431
- & format ! ( "__TEST_TRAMPOLINE_{}" , func_item . ident ) ,
432
- func_item . ident . span ( ) ,
427
+ & format ! ( "__TEST_TRAMPOLINE_{}" , func_ident ) ,
428
+ func_ident . span ( ) ,
433
429
) ;
434
430
435
- let info = handle_common_attrs ( & mut func_item, false ) ;
436
431
let ignore = info. ignore ;
437
-
438
432
// FIXME: check file exists!
439
-
440
- let orig_func_ident = & func_item. ident ;
441
- let mut args = func_item. decl . inputs . iter ( ) ;
433
+ let mut args = func_item. sig . inputs . iter ( ) ;
442
434
443
435
if info. bench {
444
436
// Skip Bencher argument
@@ -448,7 +440,7 @@ fn data_internal(
448
440
449
441
let arg = args. next ( ) ;
450
442
let ty = match arg {
451
- Some ( FnArg :: Captured ( ArgCaptured { ty, .. } ) ) => Some ( ty) ,
443
+ Some ( FnArg :: Typed ( PatType { ty, .. } ) ) => Some ( ty. as_ref ( ) ) ,
452
444
_ => None ,
453
445
} ;
454
446
let ( ref_token, ty) = match ty {
@@ -484,7 +476,7 @@ fn data_internal(
484
476
#[ automatically_derived]
485
477
#[ allow( non_snake_case) ]
486
478
fn #trampoline_func_ident( #bencher_param arg: #ty) {
487
- let result = #orig_func_ident ( #bencher_arg #ref_token arg) ;
479
+ let result = #func_ident ( #bencher_arg #ref_token arg) ;
488
480
:: datatest:: __internal:: assert_test_result( result) ;
489
481
}
490
482
@@ -545,20 +537,16 @@ pub fn test_stable(
545
537
) -> proc_macro:: TokenStream {
546
538
let mut func_item = parse_macro_input ! ( func as ItemFn ) ;
547
539
let info = handle_common_attrs ( & mut func_item, true ) ;
548
- let ignore = info. ignore ;
549
- let func_ident = & func_item. ident ;
550
- let func_name_str = func_item. ident . to_string ( ) ;
551
- let desc_ident = Ident :: new (
552
- & format ! ( "__TEST_{}" , func_item. ident) ,
553
- func_item. ident . span ( ) ,
554
- ) ;
540
+ let func_ident = & func_item. sig . ident ;
541
+ let func_name_str = func_ident. to_string ( ) ;
542
+ let desc_ident = Ident :: new ( & format ! ( "__TEST_{}" , func_ident) , func_ident. span ( ) ) ;
555
543
544
+ let ignore = info. ignore ;
556
545
let should_panic = match info. should_panic {
557
546
ShouldPanic :: No => quote ! ( :: datatest:: __internal:: RegularShouldPanic :: No ) ,
558
547
ShouldPanic :: Yes => quote ! ( :: datatest:: __internal:: RegularShouldPanic :: Yes ) ,
559
548
ShouldPanic :: YesWithMessage ( v) => quote ! ( :: datatest:: __internal:: RegularShouldPanic :: YesWithMessage ( #v) ) ,
560
549
} ;
561
-
562
550
let registration = test_registration ( Channel :: Stable , & desc_ident) ;
563
551
let output = quote ! {
564
552
#registration
0 commit comments