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-04-10 01:43:47 +08:00
|
|
|
use std::str::FromStr;
|
|
|
|
|
2021-03-15 23:38:22 +08:00
|
|
|
pub mod fixtures {
|
2021-05-31 17:32:56 +02:00
|
|
|
pub fn proc_macro_test_dylib_path() -> std::path::PathBuf {
|
2021-06-09 17:16:52 +02:00
|
|
|
proc_macro_test::PROC_MACRO_TEST_LOCATION.into()
|
2020-04-10 01:43:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-18 14:58:50 +02:00
|
|
|
fn parse_string(code: &str) -> Option<crate::abis::TestTokenStream> {
|
2021-07-12 15:47:47 +01:00
|
|
|
// This is a bit strange. We need to parse a string into a token stream into
|
|
|
|
// order to create a tt::SubTree from it in fixtures. `into_subtree` is
|
|
|
|
// implemented by all the ABIs we have so we arbitrarily choose one ABI to
|
|
|
|
// write a `parse_string` function for and use that. The tests don't really
|
|
|
|
// care which ABI we're using as the `into_subtree` function isn't part of
|
|
|
|
// the ABI and shouldn't change between ABI versions.
|
2022-07-18 14:58:50 +02:00
|
|
|
crate::abis::TestTokenStream::from_str(code).ok()
|
2020-04-10 01:43:47 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
internal: cleanup proc macro server error handlig
When dealing with proc macros, there are two very different kinds of
errors:
* first, usual errors of "proc macro panicked on this particular input"
* second, the proc macro server might day if the user, eg, kills it
First kind of errors are expected and are a normal output, while the
second kind are genuine IO-errors.
For this reason, we use a curious nested result here: `Result<Result<T,
E1>, E2>` pattern, which is 100% inspired by http://sled.rs/errors.html
2021-08-31 19:01:39 +03:00
|
|
|
pub(crate) fn list() -> Vec<String> {
|
|
|
|
let dylib_path = fixtures::proc_macro_test_dylib_path();
|
2020-04-24 10:23:01 +08:00
|
|
|
let mut srv = ProcMacroSrv::default();
|
internal: cleanup proc macro server error handlig
When dealing with proc macros, there are two very different kinds of
errors:
* first, usual errors of "proc macro panicked on this particular input"
* second, the proc macro server might day if the user, eg, kills it
First kind of errors are expected and are a normal output, while the
second kind are genuine IO-errors.
For this reason, we use a curious nested result here: `Result<Result<T,
E1>, E2>` pattern, which is 100% inspired by http://sled.rs/errors.html
2021-08-31 19:01:39 +03:00
|
|
|
let res = srv.list_macros(&dylib_path).unwrap();
|
|
|
|
res.into_iter().map(|(name, kind)| format!("{} [{:?}]", name, kind)).collect()
|
2020-04-10 01:43:47 +08:00
|
|
|
}
|