1 //! Test that skipped fields are not read into structs when they appear in input.
2 
3 use darling::{FromDeriveInput, FromMeta};
4 use syn::parse_quote;
5 
6 #[derive(Debug, PartialEq, Eq, FromDeriveInput)]
7 #[darling(attributes(skip_test))]
8 pub struct Lorem {
9     ipsum: String,
10 
11     #[darling(skip)]
12     dolor: u8,
13 }
14 
15 /// Verify variant-level and field-level skip work correctly for enums.
16 #[derive(Debug, FromMeta)]
17 pub enum Sit {
18     Amet(bool),
19 
20     #[darling(skip)]
21     Foo {
22         hello: bool,
23     },
24 
25     Bar {
26         hello: bool,
27         #[darling(skip)]
28         world: u8,
29     },
30 }
31 
32 #[test]
verify_skipped_field_not_required()33 fn verify_skipped_field_not_required() {
34     let di = parse_quote! {
35         #[skip_test(ipsum = "Hello")]
36         struct Baz;
37     };
38 
39     assert_eq!(
40         Lorem::from_derive_input(&di).unwrap(),
41         Lorem {
42             ipsum: "Hello".to_string(),
43             dolor: 0,
44         }
45     );
46 }
47 
48 /// This test verifies that a skipped field will still prefer an explicit default
49 /// over the default that would come from its field type. It would be incorrect for
50 /// `Defaulting::from_derive_input` to fail here, and it would be wrong for the value
51 /// of `dolor` to be `None`.
52 #[test]
verify_default_supersedes_from_none()53 fn verify_default_supersedes_from_none() {
54     fn default_dolor() -> Option<u8> {
55         Some(2)
56     }
57 
58     #[derive(Debug, PartialEq, Eq, FromDeriveInput)]
59     #[darling(attributes(skip_test))]
60     pub struct Defaulting {
61         #[darling(skip, default = "default_dolor")]
62         dolor: Option<u8>,
63     }
64 
65     let di = parse_quote! {
66         #[skip_test]
67         struct Baz;
68     };
69 
70     assert_eq!(
71         Defaulting::from_derive_input(&di).unwrap(),
72         Defaulting { dolor: Some(2) }
73     )
74 }
75