Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
proptest-macro 0.1.0 - Docs.rs
[go: Go Back, main page]

proptest-macro 0.1.0

Procedural macros for the proptest crate
Documentation
use core::mem::replace;

use syn::{punctuated::Punctuated, FnArg, ItemFn, PatType};

/// Convert a function to a zero-arg function, and return the args
///
/// Panics if any arg is a receiver (i.e. `self` or a variant)
pub fn strip_args(mut f: ItemFn) -> (ItemFn, Vec<PatType>) {
    let args = replace(&mut f.sig.inputs, Punctuated::new());
    (f, args.into_iter().map(|arg| match arg {
        FnArg::Typed(arg) => arg,
        FnArg::Receiver(_) => panic!("receivers aren't allowed - should be filtered by `validate`"),
    }).collect())
}

#[cfg(test)]
mod tests {
    use quote::ToTokens;
    use syn::parse_str;

    use super::*;

    #[test]
    fn strip_args_works() {
        let f = parse_str("fn foo(i: i32) {}").unwrap();
        let (f, mut types) = strip_args(f);

        assert_eq!(f.to_token_stream().to_string(), "fn foo () { }");

        assert_eq!(types.len(), 1);
        let ty = types.pop().unwrap();
        assert_eq!(ty.to_token_stream().to_string(), "i : i32");
    }

    #[test]
    #[should_panic]
    fn strip_args_panics_with_self() {
        let f = parse_str("fn foo(self) {}").unwrap();
        strip_args(f);
    }
}