third_party/rust/darling/tests/happy_path.rs
author Emilio Cobos Álvarez <emilio@crisal.io>
Sun, 02 Dec 2018 12:15:47 -0500
changeset 449236 d2c99742610827141e7c50a5da9692449fcbc6b3
parent 403619 c8318693f1d9667eaf3c1c2e9ee84d5809c84cc5
child 449246 f713c586d397c53c3d3dc403638d7609f3da4e56
permissions -rw-r--r--
Bug 1511811 - Revendor Rust dependencies.

#[macro_use]
extern crate darling;

#[macro_use]
extern crate syn;
#[macro_use]
extern crate quote;

use darling::FromDeriveInput;

#[derive(Default, FromMeta, PartialEq, Debug)]
#[darling(default)]
struct Lorem {
    ipsum: bool,
    dolor: Option<String>,
}

#[derive(FromDeriveInput, PartialEq, Debug)]
#[darling(attributes(darling_demo))]
struct Core {
    ident: syn::Ident,
    vis: syn::Visibility,
    generics: syn::Generics,
    lorem: Lorem,
}

#[derive(FromDeriveInput, PartialEq, Debug)]
#[darling(attributes(darling_demo))]
struct TraitCore {
    ident: syn::Ident,
    generics: syn::Generics,
    lorem: Lorem,
}

#[test]
fn simple() {
    let di = syn::parse_str(
        r#"
        #[derive(Foo)]
        #[darling_demo(lorem(ipsum))]
        pub struct Bar;
    "#,
    ).unwrap();

    assert_eq!(
        Core::from_derive_input(&di).unwrap(),
        Core {
            ident: parse_quote!(Bar),
            vis: parse_quote!(pub),
            generics: Default::default(),
            lorem: Lorem {
                ipsum: true,
                dolor: None,
            },
        }
    );
}

#[test]
fn trait_type() {
    let di = syn::parse_str(
        r#"
        #[derive(Foo)]
        #[darling_demo(lorem(dolor = "hello"))]
        pub struct Bar;
    "#,
    ).unwrap();

    assert_eq!(
        TraitCore::from_derive_input(&di).unwrap(),
        TraitCore {
            ident: parse_quote!(Bar),
            generics: Default::default(),
            lorem: Lorem {
                ipsum: false,
                dolor: Some("hello".to_owned()),
            }
        }
    );
}