From 349e6c62ada1fa45a8b80edb877b5e7c9d0c306d Mon Sep 17 00:00:00 2001 From: Pavan Kumar Sunkara Date: Thu, 13 Aug 2020 02:57:26 +0200 Subject: Rename ra_proc_macro_srv -> proc_macro_srv --- .../tests/fixtures/test_serialize_proc_macro.txt | 181 +++++++++++++++++++++ crates/proc_macro_srv/src/tests/mod.rs | 45 +++++ crates/proc_macro_srv/src/tests/utils.rs | 64 ++++++++ 3 files changed, 290 insertions(+) create mode 100644 crates/proc_macro_srv/src/tests/fixtures/test_serialize_proc_macro.txt create mode 100644 crates/proc_macro_srv/src/tests/mod.rs create mode 100644 crates/proc_macro_srv/src/tests/utils.rs (limited to 'crates/proc_macro_srv/src/tests') 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 @@ +SUBTREE $ + PUNCH # [alone] 4294967295 + SUBTREE [] 4294967295 + IDENT doc 4294967295 + SUBTREE () 4294967295 + IDENT hidden 4294967295 + PUNCH # [alone] 4294967295 + SUBTREE [] 4294967295 + IDENT allow 4294967295 + SUBTREE () 4294967295 + IDENT non_upper_case_globals 4294967295 + PUNCH , [alone] 4294967295 + IDENT unused_attributes 4294967295 + PUNCH , [alone] 4294967295 + IDENT unused_qualifications 4294967295 + IDENT const 4294967295 + IDENT _ 4294967295 + PUNCH : [alone] 4294967295 + SUBTREE () 4294967295 + PUNCH = [alone] 4294967295 + SUBTREE {} 4294967295 + PUNCH # [alone] 4294967295 + SUBTREE [] 4294967295 + IDENT allow 4294967295 + SUBTREE () 4294967295 + IDENT rust_2018_idioms 4294967295 + PUNCH , [alone] 4294967295 + IDENT clippy 4294967295 + PUNCH : [joint] 4294967295 + PUNCH : [alone] 4294967295 + IDENT useless_attribute 4294967295 + IDENT extern 4294967295 + IDENT crate 4294967295 + IDENT serde 4294967295 + IDENT as 4294967295 + IDENT _serde 4294967295 + PUNCH ; [alone] 4294967295 + PUNCH # [alone] 4294967295 + SUBTREE [] 4294967295 + IDENT allow 4294967295 + SUBTREE () 4294967295 + IDENT unused_macros 4294967295 + IDENT macro_rules 4294967295 + PUNCH ! [alone] 4294967295 + IDENT try 4294967295 + SUBTREE {} 4294967295 + SUBTREE () 4294967295 + PUNCH $ [alone] 4294967295 + IDENT __expr 4294967295 + PUNCH : [alone] 4294967295 + IDENT expr 4294967295 + PUNCH = [joint] 4294967295 + PUNCH > [alone] 4294967295 + SUBTREE {} 4294967295 + IDENT match 4294967295 + PUNCH $ [alone] 4294967295 + IDENT __expr 4294967295 + SUBTREE {} 4294967295 + IDENT _serde 4294967295 + PUNCH : [joint] 4294967295 + PUNCH : [alone] 4294967295 + IDENT export 4294967295 + PUNCH : [joint] 4294967295 + PUNCH : [alone] 4294967295 + IDENT Ok 4294967295 + SUBTREE () 4294967295 + IDENT __val 4294967295 + PUNCH = [joint] 4294967295 + PUNCH > [alone] 4294967295 + IDENT __val 4294967295 + PUNCH , [alone] 4294967295 + IDENT _serde 4294967295 + PUNCH : [joint] 4294967295 + PUNCH : [alone] 4294967295 + IDENT export 4294967295 + PUNCH : [joint] 4294967295 + PUNCH : [alone] 4294967295 + IDENT Err 4294967295 + SUBTREE () 4294967295 + IDENT __err 4294967295 + PUNCH = [joint] 4294967295 + PUNCH > [alone] 4294967295 + SUBTREE {} 4294967295 + IDENT return 4294967295 + IDENT _serde 4294967295 + PUNCH : [joint] 4294967295 + PUNCH : [alone] 4294967295 + IDENT export 4294967295 + PUNCH : [joint] 4294967295 + PUNCH : [alone] 4294967295 + IDENT Err 4294967295 + SUBTREE () 4294967295 + IDENT __err 4294967295 + PUNCH ; [alone] 4294967295 + PUNCH # [alone] 4294967295 + SUBTREE [] 4294967295 + IDENT automatically_derived 4294967295 + IDENT impl 4294967295 + IDENT _serde 4294967295 + PUNCH : [joint] 4294967295 + PUNCH : [alone] 4294967295 + IDENT Serialize 4294967295 + IDENT for 4294967295 + IDENT Foo 1 + SUBTREE {} 4294967295 + IDENT fn 4294967295 + IDENT serialize 4294967295 + PUNCH < [alone] 4294967295 + IDENT __S 4294967295 + PUNCH > [alone] 4294967295 + SUBTREE () 4294967295 + PUNCH & [alone] 4294967295 + IDENT self 4294967295 + PUNCH , [alone] 4294967295 + IDENT __serializer 4294967295 + PUNCH : [alone] 4294967295 + IDENT __S 4294967295 + PUNCH - [joint] 4294967295 + PUNCH > [alone] 4294967295 + IDENT _serde 4294967295 + PUNCH : [joint] 4294967295 + PUNCH : [alone] 4294967295 + IDENT export 4294967295 + PUNCH : [joint] 4294967295 + PUNCH : [alone] 4294967295 + IDENT Result 4294967295 + PUNCH < [alone] 4294967295 + IDENT __S 4294967295 + PUNCH : [joint] 4294967295 + PUNCH : [alone] 4294967295 + IDENT Ok 4294967295 + PUNCH , [alone] 4294967295 + IDENT __S 4294967295 + PUNCH : [joint] 4294967295 + PUNCH : [alone] 4294967295 + IDENT Error 4294967295 + PUNCH > [alone] 4294967295 + IDENT where 4294967295 + IDENT __S 4294967295 + PUNCH : [alone] 4294967295 + IDENT _serde 4294967295 + PUNCH : [joint] 4294967295 + PUNCH : [alone] 4294967295 + IDENT Serializer 4294967295 + PUNCH , [alone] 4294967295 + SUBTREE {} 4294967295 + IDENT let 4294967295 + IDENT __serde_state 4294967295 + PUNCH = [alone] 4294967295 + IDENT try 4294967295 + PUNCH ! [alone] 4294967295 + SUBTREE () 4294967295 + IDENT _serde 4294967295 + PUNCH : [joint] 4294967295 + PUNCH : [alone] 4294967295 + IDENT Serializer 4294967295 + PUNCH : [joint] 4294967295 + PUNCH : [alone] 4294967295 + IDENT serialize_struct 4294967295 + SUBTREE () 4294967295 + IDENT __serializer 4294967295 + PUNCH , [alone] 4294967295 + LITERAL "Foo" 4294967295 + PUNCH , [alone] 4294967295 + IDENT false 4294967295 + IDENT as 4294967295 + IDENT usize 4294967295 + PUNCH ; [alone] 4294967295 + IDENT _serde 4294967295 + PUNCH : [joint] 4294967295 + PUNCH : [alone] 4294967295 + IDENT ser 4294967295 + PUNCH : [joint] 4294967295 + PUNCH : [alone] 4294967295 + IDENT SerializeStruct 4294967295 + PUNCH : [joint] 4294967295 + PUNCH : [alone] 4294967295 + IDENT end 4294967295 + SUBTREE () 4294967295 + IDENT __serde_state 4294967295 + 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 @@ +//! proc-macro tests + +#[macro_use] +mod utils; +use test_utils::assert_eq_text; +use utils::*; + +#[test] +fn test_derive_serialize_proc_macro() { + assert_expand( + "serde_derive", + "Serialize", + "1.0", + r"struct Foo {}", + include_str!("fixtures/test_serialize_proc_macro.txt"), + ); +} + +#[test] +fn test_derive_serialize_proc_macro_failed() { + assert_expand( + "serde_derive", + "Serialize", + "1.0", + r"struct {}", + r##" +SUBTREE $ + IDENT compile_error 4294967295 + PUNCH ! [alone] 4294967295 + SUBTREE {} 4294967295 + LITERAL "expected identifier" 4294967295 +"##, + ); +} + +#[test] +fn test_derive_proc_macro_list() { + let res = list("serde_derive", "1.0").join("\n"); + + assert_eq_text!( + &res, + r#"Serialize [CustomDerive] +Deserialize [CustomDerive]"# + ); +} 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..1b6a0b6fb --- /dev/null +++ b/crates/proc_macro_srv/src/tests/utils.rs @@ -0,0 +1,64 @@ +//! utils used in proc-macro tests + +use crate::dylib; +use crate::ProcMacroSrv; +use ra_proc_macro::ListMacrosTask; +use std::str::FromStr; +use test_utils::assert_eq_text; + +mod fixtures { + use cargo_metadata::Message; + 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 { + let command = Command::new(toolchain::cargo()) + .args(&["check", "--message-format", "json"]) + .output() + .unwrap() + .stdout; + + for message in Message::parse_stream(command.as_slice()) { + match message.unwrap() { + Message::CompilerArtifact(artifact) => { + if artifact.target.kind.contains(&"proc-macro".to_string()) { + let repr = format!("{} {}", crate_name, version); + if artifact.package_id.repr.starts_with(&repr) { + return artifact.filenames[0].clone(); + } + } + } + _ => (), // Unknown message + } + } + + panic!("No proc-macro dylib for {} found!", crate_name); + } +} + +fn parse_string(code: &str) -> Option { + 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.subtree, None).unwrap(); + assert_eq_text!(&format!("{:?}", res), &expect.trim()); +} + +pub fn list(crate_name: &str, version: &str) -> Vec { + let path = fixtures::dylib_path(crate_name, version); + let task = ListMacrosTask { lib: path }; + let mut srv = ProcMacroSrv::default(); + let res = srv.list_macros(&task).unwrap(); + res.macros.into_iter().map(|(name, kind)| format!("{} [{:?}]", name, kind)).collect() +} -- cgit v1.2.3