]> git.lizzy.rs Git - rust.git/blob - crates/proc-macro-srv/src/tests/mod.rs
6339d56d01791dcfc359bd53f74a0dccad8fb59b
[rust.git] / crates / proc-macro-srv / src / tests / mod.rs
1 //! proc-macro tests
2
3 #[macro_use]
4 mod utils;
5 use expect_test::expect;
6 use paths::AbsPathBuf;
7 use utils::*;
8
9 #[test]
10 fn test_derive_empty() {
11     assert_expand("DeriveEmpty", r#"struct S;"#, expect![[r#"SUBTREE $"#]]);
12 }
13
14 #[test]
15 fn test_derive_error() {
16     assert_expand(
17         "DeriveError",
18         r#"struct S;"#,
19         expect![[r##"
20             SUBTREE $
21               IDENT   compile_error 4294967295
22               PUNCH   ! [joint] 4294967295
23               SUBTREE () 4294967295
24                 LITERAL "#[derive(DeriveError)] struct S ;" 4294967295
25               PUNCH   ; [alone] 4294967295"##]],
26     );
27 }
28
29 #[test]
30 fn test_fn_like_macro_noop() {
31     assert_expand(
32         "fn_like_noop",
33         r#"ident, 0, 1, []"#,
34         expect![[r#"
35             SUBTREE $
36               IDENT   ident 4294967295
37               PUNCH   , [alone] 4294967295
38               LITERAL 0 4294967295
39               PUNCH   , [alone] 4294967295
40               LITERAL 1 4294967295
41               PUNCH   , [alone] 4294967295
42               SUBTREE [] 4294967295"#]],
43     );
44 }
45
46 #[test]
47 fn test_fn_like_macro_clone_ident_subtree() {
48     assert_expand(
49         "fn_like_clone_tokens",
50         r#"ident, []"#,
51         expect![[r#"
52             SUBTREE $
53               IDENT   ident 4294967295
54               PUNCH   , [alone] 4294967295
55               SUBTREE [] 4294967295"#]],
56     );
57 }
58
59 #[test]
60 fn test_fn_like_macro_clone_raw_ident() {
61     assert_expand(
62         "fn_like_clone_tokens",
63         "r#async",
64         expect![[r#"
65             SUBTREE $
66               IDENT   async 4294967295"#]],
67     );
68 }
69
70 #[test]
71 fn test_fn_like_mk_literals() {
72     assert_expand(
73         "fn_like_mk_literals",
74         r#""#,
75         expect![[r#"
76             SUBTREE $
77               LITERAL b"byte_string" 4294967295
78               LITERAL 'c' 4294967295
79               LITERAL "string" 4294967295
80               LITERAL 3.14f64 4294967295
81               LITERAL 3.14 4294967295
82               LITERAL 123i64 4294967295
83               LITERAL 123 4294967295"#]],
84     );
85 }
86
87 #[test]
88 fn test_fn_like_mk_idents() {
89     // FIXME: this test is wrong: raw should be 'r#raw' but ABIs 1.64 and below
90     // simply ignore `is_raw` when implementing the `Ident` interface.
91     assert_expand(
92         "fn_like_mk_idents",
93         r#""#,
94         expect![[r#"
95             SUBTREE $
96               IDENT   standard 4294967295
97               IDENT   raw 4294967295"#]],
98     );
99 }
100
101 #[test]
102 fn test_fn_like_macro_clone_literals() {
103     assert_expand(
104         "fn_like_clone_tokens",
105         r#"1u16, 2_u32, -4i64, 3.14f32, "hello bridge""#,
106         expect![[r#"
107             SUBTREE $
108               LITERAL 1u16 4294967295
109               PUNCH   , [alone] 4294967295
110               LITERAL 2_u32 4294967295
111               PUNCH   , [alone] 4294967295
112               PUNCH   - [joint] 4294967295
113               LITERAL 4i64 4294967295
114               PUNCH   , [alone] 4294967295
115               LITERAL 3.14f32 4294967295
116               PUNCH   , [alone] 4294967295
117               LITERAL "hello bridge" 4294967295"#]],
118     );
119 }
120
121 #[test]
122 fn test_attr_macro() {
123     // Corresponds to
124     //    #[proc_macro_test::attr_error(some arguments)]
125     //    mod m {}
126     assert_expand_attr(
127         "attr_error",
128         r#"mod m {}"#,
129         r#"some arguments"#,
130         expect![[r##"
131             SUBTREE $
132               IDENT   compile_error 4294967295
133               PUNCH   ! [joint] 4294967295
134               SUBTREE () 4294967295
135                 LITERAL "#[attr_error(some arguments)] mod m {}" 4294967295
136               PUNCH   ; [alone] 4294967295"##]],
137     );
138 }
139
140 /// Tests that we find and classify all proc macros correctly.
141 #[test]
142 fn list_test_macros() {
143     let res = list().join("\n");
144
145     expect![[r#"
146         fn_like_noop [FuncLike]
147         fn_like_panic [FuncLike]
148         fn_like_error [FuncLike]
149         fn_like_clone_tokens [FuncLike]
150         fn_like_mk_literals [FuncLike]
151         fn_like_mk_idents [FuncLike]
152         attr_noop [Attr]
153         attr_panic [Attr]
154         attr_error [Attr]
155         DeriveEmpty [CustomDerive]
156         DerivePanic [CustomDerive]
157         DeriveError [CustomDerive]"#]]
158     .assert_eq(&res);
159 }
160
161 #[test]
162 fn test_version_check() {
163     let path = AbsPathBuf::assert(fixtures::proc_macro_test_dylib_path());
164     let info = proc_macro_api::read_dylib_info(&path).unwrap();
165     assert!(info.version.1 >= 50);
166 }