aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/mock.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-11-04 19:21:15 +0000
committerAleksey Kladov <[email protected]>2019-11-04 19:21:15 +0000
commit74d827bb8001952f90c85fca3d064fe3096009bd (patch)
tree672dfa705dbdd06a167a645b5c5ebea9afe3a57e /crates/ra_hir/src/mock.rs
parent7649a8ebbe900f43cc9b8789c2942cfbadab8f1c (diff)
Rename MockDatabase -> TestDB
Actually working rename is sooo useful!
Diffstat (limited to 'crates/ra_hir/src/mock.rs')
-rw-r--r--crates/ra_hir/src/mock.rs134
1 files changed, 0 insertions, 134 deletions
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 @@
1//! FIXME: write short doc here
2
3use std::{panic, sync::Arc};
4
5use hir_def::{db::DefDatabase2, ModuleId};
6use hir_expand::diagnostics::DiagnosticSink;
7use parking_lot::Mutex;
8use ra_db::{
9 salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, RelativePath, SourceDatabase,
10 SourceRootId,
11};
12
13use crate::{db, debug::HirDebugHelper};
14
15pub const WORKSPACE: SourceRootId = SourceRootId(0);
16
17#[salsa::database(
18 ra_db::SourceDatabaseExtStorage,
19 ra_db::SourceDatabaseStorage,
20 db::InternDatabaseStorage,
21 db::AstDatabaseStorage,
22 db::DefDatabaseStorage,
23 db::DefDatabase2Storage,
24 db::HirDatabaseStorage
25)]
26#[derive(Debug)]
27pub struct MockDatabase {
28 events: Mutex<Option<Vec<salsa::Event<MockDatabase>>>>,
29 runtime: salsa::Runtime<MockDatabase>,
30}
31
32impl panic::RefUnwindSafe for MockDatabase {}
33
34impl FileLoader for MockDatabase {
35 fn file_text(&self, file_id: FileId) -> Arc<String> {
36 FileLoaderDelegate(self).file_text(file_id)
37 }
38 fn resolve_relative_path(
39 &self,
40 anchor: FileId,
41 relative_path: &RelativePath,
42 ) -> Option<FileId> {
43 FileLoaderDelegate(self).resolve_relative_path(anchor, relative_path)
44 }
45 fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> {
46 FileLoaderDelegate(self).relevant_crates(file_id)
47 }
48}
49
50// FIXME: improve `WithFixture` to bring useful hir debugging back
51impl HirDebugHelper for MockDatabase {
52 fn crate_name(&self, _krate: CrateId) -> Option<String> {
53 None
54 }
55
56 fn file_path(&self, _file_id: FileId) -> Option<String> {
57 None
58 }
59}
60
61impl MockDatabase {
62 pub fn diagnostics(&self) -> String {
63 let mut buf = String::new();
64 let crate_graph = self.crate_graph();
65 for krate in crate_graph.iter().next() {
66 let crate_def_map = self.crate_def_map(krate);
67 for (module_id, _) in crate_def_map.modules.iter() {
68 let module_id = ModuleId { krate, module_id };
69 let module = crate::Module::from(module_id);
70 module.diagnostics(
71 self,
72 &mut DiagnosticSink::new(|d| {
73 buf += &format!("{:?}: {}\n", d.syntax_node(self).text(), d.message());
74 }),
75 )
76 }
77 }
78 buf
79 }
80}
81
82impl salsa::Database for MockDatabase {
83 fn salsa_runtime(&self) -> &salsa::Runtime<MockDatabase> {
84 &self.runtime
85 }
86
87 fn salsa_event(&self, event: impl Fn() -> salsa::Event<MockDatabase>) {
88 let mut events = self.events.lock();
89 if let Some(events) = &mut *events {
90 events.push(event());
91 }
92 }
93}
94
95impl Default for MockDatabase {
96 fn default() -> MockDatabase {
97 let mut db =
98 MockDatabase { events: Default::default(), runtime: salsa::Runtime::default() };
99 db.set_crate_graph(Default::default());
100 db
101 }
102}
103
104impl salsa::ParallelDatabase for MockDatabase {
105 fn snapshot(&self) -> salsa::Snapshot<MockDatabase> {
106 salsa::Snapshot::new(MockDatabase {
107 events: Default::default(),
108 runtime: self.runtime.snapshot(self),
109 })
110 }
111}
112
113impl MockDatabase {
114 pub fn log(&self, f: impl FnOnce()) -> Vec<salsa::Event<MockDatabase>> {
115 *self.events.lock() = Some(Vec::new());
116 f();
117 self.events.lock().take().unwrap()
118 }
119
120 pub fn log_executed(&self, f: impl FnOnce()) -> Vec<String> {
121 let events = self.log(f);
122 events
123 .into_iter()
124 .filter_map(|e| match e.kind {
125 // This pretty horrible, but `Debug` is the only way to inspect
126 // QueryDescriptor at the moment.
127 salsa::EventKind::WillExecute { database_key } => {
128 Some(format!("{:?}", database_key))
129 }
130 _ => None,
131 })
132 .collect()
133 }
134}