aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def/src/test_db.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_def/src/test_db.rs')
-rw-r--r--crates/ra_hir_def/src/test_db.rs36
1 files changed, 35 insertions, 1 deletions
diff --git a/crates/ra_hir_def/src/test_db.rs b/crates/ra_hir_def/src/test_db.rs
index 67714c68e..05018f8e4 100644
--- a/crates/ra_hir_def/src/test_db.rs
+++ b/crates/ra_hir_def/src/test_db.rs
@@ -1,4 +1,7 @@
1use std::{panic, sync::Arc}; 1use std::{
2 panic,
3 sync::{Arc, Mutex},
4};
2 5
3use ra_db::{salsa, CrateId, FileId, FileLoader, FileLoaderDelegate}; 6use ra_db::{salsa, CrateId, FileId, FileLoader, FileLoaderDelegate};
4use relative_path::RelativePath; 7use relative_path::RelativePath;
@@ -13,12 +16,20 @@ use relative_path::RelativePath;
13#[derive(Debug, Default)] 16#[derive(Debug, Default)]
14pub struct TestDB { 17pub struct TestDB {
15 runtime: salsa::Runtime<TestDB>, 18 runtime: salsa::Runtime<TestDB>,
19 events: Mutex<Option<Vec<salsa::Event<TestDB>>>>,
16} 20}
17 21
18impl salsa::Database for TestDB { 22impl salsa::Database for TestDB {
19 fn salsa_runtime(&self) -> &salsa::Runtime<Self> { 23 fn salsa_runtime(&self) -> &salsa::Runtime<Self> {
20 &self.runtime 24 &self.runtime
21 } 25 }
26
27 fn salsa_event(&self, event: impl Fn() -> salsa::Event<TestDB>) {
28 let mut events = self.events.lock().unwrap();
29 if let Some(events) = &mut *events {
30 events.push(event());
31 }
32 }
22} 33}
23 34
24impl panic::RefUnwindSafe for TestDB {} 35impl panic::RefUnwindSafe for TestDB {}
@@ -38,3 +49,26 @@ impl FileLoader for TestDB {
38 FileLoaderDelegate(self).relevant_crates(file_id) 49 FileLoaderDelegate(self).relevant_crates(file_id)
39 } 50 }
40} 51}
52
53impl TestDB {
54 pub fn log(&self, f: impl FnOnce()) -> Vec<salsa::Event<TestDB>> {
55 *self.events.lock().unwrap() = Some(Vec::new());
56 f();
57 self.events.lock().unwrap().take().unwrap()
58 }
59
60 pub fn log_executed(&self, f: impl FnOnce()) -> Vec<String> {
61 let events = self.log(f);
62 events
63 .into_iter()
64 .filter_map(|e| match e.kind {
65 // This pretty horrible, but `Debug` is the only way to inspect
66 // QueryDescriptor at the moment.
67 salsa::EventKind::WillExecute { database_key } => {
68 Some(format!("{:?}", database_key))
69 }
70 _ => None,
71 })
72 .collect()
73 }
74}