2020-04-10 01:43:47 +08:00
|
|
|
//! utils used in proc-macro tests
|
|
|
|
|
|
|
|
use crate::dylib;
|
2020-04-24 10:23:01 +08:00
|
|
|
use crate::ProcMacroSrv;
|
2021-05-31 17:32:56 +02:00
|
|
|
use expect_test::Expect;
|
2020-08-13 12:07:28 +02:00
|
|
|
use proc_macro_api::ListMacrosTask;
|
2020-04-10 01:43:47 +08:00
|
|
|
use std::str::FromStr;
|
|
|
|
|
2021-03-15 23:38:22 +08:00
|
|
|
pub mod fixtures {
|
2020-05-10 06:22:26 +08:00
|
|
|
use cargo_metadata::Message;
|
2021-03-02 14:27:29 +02:00
|
|
|
use std::path::PathBuf;
|
2020-04-10 01:43:47 +08:00
|
|
|
use std::process::Command;
|
|
|
|
|
|
|
|
// Use current project metadata to get the proc-macro dylib path
|
2021-05-31 17:32:56 +02:00
|
|
|
pub fn proc_macro_test_dylib_path() -> std::path::PathBuf {
|
|
|
|
let name = "proc_macro_test";
|
|
|
|
let version = "0.0.0";
|
2020-08-12 16:52:28 +02:00
|
|
|
let command = Command::new(toolchain::cargo())
|
2021-05-31 17:32:56 +02:00
|
|
|
.args(&["build", "-p", name, "--message-format", "json"])
|
2020-04-10 01:43:47 +08:00
|
|
|
.output()
|
|
|
|
.unwrap()
|
|
|
|
.stdout;
|
|
|
|
|
2020-05-10 06:22:26 +08:00
|
|
|
for message in Message::parse_stream(command.as_slice()) {
|
2020-04-10 01:43:47 +08:00
|
|
|
match message.unwrap() {
|
|
|
|
Message::CompilerArtifact(artifact) => {
|
|
|
|
if artifact.target.kind.contains(&"proc-macro".to_string()) {
|
2021-05-31 17:32:56 +02:00
|
|
|
let repr = format!("{} {}", name, version);
|
2020-05-10 06:39:36 +08:00
|
|
|
if artifact.package_id.repr.starts_with(&repr) {
|
2021-03-02 14:27:29 +02:00
|
|
|
return PathBuf::from(&artifact.filenames[0]);
|
2020-04-10 01:43:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => (), // Unknown message
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-31 17:32:56 +02:00
|
|
|
panic!("No proc-macro dylib for {} found!", name);
|
2020-04-10 01:43:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_string(code: &str) -> Option<crate::rustc_server::TokenStream> {
|
|
|
|
Some(crate::rustc_server::TokenStream::from_str(code).unwrap())
|
|
|
|
}
|
|
|
|
|
2021-05-31 17:32:56 +02:00
|
|
|
pub fn assert_expand(macro_name: &str, ra_fixture: &str, expect: Expect) {
|
|
|
|
assert_expand_impl(macro_name, ra_fixture, None, expect);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn assert_expand_attr(macro_name: &str, ra_fixture: &str, attr_args: &str, expect: Expect) {
|
|
|
|
assert_expand_impl(macro_name, ra_fixture, Some(attr_args), expect);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn assert_expand_impl(macro_name: &str, input: &str, attr: Option<&str>, expect: Expect) {
|
|
|
|
let path = fixtures::proc_macro_test_dylib_path();
|
2020-04-10 01:43:47 +08:00
|
|
|
let expander = dylib::Expander::new(&path).unwrap();
|
2021-05-31 17:32:56 +02:00
|
|
|
let fixture = parse_string(input).unwrap();
|
|
|
|
let attr = attr.map(|attr| parse_string(attr).unwrap().into_subtree());
|
2020-04-10 01:43:47 +08:00
|
|
|
|
2021-05-31 17:32:56 +02:00
|
|
|
let res = expander.expand(macro_name, &fixture.into_subtree(), attr.as_ref()).unwrap();
|
|
|
|
expect.assert_eq(&format!("{:?}", res));
|
2020-04-10 01:43:47 +08:00
|
|
|
}
|
|
|
|
|
2021-05-31 17:32:56 +02:00
|
|
|
pub fn list() -> Vec<String> {
|
|
|
|
let path = fixtures::proc_macro_test_dylib_path();
|
2020-04-10 01:43:47 +08:00
|
|
|
let task = ListMacrosTask { lib: path };
|
2020-04-24 10:23:01 +08:00
|
|
|
let mut srv = ProcMacroSrv::default();
|
|
|
|
let res = srv.list_macros(&task).unwrap();
|
2020-04-10 01:43:47 +08:00
|
|
|
res.macros.into_iter().map(|(name, kind)| format!("{} [{:?}]", name, kind)).collect()
|
|
|
|
}
|