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
extern crate proc_macro;
extern crate proc_macro2;
extern crate quote;
extern crate syn;

use std::mem;

use proc_macro::TokenStream;
use quote::quote;
use syn::{
    parse::{Parse, Parser},
    spanned::Spanned,
};

#[proc_macro_attribute]
pub fn sample_test(_args: TokenStream, input: TokenStream) -> TokenStream {
    let output = match syn::Item::parse.parse(input.clone()) {
        Ok(syn::Item::Fn(mut item_fn)) => {
            let mut inputs = syn::punctuated::Punctuated::new();
            let mut samplers: syn::punctuated::Punctuated<_, syn::token::Comma> =
                syn::punctuated::Punctuated::new();
            let mut errors = Vec::new();

            item_fn
                .sig
                .inputs
                .iter_mut()
                .for_each(|input| match *input {
                    syn::FnArg::Typed(syn::PatType {
                        ref mut ty,
                        ref mut attrs,
                        ..
                    }) => {
                        let ix = attrs
                            .iter()
                            .position(|a| a.path.segments.iter().map(|s| &s.ident).eq(["sample"]));
                        if let Some(ix) = ix {
                            samplers.push(attrs.remove(ix).tokens);
                            inputs.push(syn::BareFnArg {
                                attrs: attrs.clone(),
                                name: None,
                                ty: *ty.clone(),
                            })
                        }
                    }
                    _ => errors.push(syn::parse::Error::new(
                        input.span(),
                        "unsupported kind of function argument",
                    )),
                });

            if errors.is_empty() {
                let attrs = mem::replace(&mut item_fn.attrs, Vec::new());
                let name = &item_fn.sig.ident;
                let fn_type = syn::TypeBareFn {
                    lifetimes: None,
                    unsafety: item_fn.sig.unsafety.clone(),
                    abi: item_fn.sig.abi.clone(),
                    fn_token: <syn::Token![fn]>::default(),
                    paren_token: syn::token::Paren::default(),
                    inputs,
                    variadic: item_fn.sig.variadic.clone(),
                    output: item_fn.sig.output.clone(),
                };

                let x = samplers.clone();
                quote! {
                    #[test]
                    #(#attrs)*
                    fn #name() {
                        let sampler = (#x,);

                        #item_fn
                        ::sample_test::tester::sample_test(sampler, #name as #fn_type)
                    }
                }
            } else {
                errors
                    .iter()
                    .map(syn::parse::Error::to_compile_error)
                    .collect()
            }
        }
        Ok(syn::Item::Static(mut item_static)) => {
            let attrs = mem::replace(&mut item_static.attrs, Vec::new());
            let name = &item_static.ident;

            quote! {
                #[test]
                #(#attrs)*
                fn #name() {
                    #item_static
                    ::quickcheck::quickcheck(#name)
                }
            }
        }
        _ => {
            let span = proc_macro2::TokenStream::from(input).span();
            let msg = "#[sample_test] is only supported on statics and functions";

            syn::parse::Error::new(span, msg).to_compile_error()
        }
    };

    output.into()
}

#[cfg(test)]
mod tests {}