From 74d827bb8001952f90c85fca3d064fe3096009bd Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 4 Nov 2019 22:21:15 +0300 Subject: Rename MockDatabase -> TestDB Actually working rename is sooo useful! --- crates/ra_hir/src/expr/scope.rs | 6 +- crates/ra_hir/src/lib.rs | 4 +- crates/ra_hir/src/mock.rs | 134 ---------------------------------------- crates/ra_hir/src/test_db.rs | 131 +++++++++++++++++++++++++++++++++++++++ crates/ra_hir/src/ty/tests.rs | 32 +++++----- 5 files changed, 152 insertions(+), 155 deletions(-) delete mode 100644 crates/ra_hir/src/mock.rs create mode 100644 crates/ra_hir/src/test_db.rs (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/expr/scope.rs b/crates/ra_hir/src/expr/scope.rs index f7196b740..c14c2ab66 100644 --- a/crates/ra_hir/src/expr/scope.rs +++ b/crates/ra_hir/src/expr/scope.rs @@ -178,7 +178,7 @@ mod tests { use ra_syntax::{algo::find_node_at_offset, ast, AstNode}; use test_utils::{assert_eq_text, extract_offset}; - use crate::{mock::MockDatabase, source_binder::SourceAnalyzer}; + use crate::{source_binder::SourceAnalyzer, test_db::TestDB}; fn do_check(code: &str, expected: &[&str]) { let (off, code) = extract_offset(code); @@ -191,7 +191,7 @@ mod tests { buf }; - let (db, file_id) = MockDatabase::with_single_file(&code); + let (db, file_id) = TestDB::with_single_file(&code); let file = db.parse(file_id).ok().unwrap(); let marker: ast::PathExpr = find_node_at_offset(file.syntax(), off).unwrap(); let analyzer = SourceAnalyzer::new(&db, file_id, marker.syntax(), None); @@ -288,7 +288,7 @@ mod tests { fn do_check_local_name(code: &str, expected_offset: u32) { let (off, code) = extract_offset(code); - let (db, file_id) = MockDatabase::with_single_file(&code); + let (db, file_id) = TestDB::with_single_file(&code); let file = db.parse(file_id).ok().unwrap(); let expected_name = find_node_at_offset::(file.syntax(), expected_offset.into()) .expect("failed to find a name at the target offset"); diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 3ba99d92d..da6aa2ed8 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -29,8 +29,6 @@ macro_rules! impl_froms { pub mod debug; pub mod db; -#[macro_use] -pub mod mock; pub mod source_binder; mod ids; @@ -51,6 +49,8 @@ mod code_model; pub mod from_source; +#[cfg(test)] +mod test_db; #[cfg(test)] mod marks; diff --git a/crates/ra_hir/src/mock.rs b/crates/ra_hir/src/mock.rs deleted file mode 100644 index ff1f3fa75..000000000 --- a/crates/ra_hir/src/mock.rs +++ /dev/null @@ -1,134 +0,0 @@ -//! FIXME: write short doc here - -use std::{panic, sync::Arc}; - -use hir_def::{db::DefDatabase2, ModuleId}; -use hir_expand::diagnostics::DiagnosticSink; -use parking_lot::Mutex; -use ra_db::{ - salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, RelativePath, SourceDatabase, - SourceRootId, -}; - -use crate::{db, debug::HirDebugHelper}; - -pub const WORKSPACE: SourceRootId = SourceRootId(0); - -#[salsa::database( - ra_db::SourceDatabaseExtStorage, - ra_db::SourceDatabaseStorage, - db::InternDatabaseStorage, - db::AstDatabaseStorage, - db::DefDatabaseStorage, - db::DefDatabase2Storage, - db::HirDatabaseStorage -)] -#[derive(Debug)] -pub struct MockDatabase { - events: Mutex>>>, - runtime: salsa::Runtime, -} - -impl panic::RefUnwindSafe for MockDatabase {} - -impl FileLoader for MockDatabase { - fn file_text(&self, file_id: FileId) -> Arc { - FileLoaderDelegate(self).file_text(file_id) - } - fn resolve_relative_path( - &self, - anchor: FileId, - relative_path: &RelativePath, - ) -> Option { - FileLoaderDelegate(self).resolve_relative_path(anchor, relative_path) - } - fn relevant_crates(&self, file_id: FileId) -> Arc> { - FileLoaderDelegate(self).relevant_crates(file_id) - } -} - -// FIXME: improve `WithFixture` to bring useful hir debugging back -impl HirDebugHelper for MockDatabase { - fn crate_name(&self, _krate: CrateId) -> Option { - None - } - - fn file_path(&self, _file_id: FileId) -> Option { - None - } -} - -impl MockDatabase { - pub fn diagnostics(&self) -> String { - let mut buf = String::new(); - let crate_graph = self.crate_graph(); - for krate in crate_graph.iter().next() { - let crate_def_map = self.crate_def_map(krate); - for (module_id, _) in crate_def_map.modules.iter() { - let module_id = ModuleId { krate, module_id }; - let module = crate::Module::from(module_id); - module.diagnostics( - self, - &mut DiagnosticSink::new(|d| { - buf += &format!("{:?}: {}\n", d.syntax_node(self).text(), d.message()); - }), - ) - } - } - buf - } -} - -impl salsa::Database for MockDatabase { - fn salsa_runtime(&self) -> &salsa::Runtime { - &self.runtime - } - - fn salsa_event(&self, event: impl Fn() -> salsa::Event) { - let mut events = self.events.lock(); - if let Some(events) = &mut *events { - events.push(event()); - } - } -} - -impl Default for MockDatabase { - fn default() -> MockDatabase { - let mut db = - MockDatabase { events: Default::default(), runtime: salsa::Runtime::default() }; - db.set_crate_graph(Default::default()); - db - } -} - -impl salsa::ParallelDatabase for MockDatabase { - fn snapshot(&self) -> salsa::Snapshot { - salsa::Snapshot::new(MockDatabase { - events: Default::default(), - runtime: self.runtime.snapshot(self), - }) - } -} - -impl MockDatabase { - pub fn log(&self, f: impl FnOnce()) -> Vec> { - *self.events.lock() = Some(Vec::new()); - f(); - self.events.lock().take().unwrap() - } - - pub fn log_executed(&self, f: impl FnOnce()) -> Vec { - let events = self.log(f); - events - .into_iter() - .filter_map(|e| match e.kind { - // This pretty horrible, but `Debug` is the only way to inspect - // QueryDescriptor at the moment. - salsa::EventKind::WillExecute { database_key } => { - Some(format!("{:?}", database_key)) - } - _ => None, - }) - .collect() - } -} diff --git a/crates/ra_hir/src/test_db.rs b/crates/ra_hir/src/test_db.rs new file mode 100644 index 000000000..047a27aaf --- /dev/null +++ b/crates/ra_hir/src/test_db.rs @@ -0,0 +1,131 @@ +//! FIXME: write short doc here + +use std::{panic, sync::Arc}; + +use hir_def::{db::DefDatabase2, ModuleId}; +use hir_expand::diagnostics::DiagnosticSink; +use parking_lot::Mutex; +use ra_db::{ + salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, RelativePath, SourceDatabase, + SourceRootId, +}; + +use crate::{db, debug::HirDebugHelper}; + +#[salsa::database( + ra_db::SourceDatabaseExtStorage, + ra_db::SourceDatabaseStorage, + db::InternDatabaseStorage, + db::AstDatabaseStorage, + db::DefDatabaseStorage, + db::DefDatabase2Storage, + db::HirDatabaseStorage +)] +#[derive(Debug)] +pub struct TestDB { + events: Mutex>>>, + runtime: salsa::Runtime, +} + +impl panic::RefUnwindSafe for TestDB {} + +impl FileLoader for TestDB { + fn file_text(&self, file_id: FileId) -> Arc { + FileLoaderDelegate(self).file_text(file_id) + } + fn resolve_relative_path( + &self, + anchor: FileId, + relative_path: &RelativePath, + ) -> Option { + FileLoaderDelegate(self).resolve_relative_path(anchor, relative_path) + } + fn relevant_crates(&self, file_id: FileId) -> Arc> { + FileLoaderDelegate(self).relevant_crates(file_id) + } +} + +// FIXME: improve `WithFixture` to bring useful hir debugging back +impl HirDebugHelper for TestDB { + fn crate_name(&self, _krate: CrateId) -> Option { + None + } + + fn file_path(&self, _file_id: FileId) -> Option { + None + } +} + +impl TestDB { + pub fn diagnostics(&self) -> String { + let mut buf = String::new(); + let crate_graph = self.crate_graph(); + for krate in crate_graph.iter().next() { + let crate_def_map = self.crate_def_map(krate); + for (module_id, _) in crate_def_map.modules.iter() { + let module_id = ModuleId { krate, module_id }; + let module = crate::Module::from(module_id); + module.diagnostics( + self, + &mut DiagnosticSink::new(|d| { + buf += &format!("{:?}: {}\n", d.syntax_node(self).text(), d.message()); + }), + ) + } + } + buf + } +} + +impl salsa::Database for TestDB { + fn salsa_runtime(&self) -> &salsa::Runtime { + &self.runtime + } + + fn salsa_event(&self, event: impl Fn() -> salsa::Event) { + let mut events = self.events.lock(); + if let Some(events) = &mut *events { + events.push(event()); + } + } +} + +impl Default for TestDB { + fn default() -> TestDB { + let mut db = TestDB { events: Default::default(), runtime: salsa::Runtime::default() }; + db.set_crate_graph(Default::default()); + db + } +} + +impl salsa::ParallelDatabase for TestDB { + fn snapshot(&self) -> salsa::Snapshot { + salsa::Snapshot::new(TestDB { + events: Default::default(), + runtime: self.runtime.snapshot(self), + }) + } +} + +impl TestDB { + pub fn log(&self, f: impl FnOnce()) -> Vec> { + *self.events.lock() = Some(Vec::new()); + f(); + self.events.lock().take().unwrap() + } + + pub fn log_executed(&self, f: impl FnOnce()) -> Vec { + let events = self.log(f); + events + .into_iter() + .filter_map(|e| match e.kind { + // This pretty horrible, but `Debug` is the only way to inspect + // QueryDescriptor at the moment. + salsa::EventKind::WillExecute { database_key } => { + Some(format!("{:?}", database_key)) + } + _ => None, + }) + .collect() + } +} diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index 4af2fe0b4..e56b9356e 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs @@ -11,7 +11,7 @@ use ra_syntax::{ use test_utils::covers; use crate::{ - expr::BodySourceMap, mock::MockDatabase, ty::display::HirDisplay, ty::InferenceResult, + expr::BodySourceMap, test_db::TestDB, ty::display::HirDisplay, ty::InferenceResult, SourceAnalyzer, }; @@ -24,7 +24,7 @@ mod coercion; #[test] fn cfg_impl_block() { - let (db, pos) = MockDatabase::with_position( + let (db, pos) = TestDB::with_position( r#" //- /main.rs crate:main deps:foo cfg:test use foo::S as T; @@ -64,7 +64,7 @@ impl S { #[test] fn infer_await() { - let (db, pos) = MockDatabase::with_position( + let (db, pos) = TestDB::with_position( r#" //- /main.rs crate:main deps:std @@ -95,7 +95,7 @@ mod future { #[test] fn infer_box() { - let (db, pos) = MockDatabase::with_position( + let (db, pos) = TestDB::with_position( r#" //- /main.rs crate:main deps:std @@ -122,7 +122,7 @@ mod boxed { #[test] fn infer_adt_self() { - let (db, pos) = MockDatabase::with_position( + let (db, pos) = TestDB::with_position( r#" //- /main.rs enum Nat { Succ(Self), Demo(Nat), Zero } @@ -141,7 +141,7 @@ fn test() { #[test] fn infer_try() { - let (db, pos) = MockDatabase::with_position( + let (db, pos) = TestDB::with_position( r#" //- /main.rs crate:main deps:std @@ -181,7 +181,7 @@ mod result { #[test] fn infer_for_loop() { - let (db, pos) = MockDatabase::with_position( + let (db, pos) = TestDB::with_position( r#" //- /main.rs crate:main deps:std @@ -223,7 +223,7 @@ mod collections { #[test] fn infer_while_let() { covers!(infer_while_let); - let (db, pos) = MockDatabase::with_position( + let (db, pos) = TestDB::with_position( r#" //- /main.rs enum Option { Some(T), None } @@ -2484,7 +2484,7 @@ pub fn main_loop() { #[test] fn cross_crate_associated_method_call() { - let (db, pos) = MockDatabase::with_position( + let (db, pos) = TestDB::with_position( r#" //- /main.rs crate:main deps:other_crate fn test() { @@ -3378,7 +3378,7 @@ fn test() { S.foo()<|>; } #[test] fn infer_macro_with_dollar_crate_is_correct_in_expr() { - let (db, pos) = MockDatabase::with_position( + let (db, pos) = TestDB::with_position( r#" //- /main.rs crate:main deps:foo fn test() { @@ -3482,7 +3482,7 @@ fn test() { (&S).foo()<|>; } #[test] fn method_resolution_trait_from_prelude() { - let (db, pos) = MockDatabase::with_position( + let (db, pos) = TestDB::with_position( r#" //- /main.rs crate:main deps:other_crate struct S; @@ -4651,7 +4651,7 @@ fn test() where T: Trait, U: Trait { assert_eq!(t, "{unknown}"); } -fn type_at_pos(db: &MockDatabase, pos: FilePosition) -> String { +fn type_at_pos(db: &TestDB, pos: FilePosition) -> String { let file = db.parse(pos.file_id).ok().unwrap(); let expr = algo::find_node_at_offset::(file.syntax(), pos.offset).unwrap(); let analyzer = SourceAnalyzer::new(db, pos.file_id, expr.syntax(), Some(pos.offset)); @@ -4660,12 +4660,12 @@ fn type_at_pos(db: &MockDatabase, pos: FilePosition) -> String { } fn type_at(content: &str) -> String { - let (db, file_pos) = MockDatabase::with_position(content); + let (db, file_pos) = TestDB::with_position(content); type_at_pos(&db, file_pos) } fn infer(content: &str) -> String { - let (db, file_id) = MockDatabase::with_single_file(content); + let (db, file_id) = TestDB::with_single_file(content); let source_file = db.parse(file_id).ok().unwrap(); let mut acc = String::new(); @@ -4748,7 +4748,7 @@ fn ellipsize(mut text: String, max_len: usize) -> String { #[test] fn typing_whitespace_inside_a_function_should_not_invalidate_types() { - let (mut db, pos) = MockDatabase::with_position( + let (mut db, pos) = TestDB::with_position( " //- /lib.rs fn foo() -> i32 { @@ -4788,7 +4788,7 @@ fn typing_whitespace_inside_a_function_should_not_invalidate_types() { #[test] fn no_such_field_diagnostics() { - let diagnostics = MockDatabase::with_files( + let diagnostics = TestDB::with_files( r" //- /lib.rs struct S { foo: i32, bar: () } -- cgit v1.2.3