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