aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorErlend Tobiassen <[email protected]>2019-01-27 13:51:26 +0000
committerErlend Tobiassen <[email protected]>2019-01-27 14:15:56 +0000
commit683e5e64f42b6c74596015c5d19e2eab186fe68d (patch)
tree4cb6df38e804232bf504e9dd4b92694a5310beb4
parent964086e0d4874d7e60eb3607220e486ec4b51f86 (diff)
Test non standard crate root
-rw-r--r--crates/ra_hir/src/mock.rs16
-rw-r--r--crates/ra_hir/src/nameres/tests.rs38
2 files changed, 48 insertions, 6 deletions
diff --git a/crates/ra_hir/src/mock.rs b/crates/ra_hir/src/mock.rs
index 7da15eca0..2bba11e42 100644
--- a/crates/ra_hir/src/mock.rs
+++ b/crates/ra_hir/src/mock.rs
@@ -2,7 +2,8 @@ use std::{sync::Arc, panic};
2 2
3use parking_lot::Mutex; 3use parking_lot::Mutex;
4use ra_db::{ 4use ra_db::{
5 CheckCanceled, FilePosition, FileId, CrateGraph, SourceRoot, SourceRootId, SourceDatabase, salsa, 5 mock::FileMap, CheckCanceled, CrateGraph, FileId, FilePosition, SourceDatabase,
6 SourceRoot, SourceRootId, salsa
6}; 7};
7use relative_path::RelativePathBuf; 8use relative_path::RelativePathBuf;
8use test_utils::{parse_fixture, CURSOR_MARKER, extract_offset}; 9use test_utils::{parse_fixture, CURSOR_MARKER, extract_offset};
@@ -17,7 +18,7 @@ pub(crate) struct MockDatabase {
17 events: Mutex<Option<Vec<salsa::Event<MockDatabase>>>>, 18 events: Mutex<Option<Vec<salsa::Event<MockDatabase>>>>,
18 runtime: salsa::Runtime<MockDatabase>, 19 runtime: salsa::Runtime<MockDatabase>,
19 interner: Arc<HirInterner>, 20 interner: Arc<HirInterner>,
20 file_counter: u32, 21 file_map: Arc<FileMap>,
21} 22}
22 23
23impl panic::RefUnwindSafe for MockDatabase {} 24impl panic::RefUnwindSafe for MockDatabase {}
@@ -43,6 +44,10 @@ impl MockDatabase {
43 (db, position) 44 (db, position)
44 } 45 }
45 46
47 pub(crate) fn file_id(&self, file: &str) -> FileId {
48 self.file_map.file_id(file)
49 }
50
46 fn from_fixture(fixture: &str) -> (MockDatabase, SourceRoot, Option<FilePosition>) { 51 fn from_fixture(fixture: &str) -> (MockDatabase, SourceRoot, Option<FilePosition>) {
47 let mut db = MockDatabase::default(); 52 let mut db = MockDatabase::default();
48 53
@@ -89,8 +94,7 @@ impl MockDatabase {
89 let is_crate_root = path == "/lib.rs" || path == "/main.rs"; 94 let is_crate_root = path == "/lib.rs" || path == "/main.rs";
90 95
91 let path = RelativePathBuf::from_path(&path[1..]).unwrap(); 96 let path = RelativePathBuf::from_path(&path[1..]).unwrap();
92 let file_id = FileId(self.file_counter); 97 let file_id = Arc::make_mut(&mut self.file_map).add(path.clone());
93 self.file_counter += 1;
94 let text = Arc::new(text.to_string()); 98 let text = Arc::new(text.to_string());
95 self.set_file_text(file_id, text); 99 self.set_file_text(file_id, text);
96 self.set_file_relative_path(file_id, path.clone()); 100 self.set_file_relative_path(file_id, path.clone());
@@ -137,7 +141,7 @@ impl Default for MockDatabase {
137 events: Default::default(), 141 events: Default::default(),
138 runtime: salsa::Runtime::default(), 142 runtime: salsa::Runtime::default(),
139 interner: Default::default(), 143 interner: Default::default(),
140 file_counter: 0, 144 file_map: Default::default(),
141 }; 145 };
142 db.set_crate_graph(Default::default()); 146 db.set_crate_graph(Default::default());
143 db 147 db
@@ -150,7 +154,7 @@ impl salsa::ParallelDatabase for MockDatabase {
150 events: Default::default(), 154 events: Default::default(),
151 runtime: self.runtime.snapshot(self), 155 runtime: self.runtime.snapshot(self),
152 interner: Arc::clone(&self.interner), 156 interner: Arc::clone(&self.interner),
153 file_counter: self.file_counter, 157 file_map: Arc::clone(&self.file_map),
154 }) 158 })
155 } 159 }
156} 160}
diff --git a/crates/ra_hir/src/nameres/tests.rs b/crates/ra_hir/src/nameres/tests.rs
index e72781f51..5b161cf49 100644
--- a/crates/ra_hir/src/nameres/tests.rs
+++ b/crates/ra_hir/src/nameres/tests.rs
@@ -19,6 +19,19 @@ fn item_map(fixture: &str) -> (Arc<ItemMap>, ModuleId) {
19 (db.item_map(krate.crate_id), module_id) 19 (db.item_map(krate.crate_id), module_id)
20} 20}
21 21
22fn item_map_custom_crate_root(fixture: &str, root: &str) -> (Arc<ItemMap>, ModuleId) {
23 let (mut db, pos) = MockDatabase::with_position(fixture);
24
25 let mut crate_graph = CrateGraph::default();
26 crate_graph.add_crate_root(db.file_id(root));
27 db.set_crate_graph(Arc::new(crate_graph));
28
29 let module = crate::source_binder::module_from_position(&db, dbg!(pos)).unwrap();
30 let krate = module.krate(&db).unwrap();
31 let module_id = module.module_id;
32 (db.item_map(krate.crate_id), module_id)
33}
34
22fn check_module_item_map(map: &ItemMap, module_id: ModuleId, expected: &str) { 35fn check_module_item_map(map: &ItemMap, module_id: ModuleId, expected: &str) {
23 let mut lines = map[module_id] 36 let mut lines = map[module_id]
24 .items 37 .items
@@ -134,6 +147,31 @@ fn re_exports() {
134} 147}
135 148
136#[test] 149#[test]
150fn module_resolution_works_for_non_standard_filenames() {
151 let (item_map, module_id) = item_map_custom_crate_root(
152 "
153 //- /my_library.rs
154 mod foo;
155
156 use self::foo::Bar;
157 <|>
158
159 //- /foo/mod.rs
160 pub struct Bar;
161 ",
162 "/my_library.rs",
163 );
164 check_module_item_map(
165 &item_map,
166 module_id,
167 "
168 Bar: t v
169 foo: t
170 ",
171 );
172}
173
174#[test]
137fn name_res_works_for_broken_modules() { 175fn name_res_works_for_broken_modules() {
138 covers!(name_res_works_for_broken_modules); 176 covers!(name_res_works_for_broken_modules);
139 let (item_map, module_id) = item_map( 177 let (item_map, module_id) = item_map(