blob: 97639cf321793e88b2399fbbb51e8ff3686f665d [file] [log] [blame]
David Tolnay7d8b3312019-03-10 01:26:11 -08001extern crate syn;
2
3mod features;
4
5#[macro_use]
6mod macros;
7
David Tolnay7d8b3312019-03-10 01:26:11 -08008use syn::{Meta, MetaList, MetaNameValue, NestedMeta};
David Tolnay7d8b3312019-03-10 01:26:11 -08009
10#[test]
11fn test_parse_meta_item_word() {
David Tolnayfcd53cf2019-05-09 13:06:59 -070012 let input = "hello";
David Tolnay7d8b3312019-03-10 01:26:11 -080013
David Tolnayfcd53cf2019-05-09 13:06:59 -070014 snapshot!(input as Meta, @r###"Word("hello")"###);
David Tolnay7d8b3312019-03-10 01:26:11 -080015}
16
17#[test]
18fn test_parse_meta_name_value() {
David Tolnayfcd53cf2019-05-09 13:06:59 -070019 let input = "foo = 5";
20 let (inner, meta) = (input, input);
21
22 snapshot!(inner as MetaNameValue, @r###"
23 MetaNameValue {
24 ident: "foo",
25 lit: 5,
26 ⋮}
27 "###);
28
29 snapshot!(meta as Meta, @r###"
30 Meta::NameValue {
31 ident: "foo",
32 lit: 5,
33 ⋮}
34 "###);
35
36 assert_eq!(meta, inner.into());
David Tolnay7d8b3312019-03-10 01:26:11 -080037}
38
39#[test]
40fn test_parse_meta_name_value_with_keyword() {
David Tolnayfcd53cf2019-05-09 13:06:59 -070041 let input = "static = 5";
42 let (inner, meta) = (input, input);
43
44 snapshot!(inner as MetaNameValue, @r###"
45 MetaNameValue {
46 ident: "static",
47 lit: 5,
48 ⋮}
49 "###);
50
51 snapshot!(meta as Meta, @r###"
52 Meta::NameValue {
53 ident: "static",
54 lit: 5,
55 ⋮}
56 "###);
57
58 assert_eq!(meta, inner.into());
David Tolnay7d8b3312019-03-10 01:26:11 -080059}
60
61#[test]
62fn test_parse_meta_name_value_with_bool() {
David Tolnayfcd53cf2019-05-09 13:06:59 -070063 let input = "true = 5";
64 let (inner, meta) = (input, input);
65
66 snapshot!(inner as MetaNameValue, @r###"
67 MetaNameValue {
68 ident: "true",
69 lit: 5,
70 ⋮}
71 "###);
72
73 snapshot!(meta as Meta, @r###"
74 Meta::NameValue {
75 ident: "true",
76 lit: 5,
77 ⋮}
78 "###);
79
80 assert_eq!(meta, inner.into());
David Tolnay7d8b3312019-03-10 01:26:11 -080081}
82
83#[test]
84fn test_parse_meta_item_list_lit() {
David Tolnayfcd53cf2019-05-09 13:06:59 -070085 let input = "foo(5)";
86 let (inner, meta) = (input, input);
87
88 snapshot!(inner as MetaList, @r###"
89 MetaList {
90 ident: "foo",
91 nested: [
92 Literal(5),
93 ],
94 ⋮}
95 "###);
96
97 snapshot!(meta as Meta, @r###"
98 Meta::List {
99 ident: "foo",
100 nested: [
101 Literal(5),
102 ],
103 ⋮}
104 "###);
105
106 assert_eq!(meta, inner.into());
David Tolnay7d8b3312019-03-10 01:26:11 -0800107}
108
109#[test]
110fn test_parse_meta_item_multiple() {
David Tolnayfcd53cf2019-05-09 13:06:59 -0700111 let input = "foo(word, name = 5, list(name2 = 6), word2)";
112 let (inner, meta) = (input, input);
113
114 snapshot!(inner as MetaList, @r###"
115 MetaList {
116 ident: "foo",
117 nested: [
118 Meta(Word("word")),
119 Meta(Meta::NameValue {
120 ident: "name",
121 lit: 5,
122 }),
123 Meta(Meta::List {
124 ident: "list",
125 nested: [
126 Meta(Meta::NameValue {
127 ident: "name2",
128 lit: 6,
129 }),
130 ],
131 }),
132 Meta(Word("word2")),
133 ],
134 ⋮}
135 "###);
136
137 snapshot!(meta as Meta, @r###"
138 Meta::List {
139 ident: "foo",
140 nested: [
141 Meta(Word("word")),
142 Meta(Meta::NameValue {
143 ident: "name",
144 lit: 5,
145 }),
146 Meta(Meta::List {
147 ident: "list",
148 nested: [
149 Meta(Meta::NameValue {
150 ident: "name2",
151 lit: 6,
152 }),
153 ],
154 }),
155 Meta(Word("word2")),
156 ],
157 ⋮}
158 "###);
159
160 assert_eq!(meta, inner.into());
David Tolnay7d8b3312019-03-10 01:26:11 -0800161}
162
163#[test]
164fn test_parse_nested_meta() {
David Tolnayfcd53cf2019-05-09 13:06:59 -0700165 let input = "5";
166 snapshot!(input as NestedMeta, @"Literal(5)");
David Tolnay7d8b3312019-03-10 01:26:11 -0800167
David Tolnayfcd53cf2019-05-09 13:06:59 -0700168 let input = "list(name2 = 6)";
169 snapshot!(input as NestedMeta, @r###"
170 Meta(Meta::List {
171 ident: "list",
172 nested: [
173 Meta(Meta::NameValue {
174 ident: "name2",
175 lit: 6,
176 }),
177 ],
178 ⋮})
179 "###);
David Tolnay7d8b3312019-03-10 01:26:11 -0800180}