1 #![allow(dead_code)]
2 //! These tests verify that multiple errors will be collected up from throughout
3 //! the parsing process and returned correctly to the caller.
4 
5 use darling::{ast, FromDeriveInput, FromField, FromMeta};
6 use syn::parse_quote;
7 
8 #[derive(Debug, FromDeriveInput)]
9 #[darling(attributes(accrue))]
10 struct Lorem {
11     ipsum: String,
12     dolor: Dolor,
13     data: ast::Data<(), LoremField>,
14 }
15 
16 #[derive(Debug, FromMeta)]
17 struct Dolor {
18     sit: bool,
19 }
20 
21 #[derive(Debug, FromField)]
22 #[darling(attributes(accrue))]
23 struct LoremField {
24     ident: Option<syn::Ident>,
25     aliased_as: syn::Ident,
26 }
27 
28 #[test]
bad_type_and_missing_fields()29 fn bad_type_and_missing_fields() {
30     let input = parse_quote! {
31         #[accrue(ipsum = true, dolor(amet = "Hi"))]
32         pub struct NonConforming {
33             foo: ()
34         }
35     };
36 
37     let s_result: ::darling::Error = Lorem::from_derive_input(&input).unwrap_err();
38     let err = s_result.flatten();
39     println!("{}", err);
40     assert_eq!(3, err.len());
41 }
42 
43 #[test]
body_only_issues()44 fn body_only_issues() {
45     let input = parse_quote! {
46         #[accrue(ipsum = "Hello", dolor(sit))]
47         pub struct NonConforming {
48             foo: (),
49             bar: bool,
50         }
51     };
52 
53     let s_err = Lorem::from_derive_input(&input).unwrap_err();
54     println!("{:?}", s_err);
55     assert_eq!(2, s_err.len());
56 }
57 
58 #[derive(Debug, FromMeta)]
59 enum Week {
60     Monday,
61     Tuesday { morning: bool, afternoon: String },
62     Wednesday(Dolor),
63 }
64 
65 #[derive(Debug, FromDeriveInput)]
66 #[darling(attributes(accrue))]
67 struct Month {
68     schedule: Week,
69 }
70 
71 #[test]
error_in_enum_fields()72 fn error_in_enum_fields() {
73     let input = parse_quote! {
74         #[accrue(schedule(tuesday(morning = "yes")))]
75         pub struct NonConforming {
76             foo: (),
77             bar: bool,
78         }
79     };
80 
81     let s_err = Month::from_derive_input(&input).unwrap_err();
82     assert_eq!(2, s_err.len());
83     let err = s_err.flatten();
84     // TODO add tests to check location path is correct
85     println!("{}", err);
86 }
87 
88 #[test]
error_in_newtype_variant()89 fn error_in_newtype_variant() {
90     let input = parse_quote! {
91         #[accrue(schedule(wednesday(sit = "yes")))]
92         pub struct NonConforming {
93             foo: (),
94             bar: bool,
95         }
96     };
97 
98     let s_err = Month::from_derive_input(&input).unwrap_err();
99     assert_eq!(1, s_err.len());
100     println!("{}", s_err);
101     println!("{}", s_err.flatten());
102 }
103