From e89700f9678d3797c09f0a397b7b67fe9c6f5e9f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 28 Nov 2018 16:19:01 +0300 Subject: Move hir tests to hit --- crates/ra_hir/src/module/nameres.rs | 97 +++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) (limited to 'crates/ra_hir/src/module') diff --git a/crates/ra_hir/src/module/nameres.rs b/crates/ra_hir/src/module/nameres.rs index 837a8d5ae..de13835b2 100644 --- a/crates/ra_hir/src/module/nameres.rs +++ b/crates/ra_hir/src/module/nameres.rs @@ -336,3 +336,100 @@ where f(module_items) } } + +//TODO: move to hir +#[cfg(test)] +mod tests { + use std::sync::Arc; + + use salsa::Database; + use ra_db::FilesDatabase; + use ra_syntax::SmolStr; + + use crate::{ + self as hir, + db::HirDatabase, + mock::MockDatabase, +}; + + fn item_map(fixture: &str) -> (Arc, hir::ModuleId) { + let (db, pos) = MockDatabase::with_position(fixture); + let source_root = db.file_source_root(pos.file_id); + let module = hir::Module::guess_from_position(&db, pos).unwrap().unwrap(); + let module_id = module.module_id; + (db.item_map(source_root).unwrap(), module_id) + } + + #[test] + fn test_item_map() { + let (item_map, module_id) = item_map( + " + //- /lib.rs + mod foo; + + use crate::foo::bar::Baz; + <|> + + //- /foo/mod.rs + pub mod bar; + + //- /foo/bar.rs + pub struct Baz; + ", + ); + let name = SmolStr::from("Baz"); + let resolution = &item_map.per_module[&module_id].items[&name]; + assert!(resolution.def_id.is_some()); + } + + #[test] + fn typing_inside_a_function_should_not_invalidate_item_map() { + let (mut db, pos) = MockDatabase::with_position( + " + //- /lib.rs + mod foo;<|> + + use crate::foo::bar::Baz; + + fn foo() -> i32 { + 1 + 1 + } + //- /foo/mod.rs + pub mod bar; + + //- /foo/bar.rs + pub struct Baz; + ", + ); + let source_root = db.file_source_root(pos.file_id); + { + let events = db.log_executed(|| { + db.item_map(source_root).unwrap(); + }); + assert!(format!("{:?}", events).contains("item_map")) + } + + let new_text = " + mod foo; + + use crate::foo::bar::Baz; + + fn foo() -> i32 { 92 } + " + .to_string(); + + db.query_mut(ra_db::FileTextQuery) + .set(pos.file_id, Arc::new(new_text)); + + { + let events = db.log_executed(|| { + db.item_map(source_root).unwrap(); + }); + assert!( + !format!("{:?}", events).contains("_item_map"), + "{:#?}", + events + ) + } + } +} -- cgit v1.2.3 From d30bbfda74346c5007174d8bda93e9583c166a4c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 28 Nov 2018 16:24:06 +0300 Subject: make a bunch of stuff private --- crates/ra_hir/src/module/mod.rs | 8 +++----- crates/ra_hir/src/module/nameres.rs | 12 ++++++------ 2 files changed, 9 insertions(+), 11 deletions(-) (limited to 'crates/ra_hir/src/module') diff --git a/crates/ra_hir/src/module/mod.rs b/crates/ra_hir/src/module/mod.rs index a011fd53e..3ae83d8cb 100644 --- a/crates/ra_hir/src/module/mod.rs +++ b/crates/ra_hir/src/module/mod.rs @@ -26,8 +26,7 @@ pub use self::nameres::ModuleScope; pub struct Module { tree: Arc, source_root_id: SourceRootId, - //TODO: make private - pub module_id: ModuleId, + module_id: ModuleId, } impl Module { @@ -122,7 +121,6 @@ impl Module { } /// `name` is `None` for the crate's root module - #[allow(unused)] pub fn name(&self) -> Option { let link = self.module_id.parent_link(&self.tree)?; Some(link.name(&self.tree)) @@ -218,7 +216,7 @@ pub enum ModuleSource { /// An owned syntax node for a module. Unlike `ModuleSource`, /// this holds onto the AST for the whole file. -pub enum ModuleSourceNode { +pub(crate) enum ModuleSourceNode { SourceFile(ast::SourceFileNode), Module(ast::ModuleNode), } @@ -338,7 +336,7 @@ impl ModuleSource { } } - pub fn resolve(self, db: &impl HirDatabase) -> ModuleSourceNode { + pub(crate) fn resolve(self, db: &impl HirDatabase) -> ModuleSourceNode { match self { ModuleSource::SourceFile(file_id) => { let syntax = db.source_file(file_id); diff --git a/crates/ra_hir/src/module/nameres.rs b/crates/ra_hir/src/module/nameres.rs index de13835b2..6445d5349 100644 --- a/crates/ra_hir/src/module/nameres.rs +++ b/crates/ra_hir/src/module/nameres.rs @@ -44,7 +44,7 @@ pub struct ItemMap { #[derive(Debug, Default, PartialEq, Eq, Clone)] pub struct ModuleScope { - pub items: FxHashMap, + items: FxHashMap, } impl ModuleScope { @@ -200,11 +200,11 @@ impl ModuleItem { } pub(crate) struct Resolver<'a, DB> { - pub db: &'a DB, - pub input: &'a FxHashMap>, - pub source_root: SourceRootId, - pub module_tree: Arc, - pub result: ItemMap, + pub(crate) db: &'a DB, + pub(crate) input: &'a FxHashMap>, + pub(crate) source_root: SourceRootId, + pub(crate) module_tree: Arc, + pub(crate) result: ItemMap, } impl<'a, DB> Resolver<'a, DB> -- cgit v1.2.3 From 555483d397db530d1d102d7828c7847a948baf6b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 28 Nov 2018 16:27:43 +0300 Subject: drop comment --- crates/ra_hir/src/module/nameres.rs | 1 - 1 file changed, 1 deletion(-) (limited to 'crates/ra_hir/src/module') diff --git a/crates/ra_hir/src/module/nameres.rs b/crates/ra_hir/src/module/nameres.rs index 6445d5349..8529e16b3 100644 --- a/crates/ra_hir/src/module/nameres.rs +++ b/crates/ra_hir/src/module/nameres.rs @@ -337,7 +337,6 @@ where } } -//TODO: move to hir #[cfg(test)] mod tests { use std::sync::Arc; -- cgit v1.2.3