How do I write simple unit test functions for attributes when creating procedural macros?

Viewed 213

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.

0 Answers
Related