aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/module/nameres
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/module/nameres')
-rw-r--r--crates/ra_hir/src/module/nameres/tests.rs127
1 files changed, 127 insertions, 0 deletions
diff --git a/crates/ra_hir/src/module/nameres/tests.rs b/crates/ra_hir/src/module/nameres/tests.rs
new file mode 100644
index 000000000..9ddc32dcd
--- /dev/null
+++ b/crates/ra_hir/src/module/nameres/tests.rs
@@ -0,0 +1,127 @@
1use std::sync::Arc;
2
3use salsa::Database;
4use ra_db::{FilesDatabase, CrateGraph};
5use ra_syntax::SmolStr;
6
7use crate::{
8 self as hir,
9 db::HirDatabase,
10 mock::MockDatabase,
11};
12
13fn item_map(fixture: &str) -> (Arc<hir::ItemMap>, hir::ModuleId) {
14 let (db, pos) = MockDatabase::with_position(fixture);
15 let source_root = db.file_source_root(pos.file_id);
16 let module = hir::source_binder::module_from_position(&db, pos)
17 .unwrap()
18 .unwrap();
19 let module_id = module.module_id;
20 (db.item_map(source_root).unwrap(), module_id)
21}
22
23#[test]
24fn item_map_smoke_test() {
25 let (item_map, module_id) = item_map(
26 "
27 //- /lib.rs
28 mod foo;
29
30 use crate::foo::bar::Baz;
31 <|>
32
33 //- /foo/mod.rs
34 pub mod bar;
35
36 //- /foo/bar.rs
37 pub struct Baz;
38 ",
39 );
40 let name = SmolStr::from("Baz");
41 let resolution = &item_map.per_module[&module_id].items[&name];
42 assert!(resolution.def_id.is_some());
43}
44
45#[test]
46fn item_map_across_crates() {
47 let (mut db, files) = MockDatabase::with_files(
48 "
49 //- /main.rs
50 use test_crate::Baz;
51
52 //- /lib.rs
53 pub struct Baz;
54 ",
55 );
56 let main_id = files.file_id("/main.rs");
57 let lib_id = files.file_id("/lib.rs");
58
59 let mut crate_graph = CrateGraph::default();
60 let main_crate = crate_graph.add_crate_root(main_id);
61 let lib_crate = crate_graph.add_crate_root(lib_id);
62 crate_graph.add_dep(main_crate, "test_crate".into(), lib_crate);
63
64 db.set_crate_graph(crate_graph);
65
66 let source_root = db.file_source_root(main_id);
67 let module = hir::source_binder::module_from_file_id(&db, main_id)
68 .unwrap()
69 .unwrap();
70 let module_id = module.module_id;
71 let item_map = db.item_map(source_root).unwrap();
72
73 let name = SmolStr::from("Baz");
74 let resolution = &item_map.per_module[&module_id].items[&name];
75 assert!(resolution.def_id.is_some());
76}
77
78#[test]
79fn typing_inside_a_function_should_not_invalidate_item_map() {
80 let (mut db, pos) = MockDatabase::with_position(
81 "
82 //- /lib.rs
83 mod foo;<|>
84
85 use crate::foo::bar::Baz;
86
87 fn foo() -> i32 {
88 1 + 1
89 }
90 //- /foo/mod.rs
91 pub mod bar;
92
93 //- /foo/bar.rs
94 pub struct Baz;
95 ",
96 );
97 let source_root = db.file_source_root(pos.file_id);
98 {
99 let events = db.log_executed(|| {
100 db.item_map(source_root).unwrap();
101 });
102 assert!(format!("{:?}", events).contains("item_map"))
103 }
104
105 let new_text = "
106 mod foo;
107
108 use crate::foo::bar::Baz;
109
110 fn foo() -> i32 { 92 }
111 "
112 .to_string();
113
114 db.query_mut(ra_db::FileTextQuery)
115 .set(pos.file_id, Arc::new(new_text));
116
117 {
118 let events = db.log_executed(|| {
119 db.item_map(source_root).unwrap();
120 });
121 assert!(
122 !format!("{:?}", events).contains("_item_map"),
123 "{:#?}",
124 events
125 )
126 }
127}