]> git.lizzy.rs Git - rust.git/blob - crates/hir_def/src/macro_expansion_tests/proc_macros.rs
parameters.split_last()
[rust.git] / crates / hir_def / src / macro_expansion_tests / proc_macros.rs
1 //! Tests for user-defined procedural macros.
2 //!
3 //! Note `//- proc_macros: identity` fixture metas in tests -- we don't use real
4 //! proc-macros here, as that would be slow. Instead, we use several hard-coded
5 //! in-memory macros.
6 use expect_test::expect;
7
8 use crate::macro_expansion_tests::check;
9
10 #[test]
11 fn attribute_macro_attr_censoring() {
12     cov_mark::check!(attribute_macro_attr_censoring);
13     check(
14         r#"
15 //- proc_macros: identity
16 #[attr1] #[proc_macros::identity] #[attr2]
17 struct S;
18 "#,
19         expect![[r##"
20 #[attr1] #[proc_macros::identity] #[attr2]
21 struct S;
22
23 #[attr1]
24 #[attr2] struct S;"##]],
25     );
26 }
27
28 #[test]
29 fn derive_censoring() {
30     cov_mark::check!(derive_censoring);
31     check(
32         r#"
33 //- proc_macros: derive_identity
34 //- minicore:derive
35 #[attr1]
36 #[derive(Foo)]
37 #[derive(proc_macros::DeriveIdentity)]
38 #[derive(Bar)]
39 #[attr2]
40 struct S;
41 "#,
42         expect![[r##"
43 #[attr1]
44 #[derive(Foo)]
45 #[derive(proc_macros::DeriveIdentity)]
46 #[derive(Bar)]
47 #[attr2]
48 struct S;
49
50 #[attr1]
51 #[derive(Bar)]
52 #[attr2] struct S;"##]],
53     );
54 }
55
56 #[test]
57 fn attribute_macro_syntax_completion_1() {
58     // this is just the case where the input is actually valid
59     check(
60         r#"
61 //- proc_macros: identity_when_valid
62 #[proc_macros::identity_when_valid]
63 fn foo() { bar.baz(); blub }
64 "#,
65         expect![[r##"
66 #[proc_macros::identity_when_valid]
67 fn foo() { bar.baz(); blub }
68
69 fn foo() {
70     bar.baz();
71     blub
72 }"##]],
73     );
74 }
75
76 #[test]
77 fn attribute_macro_syntax_completion_2() {
78     // common case of dot completion while typing
79     check(
80         r#"
81 //- proc_macros: identity_when_valid
82 #[proc_macros::identity_when_valid]
83 fn foo() { bar.; blub }
84 "#,
85         expect![[r##"
86 #[proc_macros::identity_when_valid]
87 fn foo() { bar.; blub }
88
89 fn foo() {
90     bar. ;
91     blub
92 }"##]],
93     );
94 }