Skip to content

Commit

Permalink
Add test for flattening in FromField
Browse files Browse the repository at this point in the history
  • Loading branch information
TedDriggs committed Feb 22, 2024
1 parent 78ad512 commit 4790d79
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions tests/flatten_from_field.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use darling::{ast, util::Ignored, FromDeriveInput, FromField, FromMeta};
use proc_macro2::{Ident, Span};
use syn::parse_quote;

#[derive(FromMeta)]
struct Vis {
public: bool,
#[darling(default)]
private: bool,
}

#[derive(FromField)]
#[darling(attributes(v))]
struct Field {
ident: Option<Ident>,
example: Option<String>,
#[darling(flatten)]
visibility: Vis,
}

#[derive(FromDeriveInput)]
#[darling(attributes(v))]
struct Input {
data: ast::Data<Ignored, Field>,
}

#[test]
fn field_flattens() {
let di = Input::from_derive_input(&parse_quote! {
struct Demo {
#[v(public, example = "world")]
hello: String
}
})
.unwrap();

let fields = di.data.take_struct().unwrap();
let first_field = fields.into_iter().next().unwrap();
assert_eq!(
first_field.ident,
Some(Ident::new("hello", Span::call_site()))
);
assert!(first_field.visibility.public);
assert!(!first_field.visibility.private);
assert_eq!(first_field.example.unwrap(), "world");
}

0 comments on commit 4790d79

Please sign in to comment.