]> git.proxmox.com Git - rustc.git/blame_incremental - src/tools/rust-analyzer/crates/proc-macro-srv/src/tests/mod.rs
bump version to 1.80.1+dfsg1-1~bpo12+pve1
[rustc.git] / src / tools / rust-analyzer / crates / proc-macro-srv / src / tests / mod.rs
... / ...
CommitLineData
1//! proc-macro tests
2
3#[macro_use]
4mod utils;
5use utils::*;
6
7use expect_test::expect;
8
9#[test]
10fn test_derive_empty() {
11 assert_expand(
12 "DeriveEmpty",
13 r#"struct S;"#,
14 expect!["SUBTREE $$ 1 1"],
15 expect!["SUBTREE $$ 42:2@0..100#0 42:2@0..100#0"],
16 );
17}
18
19#[test]
20fn test_derive_error() {
21 assert_expand(
22 "DeriveError",
23 r#"struct S;"#,
24 expect![[r##"
25 SUBTREE $$ 1 1
26 IDENT compile_error 1
27 PUNCH ! [alone] 1
28 SUBTREE () 1 1
29 LITERAL "#[derive(DeriveError)] struct S ;"1
30 PUNCH ; [alone] 1"##]],
31 expect![[r##"
32 SUBTREE $$ 42:2@0..100#0 42:2@0..100#0
33 IDENT compile_error 42:2@0..100#0
34 PUNCH ! [alone] 42:2@0..100#0
35 SUBTREE () 42:2@0..100#0 42:2@0..100#0
36 LITERAL "#[derive(DeriveError)] struct S ;"42:2@0..100#0
37 PUNCH ; [alone] 42:2@0..100#0"##]],
38 );
39}
40
41#[test]
42fn test_fn_like_macro_noop() {
43 assert_expand(
44 "fn_like_noop",
45 r#"ident, 0, 1, []"#,
46 expect![[r#"
47 SUBTREE $$ 1 1
48 IDENT ident 1
49 PUNCH , [alone] 1
50 LITERAL 01
51 PUNCH , [alone] 1
52 LITERAL 11
53 PUNCH , [alone] 1
54 SUBTREE [] 1 1"#]],
55 expect![[r#"
56 SUBTREE $$ 42:2@0..100#0 42:2@0..100#0
57 IDENT ident 42:2@0..5#0
58 PUNCH , [alone] 42:2@5..6#0
59 LITERAL 042:2@7..8#0
60 PUNCH , [alone] 42:2@8..9#0
61 LITERAL 142:2@10..11#0
62 PUNCH , [alone] 42:2@11..12#0
63 SUBTREE [] 42:2@13..14#0 42:2@14..15#0"#]],
64 );
65}
66
67#[test]
68fn test_fn_like_macro_clone_ident_subtree() {
69 assert_expand(
70 "fn_like_clone_tokens",
71 r#"ident, []"#,
72 expect![[r#"
73 SUBTREE $$ 1 1
74 IDENT ident 1
75 PUNCH , [alone] 1
76 SUBTREE [] 1 1"#]],
77 expect![[r#"
78 SUBTREE $$ 42:2@0..100#0 42:2@0..100#0
79 IDENT ident 42:2@0..5#0
80 PUNCH , [alone] 42:2@5..6#0
81 SUBTREE [] 42:2@7..8#0 42:2@7..8#0"#]],
82 );
83}
84
85#[test]
86fn test_fn_like_macro_clone_raw_ident() {
87 assert_expand(
88 "fn_like_clone_tokens",
89 "r#async",
90 expect![[r#"
91 SUBTREE $$ 1 1
92 IDENT r#async 1"#]],
93 expect![[r#"
94 SUBTREE $$ 42:2@0..100#0 42:2@0..100#0
95 IDENT r#async 42:2@0..7#0"#]],
96 );
97}
98
99#[test]
100fn test_fn_like_fn_like_span_join() {
101 assert_expand(
102 "fn_like_span_join",
103 "foo bar",
104 expect![[r#"
105 SUBTREE $$ 1 1
106 IDENT r#joined 1"#]],
107 expect![[r#"
108 SUBTREE $$ 42:2@0..100#0 42:2@0..100#0
109 IDENT r#joined 42:2@0..11#0"#]],
110 );
111}
112
113#[test]
114fn test_fn_like_fn_like_span_ops() {
115 assert_expand(
116 "fn_like_span_ops",
117 "set_def_site resolved_at_def_site start_span",
118 expect![[r#"
119 SUBTREE $$ 1 1
120 IDENT set_def_site 0
121 IDENT resolved_at_def_site 1
122 IDENT start_span 1"#]],
123 expect![[r#"
124 SUBTREE $$ 42:2@0..100#0 42:2@0..100#0
125 IDENT set_def_site 41:1@0..150#0
126 IDENT resolved_at_def_site 42:2@13..33#0
127 IDENT start_span 42:2@34..34#0"#]],
128 );
129}
130
131#[test]
132fn test_fn_like_mk_literals() {
133 assert_expand(
134 "fn_like_mk_literals",
135 r#""#,
136 expect![[r#"
137 SUBTREE $$ 1 1
138 LITERAL b"byte_string"1
139 LITERAL 'c'1
140 LITERAL "string"1
141 LITERAL 3.14f641
142 LITERAL 3.141
143 LITERAL 123i641
144 LITERAL 1231"#]],
145 expect![[r#"
146 SUBTREE $$ 42:2@0..100#0 42:2@0..100#0
147 LITERAL b"byte_string"42:2@0..100#0
148 LITERAL 'c'42:2@0..100#0
149 LITERAL "string"42:2@0..100#0
150 LITERAL 3.14f6442:2@0..100#0
151 LITERAL 3.1442:2@0..100#0
152 LITERAL 123i6442:2@0..100#0
153 LITERAL 12342:2@0..100#0"#]],
154 );
155}
156
157#[test]
158fn test_fn_like_mk_idents() {
159 assert_expand(
160 "fn_like_mk_idents",
161 r#""#,
162 expect![[r#"
163 SUBTREE $$ 1 1
164 IDENT standard 1
165 IDENT r#raw 1"#]],
166 expect![[r#"
167 SUBTREE $$ 42:2@0..100#0 42:2@0..100#0
168 IDENT standard 42:2@0..100#0
169 IDENT r#raw 42:2@0..100#0"#]],
170 );
171}
172
173#[test]
174fn test_fn_like_macro_clone_literals() {
175 assert_expand(
176 "fn_like_clone_tokens",
177 r###"1u16, 2_u32, -4i64, 3.14f32, "hello bridge", "suffixed"suffix, r##"raw"##, 'a', b'b', c"null""###,
178 expect![[r###"
179 SUBTREE $$ 1 1
180 LITERAL 1u161
181 PUNCH , [alone] 1
182 LITERAL 2_u321
183 PUNCH , [alone] 1
184 PUNCH - [alone] 1
185 LITERAL 4i641
186 PUNCH , [alone] 1
187 LITERAL 3.14f321
188 PUNCH , [alone] 1
189 LITERAL "hello bridge"1
190 PUNCH , [alone] 1
191 LITERAL "suffixed"suffix1
192 PUNCH , [alone] 1
193 LITERAL r##"raw"##1
194 PUNCH , [alone] 1
195 LITERAL 'a'1
196 PUNCH , [alone] 1
197 LITERAL b'b'1
198 PUNCH , [alone] 1
199 LITERAL c"null"1"###]],
200 expect![[r###"
201 SUBTREE $$ 42:2@0..100#0 42:2@0..100#0
202 LITERAL 1u1642:2@0..4#0
203 PUNCH , [alone] 42:2@4..5#0
204 LITERAL 2_u3242:2@6..11#0
205 PUNCH , [alone] 42:2@11..12#0
206 PUNCH - [alone] 42:2@13..14#0
207 LITERAL 4i6442:2@14..18#0
208 PUNCH , [alone] 42:2@18..19#0
209 LITERAL 3.14f3242:2@20..27#0
210 PUNCH , [alone] 42:2@27..28#0
211 LITERAL "hello bridge"42:2@29..43#0
212 PUNCH , [alone] 42:2@43..44#0
213 LITERAL "suffixed"suffix42:2@45..61#0
214 PUNCH , [alone] 42:2@61..62#0
215 LITERAL r##"raw"##42:2@63..73#0
216 PUNCH , [alone] 42:2@73..74#0
217 LITERAL 'a'42:2@75..78#0
218 PUNCH , [alone] 42:2@78..79#0
219 LITERAL b'b'42:2@80..84#0
220 PUNCH , [alone] 42:2@84..85#0
221 LITERAL c"null"42:2@86..93#0"###]],
222 );
223}
224
225#[test]
226fn test_attr_macro() {
227 // Corresponds to
228 // #[proc_macro_test::attr_error(some arguments)]
229 // mod m {}
230 assert_expand_attr(
231 "attr_error",
232 r#"mod m {}"#,
233 r#"some arguments"#,
234 expect![[r##"
235 SUBTREE $$ 1 1
236 IDENT compile_error 1
237 PUNCH ! [alone] 1
238 SUBTREE () 1 1
239 LITERAL "#[attr_error(some arguments)] mod m {}"1
240 PUNCH ; [alone] 1"##]],
241 expect![[r##"
242 SUBTREE $$ 42:2@0..100#0 42:2@0..100#0
243 IDENT compile_error 42:2@0..100#0
244 PUNCH ! [alone] 42:2@0..100#0
245 SUBTREE () 42:2@0..100#0 42:2@0..100#0
246 LITERAL "#[attr_error(some arguments)] mod m {}"42:2@0..100#0
247 PUNCH ; [alone] 42:2@0..100#0"##]],
248 );
249}
250
251/// Tests that we find and classify all proc macros correctly.
252#[test]
253fn list_test_macros() {
254 let res = list().join("\n");
255
256 expect![[r#"
257 fn_like_noop [Bang]
258 fn_like_panic [Bang]
259 fn_like_error [Bang]
260 fn_like_clone_tokens [Bang]
261 fn_like_mk_literals [Bang]
262 fn_like_mk_idents [Bang]
263 fn_like_span_join [Bang]
264 fn_like_span_ops [Bang]
265 attr_noop [Attr]
266 attr_panic [Attr]
267 attr_error [Attr]
268 DeriveEmpty [CustomDerive]
269 DerivePanic [CustomDerive]
270 DeriveError [CustomDerive]"#]]
271 .assert_eq(&res);
272}