From 683e5e64f42b6c74596015c5d19e2eab186fe68d Mon Sep 17 00:00:00 2001 From: Erlend Tobiassen Date: Sun, 27 Jan 2019 14:51:26 +0100 Subject: Test non standard crate root --- crates/ra_hir/src/mock.rs | 16 ++++++++++------ crates/ra_hir/src/nameres/tests.rs | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 6 deletions(-) (limited to 'crates/ra_hir/src') 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}; use parking_lot::Mutex; use ra_db::{ - CheckCanceled, FilePosition, FileId, CrateGraph, SourceRoot, SourceRootId, SourceDatabase, salsa, + mock::FileMap, CheckCanceled, CrateGraph, FileId, FilePosition, SourceDatabase, + SourceRoot, SourceRootId, salsa }; use relative_path::RelativePathBuf; use test_utils::{parse_fixture, CURSOR_MARKER, extract_offset}; @@ -17,7 +18,7 @@ pub(crate) struct MockDatabase { events: Mutex>>>, runtime: salsa::Runtime, interner: Arc, - file_counter: u32, + file_map: Arc, } impl panic::RefUnwindSafe for MockDatabase {} @@ -43,6 +44,10 @@ impl MockDatabase { (db, position) } + pub(crate) fn file_id(&self, file: &str) -> FileId { + self.file_map.file_id(file) + } + fn from_fixture(fixture: &str) -> (MockDatabase, SourceRoot, Option) { let mut db = MockDatabase::default(); @@ -89,8 +94,7 @@ impl MockDatabase { let is_crate_root = path == "/lib.rs" || path == "/main.rs"; let path = RelativePathBuf::from_path(&path[1..]).unwrap(); - let file_id = FileId(self.file_counter); - self.file_counter += 1; + let file_id = Arc::make_mut(&mut self.file_map).add(path.clone()); let text = Arc::new(text.to_string()); self.set_file_text(file_id, text); self.set_file_relative_path(file_id, path.clone()); @@ -137,7 +141,7 @@ impl Default for MockDatabase { events: Default::default(), runtime: salsa::Runtime::default(), interner: Default::default(), - file_counter: 0, + file_map: Default::default(), }; db.set_crate_graph(Default::default()); db @@ -150,7 +154,7 @@ impl salsa::ParallelDatabase for MockDatabase { events: Default::default(), runtime: self.runtime.snapshot(self), interner: Arc::clone(&self.interner), - file_counter: self.file_counter, + file_map: Arc::clone(&self.file_map), }) } } 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, ModuleId) { (db.item_map(krate.crate_id), module_id) } +fn item_map_custom_crate_root(fixture: &str, root: &str) -> (Arc, ModuleId) { + let (mut db, pos) = MockDatabase::with_position(fixture); + + let mut crate_graph = CrateGraph::default(); + crate_graph.add_crate_root(db.file_id(root)); + db.set_crate_graph(Arc::new(crate_graph)); + + let module = crate::source_binder::module_from_position(&db, dbg!(pos)).unwrap(); + let krate = module.krate(&db).unwrap(); + let module_id = module.module_id; + (db.item_map(krate.crate_id), module_id) +} + fn check_module_item_map(map: &ItemMap, module_id: ModuleId, expected: &str) { let mut lines = map[module_id] .items @@ -133,6 +146,31 @@ fn re_exports() { ); } +#[test] +fn module_resolution_works_for_non_standard_filenames() { + let (item_map, module_id) = item_map_custom_crate_root( + " + //- /my_library.rs + mod foo; + + use self::foo::Bar; + <|> + + //- /foo/mod.rs + pub struct Bar; + ", + "/my_library.rs", + ); + check_module_item_map( + &item_map, + module_id, + " + Bar: t v + foo: t + ", + ); +} + #[test] fn name_res_works_for_broken_modules() { covers!(name_res_works_for_broken_modules); -- cgit v1.2.3