2

当我开始编写自定义派生时,我正在尝试测试结构的解析属性。我想将测试自定义派生拆分为多个更简单的测试功能,而不是一次将其作为一个完整的派生系统进行测试。

这是我的建议:

#[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
    }
}

我想出了这个草稿:

#[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) 
    );
}

您可以建议一种完全不同的方式。

4

0 回答 0