2

I'm trying to test parsing attributes for structs as I'm beginning to write a custom derive. I would like to split testing custom derives into multiple simpler test functions rather than testing it as a whole derive system at once.

This is my proposal:

#[derive(From)]
#[from(forward)]  //this is what I wanna parse into `FromAttribute`
struct A;
#[derive(Debug, PartialEq)]
pub enum FromAttribute {
    None,
    /// Add general bounds for `From` implementation.
    Forward,
    /// Skip generate `From` impl for this variant
    Ignore,
    /// Manually choose for witch `From` should be implemented
    Types(Vec<syn::Type>),
}

impl FromAttribute {
    pub fn from_attributes(attrs: &[syn::Attribute]) -> syn::Result<FromAttribute> {
        // function I want to test
    }
}

I come up with this draft:

#[test]
fn from_attribute_test() {
    let code: HOW_TO_MAKE_IT_PARSE_STREAM = #[from(forward))] ; /// This is part I don't know how to implement
    let attributes: Vec<syn::Attribute> = syn::Attribute::parse_inner(code).unwrap();

    assert_eq!(
        FromAttribute::Forward,
        FromAttribute::from_attributes(&attributes) 
    );
}

You can suggest a completely different way.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
S.R
  • 2,411
  • 1
  • 22
  • 33
  • Your question might be answered by the answers of [Convert string into TokenStream](https://stackoverflow.com/q/54165117/155423). If so, we can mark this question as already answered. – Shepmaster May 18 '20 at 18:47
  • 1
    It looks like no. I need `Vec` at there is no straight way to do it according to doc. [Look here](https://docs.rs/syn/1.0.22/syn/struct.Attribute.html#parsing-from-tokens-to-attribute) Even if your suggestion is a correct first step I don't know what to do with `proc_macro2::TokenStream` to convert it to `Vec` or [`syn::ParseStream`](https://docs.rs/syn/1.0.22/syn/parse/type.ParseStream.html) – S.R May 18 '20 at 19:29

0 Answers0