rust/crates/hir_def/src/body/tests.rs

82 lines
1.9 KiB
Rust
Raw Normal View History

mod block;
2021-02-09 10:22:57 -06:00
use base_db::{fixture::WithFixture, SourceDatabase};
use expect_test::Expect;
2020-10-23 12:27:04 -05:00
use crate::ModuleDefId;
2020-10-23 12:27:04 -05:00
use super::*;
fn lower(ra_fixture: &str) -> Arc<Body> {
2021-01-21 13:35:36 -06:00
let db = crate::test_db::TestDB::with_files(ra_fixture);
2020-10-23 12:27:04 -05:00
let krate = db.crate_graph().iter().next().unwrap();
let def_map = db.crate_def_map(krate);
2021-01-21 13:35:36 -06:00
let mut fn_def = None;
'outer: for (_, module) in def_map.modules() {
for decl in module.scope.declarations() {
2021-06-07 06:59:01 -05:00
if let ModuleDefId::FunctionId(it) = decl {
fn_def = Some(it);
break 'outer;
2021-01-21 13:35:36 -06:00
}
}
}
db.body(fn_def.unwrap().into())
2020-10-23 12:27:04 -05:00
}
fn block_def_map_at(ra_fixture: &str) -> String {
let (db, position) = crate::test_db::TestDB::with_position(ra_fixture);
2021-02-09 10:22:57 -06:00
let module = db.module_at_position(position);
module.def_map(&db).dump(&db)
}
2021-04-21 10:57:45 -05:00
fn check_block_scopes_at(ra_fixture: &str, expect: Expect) {
let (db, position) = crate::test_db::TestDB::with_position(ra_fixture);
let module = db.module_at_position(position);
let actual = module.def_map(&db).dump_block_scopes(&db);
expect.assert_eq(&actual);
}
fn check_at(ra_fixture: &str, expect: Expect) {
let actual = block_def_map_at(ra_fixture);
expect.assert_eq(&actual);
}
2020-10-23 12:27:04 -05:00
#[test]
fn your_stack_belongs_to_me() {
2021-03-08 14:19:44 -06:00
cov_mark::check!(your_stack_belongs_to_me);
2020-10-23 12:27:04 -05:00
lower(
r#"
2020-10-23 12:27:04 -05:00
macro_rules! n_nuple {
($e:tt) => ();
($($rest:tt)*) => {{
(n_nuple!($($rest)*)None,)
}};
}
fn main() { n_nuple!(1,2,3); }
"#,
2020-10-23 12:27:04 -05:00
);
}
2021-01-21 13:35:36 -06:00
#[test]
fn macro_resolve() {
// Regression test for a path resolution bug introduced with inner item handling.
lower(
r#"
2021-01-21 13:35:36 -06:00
macro_rules! vec {
() => { () };
($elem:expr; $n:expr) => { () };
($($x:expr),+ $(,)?) => { () };
}
mod m {
fn outer() {
let _ = vec![FileSet::default(); self.len()];
}
}
"#,
2021-01-21 13:35:36 -06:00
);
}