diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-11-03 21:34:50 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2019-11-03 21:34:50 +0000 |
commit | 4fd742d6bc23e2f61d0980e15234f09d7e715b76 (patch) | |
tree | c37d38f9313f54ea71580f6bd005f554388a57ab /crates/ra_hir_def/src/test_db.rs | |
parent | f9f1effd011b906903891c09f1cb6b2a42f73e95 (diff) | |
parent | 8922a44395482896fec0c0a47a7fac4612112d45 (diff) |
Merge #2163
2163: Move CrateDefMap to hir_def r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
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 | } | ||