mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-03 23:25:03 +00:00
Simplify proc_macro_srv tests
This commit is contained in:
parent
70e347332d
commit
cfcadcb295
6 changed files with 119 additions and 244 deletions
|
@ -2,9 +2,9 @@
|
|||
|
||||
use crate::dylib;
|
||||
use crate::ProcMacroSrv;
|
||||
use expect_test::Expect;
|
||||
use proc_macro_api::ListMacrosTask;
|
||||
use std::str::FromStr;
|
||||
use test_utils::assert_eq_text;
|
||||
|
||||
pub mod fixtures {
|
||||
use cargo_metadata::Message;
|
||||
|
@ -12,9 +12,11 @@ pub mod fixtures {
|
|||
use std::process::Command;
|
||||
|
||||
// Use current project metadata to get the proc-macro dylib path
|
||||
pub fn dylib_path(crate_name: &str, version: &str) -> std::path::PathBuf {
|
||||
pub fn proc_macro_test_dylib_path() -> std::path::PathBuf {
|
||||
let name = "proc_macro_test";
|
||||
let version = "0.0.0";
|
||||
let command = Command::new(toolchain::cargo())
|
||||
.args(&["check", "--tests", "--message-format", "json"])
|
||||
.args(&["build", "-p", name, "--message-format", "json"])
|
||||
.output()
|
||||
.unwrap()
|
||||
.stdout;
|
||||
|
@ -23,7 +25,7 @@ pub mod fixtures {
|
|||
match message.unwrap() {
|
||||
Message::CompilerArtifact(artifact) => {
|
||||
if artifact.target.kind.contains(&"proc-macro".to_string()) {
|
||||
let repr = format!("{} {}", crate_name, version);
|
||||
let repr = format!("{} {}", name, version);
|
||||
if artifact.package_id.repr.starts_with(&repr) {
|
||||
return PathBuf::from(&artifact.filenames[0]);
|
||||
}
|
||||
|
@ -33,7 +35,7 @@ pub mod fixtures {
|
|||
}
|
||||
}
|
||||
|
||||
panic!("No proc-macro dylib for {} found!", crate_name);
|
||||
panic!("No proc-macro dylib for {} found!", name);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -41,23 +43,26 @@ fn parse_string(code: &str) -> Option<crate::rustc_server::TokenStream> {
|
|||
Some(crate::rustc_server::TokenStream::from_str(code).unwrap())
|
||||
}
|
||||
|
||||
pub fn assert_expand(
|
||||
crate_name: &str,
|
||||
macro_name: &str,
|
||||
version: &str,
|
||||
ra_fixture: &str,
|
||||
expect: &str,
|
||||
) {
|
||||
let path = fixtures::dylib_path(crate_name, version);
|
||||
let expander = dylib::Expander::new(&path).unwrap();
|
||||
let fixture = parse_string(ra_fixture).unwrap();
|
||||
|
||||
let res = expander.expand(macro_name, &fixture.into_subtree(), None).unwrap();
|
||||
assert_eq_text!(&expect.trim(), &format!("{:?}", res));
|
||||
pub fn assert_expand(macro_name: &str, ra_fixture: &str, expect: Expect) {
|
||||
assert_expand_impl(macro_name, ra_fixture, None, expect);
|
||||
}
|
||||
|
||||
pub fn list(crate_name: &str, version: &str) -> Vec<String> {
|
||||
let path = fixtures::dylib_path(crate_name, version);
|
||||
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();
|
||||
let expander = dylib::Expander::new(&path).unwrap();
|
||||
let fixture = parse_string(input).unwrap();
|
||||
let attr = attr.map(|attr| parse_string(attr).unwrap().into_subtree());
|
||||
|
||||
let res = expander.expand(macro_name, &fixture.into_subtree(), attr.as_ref()).unwrap();
|
||||
expect.assert_eq(&format!("{:?}", res));
|
||||
}
|
||||
|
||||
pub fn list() -> Vec<String> {
|
||||
let path = fixtures::proc_macro_test_dylib_path();
|
||||
let task = ListMacrosTask { lib: path };
|
||||
let mut srv = ProcMacroSrv::default();
|
||||
let res = srv.list_macros(&task).unwrap();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue