diff options
Diffstat (limited to 'crates/ra_hir_def/src/test_db.rs')
-rw-r--r-- | crates/ra_hir_def/src/test_db.rs | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/crates/ra_hir_def/src/test_db.rs b/crates/ra_hir_def/src/test_db.rs new file mode 100644 index 000000000..f91a5b41d --- /dev/null +++ b/crates/ra_hir_def/src/test_db.rs | |||
@@ -0,0 +1,76 @@ | |||
1 | //! Database used for testing `hir_def`. | ||
2 | |||
3 | use std::{ | ||
4 | panic, | ||
5 | sync::{Arc, Mutex}, | ||
6 | }; | ||
7 | |||
8 | use ra_db::{salsa, CrateId, FileId, FileLoader, FileLoaderDelegate}; | ||
9 | use relative_path::RelativePath; | ||
10 | |||
11 | #[salsa::database( | ||
12 | ra_db::SourceDatabaseExtStorage, | ||
13 | ra_db::SourceDatabaseStorage, | ||
14 | hir_expand::db::AstDatabaseStorage, | ||
15 | crate::db::InternDatabaseStorage, | ||
16 | crate::db::DefDatabase2Storage | ||
17 | )] | ||
18 | #[derive(Debug, Default)] | ||
19 | pub struct TestDB { | ||
20 | runtime: salsa::Runtime<TestDB>, | ||
21 | events: Mutex<Option<Vec<salsa::Event<TestDB>>>>, | ||
22 | } | ||
23 | |||
24 | impl salsa::Database for TestDB { | ||
25 | fn salsa_runtime(&self) -> &salsa::Runtime<Self> { | ||
26 | &self.runtime | ||
27 | } | ||
28 | |||
29 | fn salsa_event(&self, event: impl Fn() -> salsa::Event<TestDB>) { | ||
30 | let mut events = self.events.lock().unwrap(); | ||
31 | if let Some(events) = &mut *events { | ||
32 | events.push(event()); | ||
33 | } | ||
34 | } | ||
35 | } | ||
36 | |||
37 | impl panic::RefUnwindSafe for TestDB {} | ||
38 | |||
39 | impl FileLoader for TestDB { | ||
40 | fn file_text(&self, file_id: FileId) -> Arc<String> { | ||
41 | FileLoaderDelegate(self).file_text(file_id) | ||
42 | } | ||
43 | fn resolve_relative_path( | ||
44 | &self, | ||
45 | anchor: FileId, | ||
46 | relative_path: &RelativePath, | ||
47 | ) -> Option<FileId> { | ||
48 | FileLoaderDelegate(self).resolve_relative_path(anchor, relative_path) | ||
49 | } | ||
50 | fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> { | ||
51 | FileLoaderDelegate(self).relevant_crates(file_id) | ||
52 | } | ||
53 | } | ||
54 | |||
55 | impl TestDB { | ||
56 | pub fn log(&self, f: impl FnOnce()) -> Vec<salsa::Event<TestDB>> { | ||
57 | *self.events.lock().unwrap() = Some(Vec::new()); | ||
58 | f(); | ||
59 | self.events.lock().unwrap().take().unwrap() | ||
60 | } | ||
61 | |||
62 | pub fn log_executed(&self, f: impl FnOnce()) -> Vec<String> { | ||
63 | let events = self.log(f); | ||
64 | events | ||
65 | .into_iter() | ||
66 | .filter_map(|e| match e.kind { | ||
67 | // This pretty horrible, but `Debug` is the only way to inspect | ||
68 | // QueryDescriptor at the moment. | ||
69 | salsa::EventKind::WillExecute { database_key } => { | ||
70 | Some(format!("{:?}", database_key)) | ||
71 | } | ||
72 | _ => None, | ||
73 | }) | ||
74 | .collect() | ||
75 | } | ||
76 | } | ||