blob: 2a7e106b97415fb386118ed2924846c0a30475bc [file] [log] [blame]
David Tolnay7d8b3312019-03-10 01:26:11 -08001extern crate syn;
2
3mod features;
4
5#[macro_use]
6mod macros;
7
8use syn::parse::Parser;
David Tolnayfcd53cf2019-05-09 13:06:59 -07009use syn::{Attribute, Meta};
David Tolnay7d8b3312019-03-10 01:26:11 -080010
11#[test]
12fn test_meta_item_word() {
David Tolnayfcd53cf2019-05-09 13:06:59 -070013 let (interpret, parse) = test("#[foo]");
14
15 snapshot!(interpret, @r###"Word("foo")"###);
16
17 snapshot!(parse, @r###"Word("foo")"###);
David Tolnay7d8b3312019-03-10 01:26:11 -080018}
19
20#[test]
21fn test_meta_item_name_value() {
David Tolnayfcd53cf2019-05-09 13:06:59 -070022 let (interpret, parse) = test("#[foo = 5]");
23
24 snapshot!(interpret, @r###"
25 Meta::NameValue {
26 ident: "foo",
27 lit: 5,
28 ⋮}
29 "###);
30
31 snapshot!(parse, @r###"
32 Meta::NameValue {
33 ident: "foo",
34 lit: 5,
35 ⋮}
36 "###);
David Tolnay7d8b3312019-03-10 01:26:11 -080037}
38
39#[test]
40fn test_meta_item_bool_value() {
David Tolnayfcd53cf2019-05-09 13:06:59 -070041 let (interpret, parse) = test("#[foo = true]");;
42
43 snapshot!(interpret, @r###"
44 Meta::NameValue {
45 ident: "foo",
46 lit: Lit::Bool {
47 value: true,
48 },
49 ⋮}
50 "###);
51
52 snapshot!(parse, @r###"
53 Meta::NameValue {
54 ident: "foo",
55 lit: Lit::Bool {
56 value: true,
57 },
58 ⋮}
59 "###);
60
61 let (interpret, parse) = test("#[foo = false]");
62
63 snapshot!(interpret, @r###"
64 Meta::NameValue {
65 ident: "foo",
66 lit: Lit::Bool {
67 value: false,
68 },
69 ⋮}
70 "###);
71
72 snapshot!(parse, @r###"
73 Meta::NameValue {
74 ident: "foo",
75 lit: Lit::Bool {
76 value: false,
77 },
78 ⋮}
79 "###);
David Tolnay7d8b3312019-03-10 01:26:11 -080080}
81
82#[test]
83fn test_meta_item_list_lit() {
David Tolnayfcd53cf2019-05-09 13:06:59 -070084 let (interpret, parse) = test("#[foo(5)]");
85
86 snapshot!(interpret, @r###"
87 Meta::List {
88 ident: "foo",
89 nested: [
90 Literal(5),
91 ],
92 ⋮}
93 "###);
94
95 snapshot!(parse, @r###"
96 Meta::List {
97 ident: "foo",
98 nested: [
99 Literal(5),
100 ],
101 ⋮}
102 "###);
David Tolnay7d8b3312019-03-10 01:26:11 -0800103}
104
105#[test]
106fn test_meta_item_list_word() {
David Tolnayfcd53cf2019-05-09 13:06:59 -0700107 let (interpret, parse) = test("#[foo(bar)]");
108
109 snapshot!(interpret, @r###"
110 Meta::List {
111 ident: "foo",
112 nested: [
113 Meta(Word("bar")),
114 ],
115 ⋮}
116 "###);
117
118 snapshot!(parse, @r###"
119 Meta::List {
120 ident: "foo",
121 nested: [
122 Meta(Word("bar")),
123 ],
124 ⋮}
125 "###);
David Tolnay7d8b3312019-03-10 01:26:11 -0800126}
127
128#[test]
129fn test_meta_item_list_name_value() {
David Tolnayfcd53cf2019-05-09 13:06:59 -0700130 let (interpret, parse) = test("#[foo(bar = 5)]");
131
132 snapshot!(interpret, @r###"
133 Meta::List {
134 ident: "foo",
135 nested: [
136 Meta(Meta::NameValue {
137 ident: "bar",
138 lit: 5,
139 }),
140 ],
141 ⋮}
142 "###);
143
144 snapshot!(parse, @r###"
145 Meta::List {
146 ident: "foo",
147 nested: [
148 Meta(Meta::NameValue {
149 ident: "bar",
150 lit: 5,
151 }),
152 ],
153 ⋮}
154 "###);
David Tolnay7d8b3312019-03-10 01:26:11 -0800155}
156
157#[test]
158fn test_meta_item_list_bool_value() {
David Tolnayfcd53cf2019-05-09 13:06:59 -0700159 let (interpret, parse) = test("#[foo(bar = true)]");
160
161 snapshot!(interpret, @r###"
162 Meta::List {
163 ident: "foo",
164 nested: [
165 Meta(Meta::NameValue {
166 ident: "bar",
167 lit: Lit::Bool {
168 value: true,
169 },
170 }),
171 ],
172 ⋮}
173 "###);
174
175 snapshot!(parse, @r###"
176 Meta::List {
177 ident: "foo",
178 nested: [
179 Meta(Meta::NameValue {
180 ident: "bar",
181 lit: Lit::Bool {
182 value: true,
183 },
184 }),
185 ],
186 ⋮}
187 "###);
David Tolnay7d8b3312019-03-10 01:26:11 -0800188}
189
190#[test]
191fn test_meta_item_multiple() {
David Tolnayfcd53cf2019-05-09 13:06:59 -0700192 let (interpret, parse) = test("#[foo(word, name = 5, list(name2 = 6), word2)]");
193
194 snapshot!(interpret, @r###"
195 Meta::List {
196 ident: "foo",
197 nested: [
198 Meta(Word("word")),
199 Meta(Meta::NameValue {
200 ident: "name",
201 lit: 5,
202 }),
203 Meta(Meta::List {
204 ident: "list",
205 nested: [
206 Meta(Meta::NameValue {
207 ident: "name2",
208 lit: 6,
209 }),
210 ],
211 }),
212 Meta(Word("word2")),
213 ],
214 ⋮}
215 "###);
216
217 snapshot!(parse, @r###"
218 Meta::List {
219 ident: "foo",
220 nested: [
221 Meta(Word("word")),
222 Meta(Meta::NameValue {
223 ident: "name",
224 lit: 5,
225 }),
226 Meta(Meta::List {
227 ident: "list",
228 nested: [
229 Meta(Meta::NameValue {
230 ident: "name2",
231 lit: 6,
232 }),
233 ],
234 }),
235 Meta(Word("word2")),
236 ],
237 ⋮}
238 "###);
David Tolnay7d8b3312019-03-10 01:26:11 -0800239}
240
241#[test]
242fn test_bool_lit() {
David Tolnayfcd53cf2019-05-09 13:06:59 -0700243 let (interpret, parse) = test("#[foo(true)]");
244
245 snapshot!(interpret, @r###"
246 Meta::List {
247 ident: "foo",
248 nested: [
249 Literal(Lit::Bool {
250 value: true,
251 }),
252 ],
253 ⋮}
254 "###);
255
256 snapshot!(parse, @r###"
257 Meta::List {
258 ident: "foo",
259 nested: [
260 Literal(Lit::Bool {
261 value: true,
262 }),
263 ],
264 ⋮}
265 "###);
David Tolnay7d8b3312019-03-10 01:26:11 -0800266}
267
David Tolnayfcd53cf2019-05-09 13:06:59 -0700268fn test(input: &str) -> (Meta, Meta) {
David Tolnay7d8b3312019-03-10 01:26:11 -0800269 let attrs = Attribute::parse_outer.parse_str(input).unwrap();
270
271 assert_eq!(attrs.len(), 1);
David Tolnay7d8b3312019-03-10 01:26:11 -0800272 let attr = attrs.into_iter().next().unwrap();
David Tolnay7d8b3312019-03-10 01:26:11 -0800273
David Tolnayfcd53cf2019-05-09 13:06:59 -0700274 let interpret = attr.interpret_meta().unwrap();
275 let parse = attr.parse_meta().unwrap();
David Tolnay7d8b3312019-03-10 01:26:11 -0800276 assert_eq!(interpret, parse);
David Tolnayfcd53cf2019-05-09 13:06:59 -0700277
278 (interpret, parse)
David Tolnay7d8b3312019-03-10 01:26:11 -0800279}