aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorJonas Schievink <[email protected]>2021-05-31 16:32:56 +0100
committerJonas Schievink <[email protected]>2021-05-31 16:32:56 +0100
commitcfcadcb2959d2dbaf7d95e8b9a33c48b75480e9f (patch)
tree65fdc84b5cb70a59b67ce7464f62dfe9c5da8eec /crates
parent70e347332d8b37c374db949bd966ff0bec8b32ab (diff)
Simplify proc_macro_srv tests
Diffstat (limited to 'crates')
-rw-r--r--crates/proc_macro_srv/Cargo.toml5
-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.rs92
-rw-r--r--crates/proc_macro_srv/src/tests/utils.rs41
-rw-r--r--crates/proc_macro_test/src/lib.rs38
5 files changed, 116 insertions, 241 deletions
diff --git a/crates/proc_macro_srv/Cargo.toml b/crates/proc_macro_srv/Cargo.toml
index 4ea41175e..c15cb8821 100644
--- a/crates/proc_macro_srv/Cargo.toml
+++ b/crates/proc_macro_srv/Cargo.toml
@@ -20,10 +20,9 @@ proc_macro_api = { path = "../proc_macro_api", version = "0.0.0" }
20 20
21[dev-dependencies] 21[dev-dependencies]
22test_utils = { path = "../test_utils" } 22test_utils = { path = "../test_utils" }
23toolchain = { path = "../toolchain" }
23cargo_metadata = "0.13" 24cargo_metadata = "0.13"
25expect-test = "1.1.0"
24 26
25# used as proc macro test targets 27# used as proc macro test targets
26serde_derive = "1.0.106"
27proc_macro_test = { path = "../proc_macro_test" } 28proc_macro_test = { path = "../proc_macro_test" }
28
29toolchain = { path = "../toolchain" }
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
deleted file mode 100644
index eb67c7134..000000000
--- a/crates/proc_macro_srv/src/tests/fixtures/test_serialize_proc_macro.txt
+++ /dev/null
@@ -1,181 +0,0 @@
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 unused_extern_crates 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 __private 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 __private 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 __private 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 4294967295
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 __private 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
index b4ab4c077..5ca2b8a75 100644
--- a/crates/proc_macro_srv/src/tests/mod.rs
+++ b/crates/proc_macro_srv/src/tests/mod.rs
@@ -2,64 +2,86 @@
2 2
3#[macro_use] 3#[macro_use]
4mod utils; 4mod utils;
5use test_utils::assert_eq_text; 5use expect_test::expect;
6use utils::*; 6use utils::*;
7 7
8#[test] 8#[test]
9fn test_derive_serialize_proc_macro() { 9fn test_derive_empty() {
10 assert_expand("DeriveEmpty", r#"struct S;"#, expect![[r#"SUBTREE $"#]]);
11}
12
13#[test]
14fn test_derive_error() {
10 assert_expand( 15 assert_expand(
11 "serde_derive", 16 "DeriveError",
12 "Serialize", 17 r#"struct S;"#,
13 "1.0", 18 expect![[r##"
14 r"struct Foo {}", 19 SUBTREE $
15 include_str!("fixtures/test_serialize_proc_macro.txt"), 20 IDENT compile_error 4294967295
21 PUNCH ! [alone] 4294967295
22 SUBTREE () 4294967295
23 LITERAL "#[derive(DeriveError)] struct S ;" 4294967295
24 PUNCH ; [alone] 4294967295"##]],
16 ); 25 );
17} 26}
18 27
19#[test] 28#[test]
20fn test_derive_serialize_proc_macro_failed() { 29fn test_fn_like_macro() {
21 assert_expand( 30 assert_expand(
22 "serde_derive", 31 "fn_like_noop",
23 "Serialize", 32 r#"ident, 0, 1, []"#,
24 "1.0", 33 expect![[r#"
25 r"struct {}", 34 SUBTREE $
26 r##" 35 IDENT ident 4294967295
27SUBTREE $ 36 PUNCH , [alone] 4294967295
28 IDENT compile_error 4294967295 37 LITERAL 0 4294967295
29 PUNCH ! [alone] 4294967295 38 PUNCH , [alone] 4294967295
30 SUBTREE {} 4294967295 39 LITERAL 1 4294967295
31 LITERAL "expected identifier" 4294967295 40 PUNCH , [alone] 4294967295
32"##, 41 SUBTREE [] 4294967295"#]],
33 ); 42 );
34} 43}
35 44
36#[test] 45#[test]
37fn test_derive_proc_macro_list() { 46fn test_attr_macro() {
38 let res = list("serde_derive", "1").join("\n"); 47 // Corresponds to
39 48 // #[proc_macro_test::attr_error(some arguments)]
40 assert_eq_text!( 49 // mod m {}
41 r#"Serialize [CustomDerive] 50 assert_expand_attr(
42Deserialize [CustomDerive]"#, 51 "attr_error",
43 &res 52 r#"mod m {}"#,
53 r#"some arguments"#,
54 expect![[r##"
55 SUBTREE $
56 IDENT compile_error 4294967295
57 PUNCH ! [alone] 4294967295
58 SUBTREE () 4294967295
59 LITERAL "#[attr_error(some arguments)] mod m {}" 4294967295
60 PUNCH ; [alone] 4294967295"##]],
44 ); 61 );
45} 62}
46 63
47/// Tests that we find and classify non-derive macros correctly. 64/// Tests that we find and classify all proc macros correctly.
48#[test] 65#[test]
49fn list_test_macros() { 66fn list_test_macros() {
50 let res = list("proc_macro_test", "0.0.0").join("\n"); 67 let res = list().join("\n");
51 68
52 assert_eq_text!( 69 expect![[r#"
53 r#"function_like_macro [FuncLike] 70 fn_like_noop [FuncLike]
54attribute_macro [Attr] 71 fn_like_panic [FuncLike]
55DummyTrait [CustomDerive]"#, 72 fn_like_error [FuncLike]
56 &res 73 attr_noop [Attr]
57 ); 74 attr_panic [Attr]
75 attr_error [Attr]
76 DeriveEmpty [CustomDerive]
77 DerivePanic [CustomDerive]
78 DeriveError [CustomDerive]"#]]
79 .assert_eq(&res);
58} 80}
59 81
60#[test] 82#[test]
61fn test_version_check() { 83fn test_version_check() {
62 let path = fixtures::dylib_path("proc_macro_test", "0.0.0"); 84 let path = fixtures::proc_macro_test_dylib_path();
63 let info = proc_macro_api::read_dylib_info(&path).unwrap(); 85 let info = proc_macro_api::read_dylib_info(&path).unwrap();
64 assert!(info.version.1 >= 50); 86 assert!(info.version.1 >= 50);
65} 87}
diff --git a/crates/proc_macro_srv/src/tests/utils.rs b/crates/proc_macro_srv/src/tests/utils.rs
index f15381f0f..f0a514b32 100644
--- a/crates/proc_macro_srv/src/tests/utils.rs
+++ b/crates/proc_macro_srv/src/tests/utils.rs
@@ -2,9 +2,9 @@
2 2
3use crate::dylib; 3use crate::dylib;
4use crate::ProcMacroSrv; 4use crate::ProcMacroSrv;
5use expect_test::Expect;
5use proc_macro_api::ListMacrosTask; 6use proc_macro_api::ListMacrosTask;
6use std::str::FromStr; 7use std::str::FromStr;
7use test_utils::assert_eq_text;
8 8
9pub mod fixtures { 9pub mod fixtures {
10 use cargo_metadata::Message; 10 use cargo_metadata::Message;
@@ -12,9 +12,11 @@ pub mod fixtures {
12 use std::process::Command; 12 use std::process::Command;
13 13
14 // Use current project metadata to get the proc-macro dylib path 14 // Use current project metadata to get the proc-macro dylib path
15 pub fn dylib_path(crate_name: &str, version: &str) -> std::path::PathBuf { 15 pub fn proc_macro_test_dylib_path() -> std::path::PathBuf {
16 let name = "proc_macro_test";
17 let version = "0.0.0";
16 let command = Command::new(toolchain::cargo()) 18 let command = Command::new(toolchain::cargo())
17 .args(&["check", "--tests", "--message-format", "json"]) 19 .args(&["build", "-p", name, "--message-format", "json"])
18 .output() 20 .output()
19 .unwrap() 21 .unwrap()
20 .stdout; 22 .stdout;
@@ -23,7 +25,7 @@ pub mod fixtures {
23 match message.unwrap() { 25 match message.unwrap() {
24 Message::CompilerArtifact(artifact) => { 26 Message::CompilerArtifact(artifact) => {
25 if artifact.target.kind.contains(&"proc-macro".to_string()) { 27 if artifact.target.kind.contains(&"proc-macro".to_string()) {
26 let repr = format!("{} {}", crate_name, version); 28 let repr = format!("{} {}", name, version);
27 if artifact.package_id.repr.starts_with(&repr) { 29 if artifact.package_id.repr.starts_with(&repr) {
28 return PathBuf::from(&artifact.filenames[0]); 30 return PathBuf::from(&artifact.filenames[0]);
29 } 31 }
@@ -33,7 +35,7 @@ pub mod fixtures {
33 } 35 }
34 } 36 }
35 37
36 panic!("No proc-macro dylib for {} found!", crate_name); 38 panic!("No proc-macro dylib for {} found!", name);
37 } 39 }
38} 40}
39 41
@@ -41,23 +43,26 @@ fn parse_string(code: &str) -> Option<crate::rustc_server::TokenStream> {
41 Some(crate::rustc_server::TokenStream::from_str(code).unwrap()) 43 Some(crate::rustc_server::TokenStream::from_str(code).unwrap())
42} 44}
43 45
44pub fn assert_expand( 46pub fn assert_expand(macro_name: &str, ra_fixture: &str, expect: Expect) {
45 crate_name: &str, 47 assert_expand_impl(macro_name, ra_fixture, None, expect);
46 macro_name: &str, 48}
47 version: &str, 49
48 ra_fixture: &str, 50pub fn assert_expand_attr(macro_name: &str, ra_fixture: &str, attr_args: &str, expect: Expect) {
49 expect: &str, 51 assert_expand_impl(macro_name, ra_fixture, Some(attr_args), expect);
50) { 52}
51 let path = fixtures::dylib_path(crate_name, version); 53
54fn assert_expand_impl(macro_name: &str, input: &str, attr: Option<&str>, expect: Expect) {
55 let path = fixtures::proc_macro_test_dylib_path();
52 let expander = dylib::Expander::new(&path).unwrap(); 56 let expander = dylib::Expander::new(&path).unwrap();
53 let fixture = parse_string(ra_fixture).unwrap(); 57 let fixture = parse_string(input).unwrap();
58 let attr = attr.map(|attr| parse_string(attr).unwrap().into_subtree());
54 59
55 let res = expander.expand(macro_name, &fixture.into_subtree(), None).unwrap(); 60 let res = expander.expand(macro_name, &fixture.into_subtree(), attr.as_ref()).unwrap();
56 assert_eq_text!(&expect.trim(), &format!("{:?}", res)); 61 expect.assert_eq(&format!("{:?}", res));
57} 62}
58 63
59pub fn list(crate_name: &str, version: &str) -> Vec<String> { 64pub fn list() -> Vec<String> {
60 let path = fixtures::dylib_path(crate_name, version); 65 let path = fixtures::proc_macro_test_dylib_path();
61 let task = ListMacrosTask { lib: path }; 66 let task = ListMacrosTask { lib: path };
62 let mut srv = ProcMacroSrv::default(); 67 let mut srv = ProcMacroSrv::default();
63 let res = srv.list_macros(&task).unwrap(); 68 let res = srv.list_macros(&task).unwrap();
diff --git a/crates/proc_macro_test/src/lib.rs b/crates/proc_macro_test/src/lib.rs
index ec2a114a3..4b26d2472 100644
--- a/crates/proc_macro_test/src/lib.rs
+++ b/crates/proc_macro_test/src/lib.rs
@@ -3,16 +3,46 @@
3use proc_macro::TokenStream; 3use proc_macro::TokenStream;
4 4
5#[proc_macro] 5#[proc_macro]
6pub fn function_like_macro(args: TokenStream) -> TokenStream { 6pub fn fn_like_noop(args: TokenStream) -> TokenStream {
7 args 7 args
8} 8}
9 9
10#[proc_macro]
11pub fn fn_like_panic(args: TokenStream) -> TokenStream {
12 panic!("fn_like_panic!({})", args);
13}
14
15#[proc_macro]
16pub fn fn_like_error(args: TokenStream) -> TokenStream {
17 format!("compile_error!(\"fn_like_error!({})\");", args).parse().unwrap()
18}
19
10#[proc_macro_attribute] 20#[proc_macro_attribute]
11pub fn attribute_macro(_args: TokenStream, item: TokenStream) -> TokenStream { 21pub fn attr_noop(_args: TokenStream, item: TokenStream) -> TokenStream {
12 item 22 item
13} 23}
14 24
15#[proc_macro_derive(DummyTrait)] 25#[proc_macro_attribute]
16pub fn derive_macro(_item: TokenStream) -> TokenStream { 26pub fn attr_panic(args: TokenStream, item: TokenStream) -> TokenStream {
27 panic!("#[attr_panic {}] {}", args, item);
28}
29
30#[proc_macro_attribute]
31pub fn attr_error(args: TokenStream, item: TokenStream) -> TokenStream {
32 format!("compile_error!(\"#[attr_error({})] {}\");", args, item).parse().unwrap()
33}
34
35#[proc_macro_derive(DeriveEmpty)]
36pub fn derive_empty(_item: TokenStream) -> TokenStream {
17 TokenStream::new() 37 TokenStream::new()
18} 38}
39
40#[proc_macro_derive(DerivePanic)]
41pub fn derive_panic(item: TokenStream) -> TokenStream {
42 panic!("#[derive(DerivePanic)] {}", item);
43}
44
45#[proc_macro_derive(DeriveError)]
46pub fn derive_error(item: TokenStream) -> TokenStream {
47 format!("compile_error!(\"#[derive(DeriveError)] {}\");", item).parse().unwrap()
48}