1 | #![allow (clippy::uninlined_format_args)] |
2 | |
3 | #[macro_use ] |
4 | mod macros; |
5 | |
6 | use syn::parse::Parser; |
7 | use syn::{Attribute, Meta}; |
8 | |
9 | #[test] |
10 | fn test_meta_item_word() { |
11 | let meta = test("#[foo]" ); |
12 | |
13 | snapshot!(meta, @r###" |
14 | Meta::Path { |
15 | segments: [ |
16 | PathSegment { |
17 | ident: "foo", |
18 | }, |
19 | ], |
20 | } |
21 | "### ); |
22 | } |
23 | |
24 | #[test] |
25 | fn test_meta_item_name_value() { |
26 | let meta = test("#[foo = 5]" ); |
27 | |
28 | snapshot!(meta, @r###" |
29 | Meta::NameValue { |
30 | path: Path { |
31 | segments: [ |
32 | PathSegment { |
33 | ident: "foo", |
34 | }, |
35 | ], |
36 | }, |
37 | value: Expr::Lit { |
38 | lit: 5, |
39 | }, |
40 | } |
41 | "### ); |
42 | } |
43 | |
44 | #[test] |
45 | fn test_meta_item_bool_value() { |
46 | let meta = test("#[foo = true]" ); |
47 | |
48 | snapshot!(meta, @r###" |
49 | Meta::NameValue { |
50 | path: Path { |
51 | segments: [ |
52 | PathSegment { |
53 | ident: "foo", |
54 | }, |
55 | ], |
56 | }, |
57 | value: Expr::Lit { |
58 | lit: Lit::Bool { |
59 | value: true, |
60 | }, |
61 | }, |
62 | } |
63 | "### ); |
64 | |
65 | let meta = test("#[foo = false]" ); |
66 | |
67 | snapshot!(meta, @r###" |
68 | Meta::NameValue { |
69 | path: Path { |
70 | segments: [ |
71 | PathSegment { |
72 | ident: "foo", |
73 | }, |
74 | ], |
75 | }, |
76 | value: Expr::Lit { |
77 | lit: Lit::Bool { |
78 | value: false, |
79 | }, |
80 | }, |
81 | } |
82 | "### ); |
83 | } |
84 | |
85 | #[test] |
86 | fn test_meta_item_list_lit() { |
87 | let meta = test("#[foo(5)]" ); |
88 | |
89 | snapshot!(meta, @r###" |
90 | Meta::List { |
91 | path: Path { |
92 | segments: [ |
93 | PathSegment { |
94 | ident: "foo", |
95 | }, |
96 | ], |
97 | }, |
98 | delimiter: MacroDelimiter::Paren, |
99 | tokens: TokenStream(`5`), |
100 | } |
101 | "### ); |
102 | } |
103 | |
104 | #[test] |
105 | fn test_meta_item_list_word() { |
106 | let meta = test("#[foo(bar)]" ); |
107 | |
108 | snapshot!(meta, @r###" |
109 | Meta::List { |
110 | path: Path { |
111 | segments: [ |
112 | PathSegment { |
113 | ident: "foo", |
114 | }, |
115 | ], |
116 | }, |
117 | delimiter: MacroDelimiter::Paren, |
118 | tokens: TokenStream(`bar`), |
119 | } |
120 | "### ); |
121 | } |
122 | |
123 | #[test] |
124 | fn test_meta_item_list_name_value() { |
125 | let meta = test("#[foo(bar = 5)]" ); |
126 | |
127 | snapshot!(meta, @r###" |
128 | Meta::List { |
129 | path: Path { |
130 | segments: [ |
131 | PathSegment { |
132 | ident: "foo", |
133 | }, |
134 | ], |
135 | }, |
136 | delimiter: MacroDelimiter::Paren, |
137 | tokens: TokenStream(`bar = 5`), |
138 | } |
139 | "### ); |
140 | } |
141 | |
142 | #[test] |
143 | fn test_meta_item_list_bool_value() { |
144 | let meta = test("#[foo(bar = true)]" ); |
145 | |
146 | snapshot!(meta, @r###" |
147 | Meta::List { |
148 | path: Path { |
149 | segments: [ |
150 | PathSegment { |
151 | ident: "foo", |
152 | }, |
153 | ], |
154 | }, |
155 | delimiter: MacroDelimiter::Paren, |
156 | tokens: TokenStream(`bar = true`), |
157 | } |
158 | "### ); |
159 | } |
160 | |
161 | #[test] |
162 | fn test_meta_item_multiple() { |
163 | let meta = test("#[foo(word, name = 5, list(name2 = 6), word2)]" ); |
164 | |
165 | snapshot!(meta, @r###" |
166 | Meta::List { |
167 | path: Path { |
168 | segments: [ |
169 | PathSegment { |
170 | ident: "foo", |
171 | }, |
172 | ], |
173 | }, |
174 | delimiter: MacroDelimiter::Paren, |
175 | tokens: TokenStream(`word , name = 5 , list (name2 = 6) , word2`), |
176 | } |
177 | "### ); |
178 | } |
179 | |
180 | #[test] |
181 | fn test_bool_lit() { |
182 | let meta = test("#[foo(true)]" ); |
183 | |
184 | snapshot!(meta, @r###" |
185 | Meta::List { |
186 | path: Path { |
187 | segments: [ |
188 | PathSegment { |
189 | ident: "foo", |
190 | }, |
191 | ], |
192 | }, |
193 | delimiter: MacroDelimiter::Paren, |
194 | tokens: TokenStream(`true`), |
195 | } |
196 | "### ); |
197 | } |
198 | |
199 | #[test] |
200 | fn test_negative_lit() { |
201 | let meta = test("#[form(min = -1, max = 200)]" ); |
202 | |
203 | snapshot!(meta, @r###" |
204 | Meta::List { |
205 | path: Path { |
206 | segments: [ |
207 | PathSegment { |
208 | ident: "form", |
209 | }, |
210 | ], |
211 | }, |
212 | delimiter: MacroDelimiter::Paren, |
213 | tokens: TokenStream(`min = - 1 , max = 200`), |
214 | } |
215 | "### ); |
216 | } |
217 | |
218 | fn test(input: &str) -> Meta { |
219 | let attrs = Attribute::parse_outer.parse_str(input).unwrap(); |
220 | |
221 | assert_eq!(attrs.len(), 1); |
222 | let attr = attrs.into_iter().next().unwrap(); |
223 | |
224 | attr.meta |
225 | } |
226 | |