aboutsummaryrefslogtreecommitdiff
path: root/crates/proc_macro_srv/src/tests
diff options
context:
space:
mode:
Diffstat (limited to 'crates/proc_macro_srv/src/tests')
-rw-r--r--crates/proc_macro_srv/src/tests/fixtures/test_serialize_proc_macro.txt181
-rw-r--r--crates/proc_macro_srv/src/tests/mod.rs45
-rw-r--r--crates/proc_macro_srv/src/tests/utils.rs64
3 files changed, 290 insertions, 0 deletions
diff --git a/crates/proc_macro_srv/src/tests/fixtures/test_serialize_proc_macro.txt b/crates/proc_macro_srv/src/tests/fixtures/test_serialize_proc_macro.txt
new file mode 100644
index 000000000..e6fd21610
--- /dev/null
+++ b/crates/proc_macro_srv/src/tests/fixtures/test_serialize_proc_macro.txt
@@ -0,0 +1,181 @@
1SUBTREE $
2 PUNCH # [alone] 4294967295
3 SUBTREE [] 4294967295
4 IDENT doc 4294967295
5 SUBTREE () 4294967295
6 IDENT hidden 4294967295
7 PUNCH # [alone] 4294967295
8 SUBTREE [] 4294967295
9 IDENT allow 4294967295
10 SUBTREE () 4294967295
11 IDENT non_upper_case_globals 4294967295
12 PUNCH , [alone] 4294967295
13 IDENT unused_attributes 4294967295
14 PUNCH , [alone] 4294967295
15 IDENT unused_qualifications 4294967295
16 IDENT const 4294967295
17 IDENT _ 4294967295
18 PUNCH : [alone] 4294967295
19 SUBTREE () 4294967295
20 PUNCH = [alone] 4294967295
21 SUBTREE {} 4294967295
22 PUNCH # [alone] 4294967295
23 SUBTREE [] 4294967295
24 IDENT allow 4294967295
25 SUBTREE () 4294967295
26 IDENT rust_2018_idioms 4294967295
27 PUNCH , [alone] 4294967295
28 IDENT clippy 4294967295
29 PUNCH : [joint] 4294967295
30 PUNCH : [alone] 4294967295
31 IDENT useless_attribute 4294967295
32 IDENT extern 4294967295
33 IDENT crate 4294967295
34 IDENT serde 4294967295
35 IDENT as 4294967295
36 IDENT _serde 4294967295
37 PUNCH ; [alone] 4294967295
38 PUNCH # [alone] 4294967295
39 SUBTREE [] 4294967295
40 IDENT allow 4294967295
41 SUBTREE () 4294967295
42 IDENT unused_macros 4294967295
43 IDENT macro_rules 4294967295
44 PUNCH ! [alone] 4294967295
45 IDENT try 4294967295
46 SUBTREE {} 4294967295
47 SUBTREE () 4294967295
48 PUNCH $ [alone] 4294967295
49 IDENT __expr 4294967295
50 PUNCH : [alone] 4294967295
51 IDENT expr 4294967295
52 PUNCH = [joint] 4294967295
53 PUNCH > [alone] 4294967295
54 SUBTREE {} 4294967295
55 IDENT match 4294967295
56 PUNCH $ [alone] 4294967295
57 IDENT __expr 4294967295
58 SUBTREE {} 4294967295
59 IDENT _serde 4294967295
60 PUNCH : [joint] 4294967295
61 PUNCH : [alone] 4294967295
62 IDENT export 4294967295
63 PUNCH : [joint] 4294967295
64 PUNCH : [alone] 4294967295
65 IDENT Ok 4294967295
66 SUBTREE () 4294967295
67 IDENT __val 4294967295
68 PUNCH = [joint] 4294967295
69 PUNCH > [alone] 4294967295
70 IDENT __val 4294967295
71 PUNCH , [alone] 4294967295
72 IDENT _serde 4294967295
73 PUNCH : [joint] 4294967295
74 PUNCH : [alone] 4294967295
75 IDENT export 4294967295
76 PUNCH : [joint] 4294967295
77 PUNCH : [alone] 4294967295
78 IDENT Err 4294967295
79 SUBTREE () 4294967295
80 IDENT __err 4294967295
81 PUNCH = [joint] 4294967295
82 PUNCH > [alone] 4294967295
83 SUBTREE {} 4294967295
84 IDENT return 4294967295
85 IDENT _serde 4294967295
86 PUNCH : [joint] 4294967295
87 PUNCH : [alone] 4294967295
88 IDENT export 4294967295
89 PUNCH : [joint] 4294967295
90 PUNCH : [alone] 4294967295
91 IDENT Err 4294967295
92 SUBTREE () 4294967295
93 IDENT __err 4294967295
94 PUNCH ; [alone] 4294967295
95 PUNCH # [alone] 4294967295
96 SUBTREE [] 4294967295
97 IDENT automatically_derived 4294967295
98 IDENT impl 4294967295
99 IDENT _serde 4294967295
100 PUNCH : [joint] 4294967295
101 PUNCH : [alone] 4294967295
102 IDENT Serialize 4294967295
103 IDENT for 4294967295
104 IDENT Foo 1
105 SUBTREE {} 4294967295
106 IDENT fn 4294967295
107 IDENT serialize 4294967295
108 PUNCH < [alone] 4294967295
109 IDENT __S 4294967295
110 PUNCH > [alone] 4294967295
111 SUBTREE () 4294967295
112 PUNCH & [alone] 4294967295
113 IDENT self 4294967295
114 PUNCH , [alone] 4294967295
115 IDENT __serializer 4294967295
116 PUNCH : [alone] 4294967295
117 IDENT __S 4294967295
118 PUNCH - [joint] 4294967295
119 PUNCH > [alone] 4294967295
120 IDENT _serde 4294967295
121 PUNCH : [joint] 4294967295
122 PUNCH : [alone] 4294967295
123 IDENT export 4294967295
124 PUNCH : [joint] 4294967295
125 PUNCH : [alone] 4294967295
126 IDENT Result 4294967295
127 PUNCH < [alone] 4294967295
128 IDENT __S 4294967295
129 PUNCH : [joint] 4294967295
130 PUNCH : [alone] 4294967295
131 IDENT Ok 4294967295
132 PUNCH , [alone] 4294967295
133 IDENT __S 4294967295
134 PUNCH : [joint] 4294967295
135 PUNCH : [alone] 4294967295
136 IDENT Error 4294967295
137 PUNCH > [alone] 4294967295
138 IDENT where 4294967295
139 IDENT __S 4294967295
140 PUNCH : [alone] 4294967295
141 IDENT _serde 4294967295
142 PUNCH : [joint] 4294967295
143 PUNCH : [alone] 4294967295
144 IDENT Serializer 4294967295
145 PUNCH , [alone] 4294967295
146 SUBTREE {} 4294967295
147 IDENT let 4294967295
148 IDENT __serde_state 4294967295
149 PUNCH = [alone] 4294967295
150 IDENT try 4294967295
151 PUNCH ! [alone] 4294967295
152 SUBTREE () 4294967295
153 IDENT _serde 4294967295
154 PUNCH : [joint] 4294967295
155 PUNCH : [alone] 4294967295
156 IDENT Serializer 4294967295
157 PUNCH : [joint] 4294967295
158 PUNCH : [alone] 4294967295
159 IDENT serialize_struct 4294967295
160 SUBTREE () 4294967295
161 IDENT __serializer 4294967295
162 PUNCH , [alone] 4294967295
163 LITERAL "Foo" 4294967295
164 PUNCH , [alone] 4294967295
165 IDENT false 4294967295
166 IDENT as 4294967295
167 IDENT usize 4294967295
168 PUNCH ; [alone] 4294967295
169 IDENT _serde 4294967295
170 PUNCH : [joint] 4294967295
171 PUNCH : [alone] 4294967295
172 IDENT ser 4294967295
173 PUNCH : [joint] 4294967295
174 PUNCH : [alone] 4294967295
175 IDENT SerializeStruct 4294967295
176 PUNCH : [joint] 4294967295
177 PUNCH : [alone] 4294967295
178 IDENT end 4294967295
179 SUBTREE () 4294967295
180 IDENT __serde_state 4294967295
181 PUNCH ; [alone] 4294967295
diff --git a/crates/proc_macro_srv/src/tests/mod.rs b/crates/proc_macro_srv/src/tests/mod.rs
new file mode 100644
index 000000000..8e6f28abd
--- /dev/null
+++ b/crates/proc_macro_srv/src/tests/mod.rs
@@ -0,0 +1,45 @@
1//! proc-macro tests
2
3#[macro_use]
4mod utils;
5use test_utils::assert_eq_text;
6use utils::*;
7
8#[test]
9fn test_derive_serialize_proc_macro() {
10 assert_expand(
11 "serde_derive",
12 "Serialize",
13 "1.0",
14 r"struct Foo {}",
15 include_str!("fixtures/test_serialize_proc_macro.txt"),
16 );
17}
18
19#[test]
20fn test_derive_serialize_proc_macro_failed() {
21 assert_expand(
22 "serde_derive",
23 "Serialize",
24 "1.0",
25 r"struct {}",
26 r##"
27SUBTREE $
28 IDENT compile_error 4294967295
29 PUNCH ! [alone] 4294967295
30 SUBTREE {} 4294967295
31 LITERAL "expected identifier" 4294967295
32"##,
33 );
34}
35
36#[test]
37fn test_derive_proc_macro_list() {
38 let res = list("serde_derive", "1.0").join("\n");
39
40 assert_eq_text!(
41 &res,
42 r#"Serialize [CustomDerive]
43Deserialize [CustomDerive]"#
44 );
45}
diff --git a/crates/proc_macro_srv/src/tests/utils.rs b/crates/proc_macro_srv/src/tests/utils.rs
new file mode 100644
index 000000000..5828512d6
--- /dev/null
+++ b/crates/proc_macro_srv/src/tests/utils.rs
@@ -0,0 +1,64 @@
1//! utils used in proc-macro tests
2
3use crate::dylib;
4use crate::ProcMacroSrv;
5use proc_macro_api::ListMacrosTask;
6use std::str::FromStr;
7use test_utils::assert_eq_text;
8
9mod fixtures {
10 use cargo_metadata::Message;
11 use std::process::Command;
12
13 // Use current project metadata to get the proc-macro dylib path
14 pub fn dylib_path(crate_name: &str, version: &str) -> std::path::PathBuf {
15 let command = Command::new(toolchain::cargo())
16 .args(&["check", "--message-format", "json"])
17 .output()
18 .unwrap()
19 .stdout;
20
21 for message in Message::parse_stream(command.as_slice()) {
22 match message.unwrap() {
23 Message::CompilerArtifact(artifact) => {
24 if artifact.target.kind.contains(&"proc-macro".to_string()) {
25 let repr = format!("{} {}", crate_name, version);
26 if artifact.package_id.repr.starts_with(&repr) {
27 return artifact.filenames[0].clone();
28 }
29 }
30 }
31 _ => (), // Unknown message
32 }
33 }
34
35 panic!("No proc-macro dylib for {} found!", crate_name);
36 }
37}
38
39fn parse_string(code: &str) -> Option<crate::rustc_server::TokenStream> {
40 Some(crate::rustc_server::TokenStream::from_str(code).unwrap())
41}
42
43pub fn assert_expand(
44 crate_name: &str,
45 macro_name: &str,
46 version: &str,
47 ra_fixture: &str,
48 expect: &str,
49) {
50 let path = fixtures::dylib_path(crate_name, version);
51 let expander = dylib::Expander::new(&path).unwrap();
52 let fixture = parse_string(ra_fixture).unwrap();
53
54 let res = expander.expand(macro_name, &fixture.subtree, None).unwrap();
55 assert_eq_text!(&format!("{:?}", res), &expect.trim());
56}
57
58pub fn list(crate_name: &str, version: &str) -> Vec<String> {
59 let path = fixtures::dylib_path(crate_name, version);
60 let task = ListMacrosTask { lib: path };
61 let mut srv = ProcMacroSrv::default();
62 let res = srv.list_macros(&task).unwrap();
63 res.macros.into_iter().map(|(name, kind)| format!("{} [{:?}]", name, kind)).collect()
64}