]> git.lizzy.rs Git - rust.git/blob - crates/hir_def/src/body/tests.rs
Merge #9258
[rust.git] / crates / hir_def / src / body / tests.rs
1 mod block;
2
3 use base_db::{fixture::WithFixture, SourceDatabase};
4 use expect_test::Expect;
5
6 use crate::ModuleDefId;
7
8 use super::*;
9
10 fn lower(ra_fixture: &str) -> Arc<Body> {
11     let db = crate::test_db::TestDB::with_files(ra_fixture);
12
13     let krate = db.crate_graph().iter().next().unwrap();
14     let def_map = db.crate_def_map(krate);
15     let mut fn_def = None;
16     'outer: for (_, module) in def_map.modules() {
17         for decl in module.scope.declarations() {
18             match decl {
19                 ModuleDefId::FunctionId(it) => {
20                     fn_def = Some(it);
21                     break 'outer;
22                 }
23                 _ => {}
24             }
25         }
26     }
27
28     db.body(fn_def.unwrap().into())
29 }
30
31 fn block_def_map_at(ra_fixture: &str) -> String {
32     let (db, position) = crate::test_db::TestDB::with_position(ra_fixture);
33
34     let module = db.module_at_position(position);
35     module.def_map(&db).dump(&db)
36 }
37
38 fn check_block_scopes_at(ra_fixture: &str, expect: Expect) {
39     let (db, position) = crate::test_db::TestDB::with_position(ra_fixture);
40
41     let module = db.module_at_position(position);
42     let actual = module.def_map(&db).dump_block_scopes(&db);
43     expect.assert_eq(&actual);
44 }
45
46 fn check_at(ra_fixture: &str, expect: Expect) {
47     let actual = block_def_map_at(ra_fixture);
48     expect.assert_eq(&actual);
49 }
50
51 #[test]
52 fn your_stack_belongs_to_me() {
53     cov_mark::check!(your_stack_belongs_to_me);
54     lower(
55         r#"
56 macro_rules! n_nuple {
57     ($e:tt) => ();
58     ($($rest:tt)*) => {{
59         (n_nuple!($($rest)*)None,)
60     }};
61 }
62 fn main() { n_nuple!(1,2,3); }
63 "#,
64     );
65 }
66
67 #[test]
68 fn macro_resolve() {
69     // Regression test for a path resolution bug introduced with inner item handling.
70     lower(
71         r#"
72 macro_rules! vec {
73     () => { () };
74     ($elem:expr; $n:expr) => { () };
75     ($($x:expr),+ $(,)?) => { () };
76 }
77 mod m {
78     fn outer() {
79         let _ = vec![FileSet::default(); self.len()];
80     }
81 }
82 "#,
83     );
84 }