1#![allow(clippy::shadow_unrelated, clippy::too_many_lines)]
2
3#[macro_use]
4mod macros;
5
6use syn::{Meta, MetaList, MetaNameValue, NestedMeta};
7
8#[test]
9fn test_parse_meta_item_word() {
10 let input = "hello";
11
12 snapshot!(input as Meta, @r###"
13 Path(Path {
14 segments: [
15 PathSegment {
16 ident: "hello",
17 arguments: None,
18 },
19 ],
20 })
21 "###);
22}
23
24#[test]
25fn test_parse_meta_name_value() {
26 let input = "foo = 5";
27 let (inner, meta) = (input, input);
28
29 snapshot!(inner as MetaNameValue, @r###"
30 MetaNameValue {
31 path: Path {
32 segments: [
33 PathSegment {
34 ident: "foo",
35 arguments: None,
36 },
37 ],
38 },
39 lit: 5,
40 }
41 "###);
42
43 snapshot!(meta as Meta, @r###"
44 Meta::NameValue {
45 path: Path {
46 segments: [
47 PathSegment {
48 ident: "foo",
49 arguments: None,
50 },
51 ],
52 },
53 lit: 5,
54 }
55 "###);
56
57 assert_eq!(meta, inner.into());
58}
59
60#[test]
61fn test_parse_meta_name_value_with_keyword() {
62 let input = "static = 5";
63 let (inner, meta) = (input, input);
64
65 snapshot!(inner as MetaNameValue, @r###"
66 MetaNameValue {
67 path: Path {
68 segments: [
69 PathSegment {
70 ident: "static",
71 arguments: None,
72 },
73 ],
74 },
75 lit: 5,
76 }
77 "###);
78
79 snapshot!(meta as Meta, @r###"
80 Meta::NameValue {
81 path: Path {
82 segments: [
83 PathSegment {
84 ident: "static",
85 arguments: None,
86 },
87 ],
88 },
89 lit: 5,
90 }
91 "###);
92
93 assert_eq!(meta, inner.into());
94}
95
96#[test]
97fn test_parse_meta_name_value_with_bool() {
98 let input = "true = 5";
99 let (inner, meta) = (input, input);
100
101 snapshot!(inner as MetaNameValue, @r###"
102 MetaNameValue {
103 path: Path {
104 segments: [
105 PathSegment {
106 ident: "true",
107 arguments: None,
108 },
109 ],
110 },
111 lit: 5,
112 }
113 "###);
114
115 snapshot!(meta as Meta, @r###"
116 Meta::NameValue {
117 path: Path {
118 segments: [
119 PathSegment {
120 ident: "true",
121 arguments: None,
122 },
123 ],
124 },
125 lit: 5,
126 }
127 "###);
128
129 assert_eq!(meta, inner.into());
130}
131
132#[test]
133fn test_parse_meta_item_list_lit() {
134 let input = "foo(5)";
135 let (inner, meta) = (input, input);
136
137 snapshot!(inner as MetaList, @r###"
138 MetaList {
139 path: Path {
140 segments: [
141 PathSegment {
142 ident: "foo",
143 arguments: None,
144 },
145 ],
146 },
147 nested: [
148 Lit(5),
149 ],
150 }
151 "###);
152
153 snapshot!(meta as Meta, @r###"
154 Meta::List {
155 path: Path {
156 segments: [
157 PathSegment {
158 ident: "foo",
159 arguments: None,
160 },
161 ],
162 },
163 nested: [
164 Lit(5),
165 ],
166 }
167 "###);
168
169 assert_eq!(meta, inner.into());
170}
171
172#[test]
173fn test_parse_meta_item_multiple() {
174 let input = "foo(word, name = 5, list(name2 = 6), word2)";
175 let (inner, meta) = (input, input);
176
177 snapshot!(inner as MetaList, @r###"
178 MetaList {
179 path: Path {
180 segments: [
181 PathSegment {
182 ident: "foo",
183 arguments: None,
184 },
185 ],
186 },
187 nested: [
188 Meta(Path(Path {
189 segments: [
190 PathSegment {
191 ident: "word",
192 arguments: None,
193 },
194 ],
195 })),
196 Meta(Meta::NameValue {
197 path: Path {
198 segments: [
199 PathSegment {
200 ident: "name",
201 arguments: None,
202 },
203 ],
204 },
205 lit: 5,
206 }),
207 Meta(Meta::List {
208 path: Path {
209 segments: [
210 PathSegment {
211 ident: "list",
212 arguments: None,
213 },
214 ],
215 },
216 nested: [
217 Meta(Meta::NameValue {
218 path: Path {
219 segments: [
220 PathSegment {
221 ident: "name2",
222 arguments: None,
223 },
224 ],
225 },
226 lit: 6,
227 }),
228 ],
229 }),
230 Meta(Path(Path {
231 segments: [
232 PathSegment {
233 ident: "word2",
234 arguments: None,
235 },
236 ],
237 })),
238 ],
239 }
240 "###);
241
242 snapshot!(meta as Meta, @r###"
243 Meta::List {
244 path: Path {
245 segments: [
246 PathSegment {
247 ident: "foo",
248 arguments: None,
249 },
250 ],
251 },
252 nested: [
253 Meta(Path(Path {
254 segments: [
255 PathSegment {
256 ident: "word",
257 arguments: None,
258 },
259 ],
260 })),
261 Meta(Meta::NameValue {
262 path: Path {
263 segments: [
264 PathSegment {
265 ident: "name",
266 arguments: None,
267 },
268 ],
269 },
270 lit: 5,
271 }),
272 Meta(Meta::List {
273 path: Path {
274 segments: [
275 PathSegment {
276 ident: "list",
277 arguments: None,
278 },
279 ],
280 },
281 nested: [
282 Meta(Meta::NameValue {
283 path: Path {
284 segments: [
285 PathSegment {
286 ident: "name2",
287 arguments: None,
288 },
289 ],
290 },
291 lit: 6,
292 }),
293 ],
294 }),
295 Meta(Path(Path {
296 segments: [
297 PathSegment {
298 ident: "word2",
299 arguments: None,
300 },
301 ],
302 })),
303 ],
304 }
305 "###);
306
307 assert_eq!(meta, inner.into());
308}
309
310#[test]
311fn test_parse_nested_meta() {
312 let input = "5";
313 snapshot!(input as NestedMeta, @"Lit(5)");
314
315 let input = "list(name2 = 6)";
316 snapshot!(input as NestedMeta, @r###"
317 Meta(Meta::List {
318 path: Path {
319 segments: [
320 PathSegment {
321 ident: "list",
322 arguments: None,
323 },
324 ],
325 },
326 nested: [
327 Meta(Meta::NameValue {
328 path: Path {
329 segments: [
330 PathSegment {
331 ident: "name2",
332 arguments: None,
333 },
334 ],
335 },
336 lit: 6,
337 }),
338 ],
339 })
340 "###);
341}
342
343#[test]
344fn test_parse_path() {
345 let input = "::serde::Serialize";
346 snapshot!(input as Meta, @r###"
347 Path(Path {
348 leading_colon: Some,
349 segments: [
350 PathSegment {
351 ident: "serde",
352 arguments: None,
353 },
354 PathSegment {
355 ident: "Serialize",
356 arguments: None,
357 },
358 ],
359 })
360 "###);
361
362 let input = "::serde::Serialize";
363 snapshot!(input as NestedMeta, @r###"
364 Meta(Path(Path {
365 leading_colon: Some,
366 segments: [
367 PathSegment {
368 ident: "serde",
369 arguments: None,
370 },
371 PathSegment {
372 ident: "Serialize",
373 arguments: None,
374 },
375 ],
376 }))
377 "###);
378}
379