]> git.lizzy.rs Git - rust.git/commitdiff
Introduce ra_db::fixture fixture module
authorAleksey Kladov <aleksey.kladov@gmail.com>
Sun, 3 Nov 2019 17:53:17 +0000 (20:53 +0300)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Sun, 3 Nov 2019 17:55:20 +0000 (20:55 +0300)
The goal here is to share more testing infrastructure between crates.

Cargo.lock
crates/ra_db/Cargo.toml
crates/ra_db/src/fixture.rs [new file with mode: 0644]
crates/ra_db/src/lib.rs
crates/ra_hir_def/src/lib.rs
crates/ra_hir_def/src/nameres/collector.rs
crates/ra_hir_def/src/test_db.rs [new file with mode: 0644]

index 29f77044e832a3dee2137cb4475cae5cb94b6472..c96e0869cab3c84e96c0c621eaafd31d91bcb58e 100644 (file)
@@ -985,6 +985,7 @@ dependencies = [
  "relative-path 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
  "salsa 0.13.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "test_utils 0.1.0",
 ]
 
 [[package]]
index 3394ae8ce6c33436c1807e82c1295ddfab641d7a..bf1f7920c583d8dfe92f2f400f52fa62f6bed961 100644 (file)
@@ -12,3 +12,4 @@ rustc-hash = "1.0"
 ra_syntax = { path = "../ra_syntax" }
 ra_cfg = { path = "../ra_cfg" }
 ra_prof = { path = "../ra_prof" }
+test_utils = { path = "../test_utils" }
diff --git a/crates/ra_db/src/fixture.rs b/crates/ra_db/src/fixture.rs
new file mode 100644 (file)
index 0000000..469251f
--- /dev/null
@@ -0,0 +1,40 @@
+//! FIXME: write short doc here
+
+use std::sync::Arc;
+
+use ra_cfg::CfgOptions;
+
+use crate::{
+    CrateGraph, Edition, FileId, RelativePathBuf, SourceDatabaseExt, SourceRoot, SourceRootId,
+};
+
+pub const WORKSPACE: SourceRootId = SourceRootId(0);
+
+pub trait WithFixture: Default + SourceDatabaseExt + 'static {
+    fn with_single_file(text: &str) -> (Self, FileId) {
+        let mut db = Self::default();
+        let file_id = with_single_file(&mut db, text);
+        (db, file_id)
+    }
+}
+
+impl<DB: SourceDatabaseExt + Default + 'static> WithFixture for DB {}
+
+fn with_single_file(db: &mut dyn SourceDatabaseExt, text: &str) -> FileId {
+    let file_id = FileId(0);
+    let rel_path: RelativePathBuf = "/main.rs".into();
+
+    let mut source_root = SourceRoot::default();
+    source_root.insert_file(rel_path.clone(), file_id);
+
+    let mut crate_graph = CrateGraph::default();
+    crate_graph.add_crate_root(file_id, Edition::Edition2018, CfgOptions::default());
+
+    db.set_file_text(file_id, Arc::new(text.to_string()));
+    db.set_file_relative_path(file_id, rel_path);
+    db.set_file_source_root(file_id, WORKSPACE);
+    db.set_source_root(WORKSPACE, Arc::new(source_root));
+    db.set_crate_graph(Arc::new(crate_graph));
+
+    file_id
+}
index 0d1ab48438d02d717eef8fe36988d2d52c9e6d50..b6bfd531de934029e4c19f81c8110ad0291735f1 100644 (file)
@@ -1,17 +1,18 @@
 //! ra_db defines basic database traits. The concrete DB is defined by ra_ide_api.
 mod cancellation;
 mod input;
+pub mod fixture;
 
 use std::{panic, sync::Arc};
 
 use ra_prof::profile;
 use ra_syntax::{ast, Parse, SourceFile, TextRange, TextUnit};
-use relative_path::{RelativePath, RelativePathBuf};
 
 pub use crate::{
     cancellation::Canceled,
     input::{CrateGraph, CrateId, Dependency, Edition, FileId, SourceRoot, SourceRootId},
 };
+pub use relative_path::{RelativePath, RelativePathBuf};
 pub use salsa;
 
 pub trait CheckCanceled {
index 42e080a72e94a7dcc6b7b17483eebfb4ab2d4670..02bd808fc2178975362abc1ee2ca5ff0c9c0fd85 100644 (file)
@@ -15,6 +15,9 @@
 pub mod adt;
 pub mod diagnostics;
 
+#[cfg(test)]
+mod test_db;
+
 // FIXME: this should be private
 pub mod nameres;
 
index 8a96d3d31f57a2bf886ab6152cf314fd6f714044..0bc36910c3f0c4ff756b663c1f1ebe462491b0b3 100644 (file)
@@ -739,17 +739,18 @@ fn is_macro_rules(path: &Path) -> bool {
     path.as_ident() == Some(&name::MACRO_RULES)
 }
 
-#[cfg(never)]
+#[cfg(test)]
 mod tests {
-    use ra_db::SourceDatabase;
-
-    use super::*;
-    use crate::{db::DefDatabase, mock::MockDatabase, Crate};
     use ra_arena::Arena;
+    use ra_db::{fixture::WithFixture, SourceDatabase};
     use rustc_hash::FxHashSet;
 
+    use crate::{db::DefDatabase2, test_db::TestDB};
+
+    use super::*;
+
     fn do_collect_defs(
-        db: &impl DefDatabase,
+        db: &impl DefDatabase2,
         def_map: CrateDefMap,
         monitor: MacroStackMonitor,
     ) -> CrateDefMap {
@@ -768,12 +769,11 @@ fn do_collect_defs(
     }
 
     fn do_limited_resolve(code: &str, limit: u32, poison_limit: u32) -> CrateDefMap {
-        let (db, _source_root, _) = MockDatabase::with_single_file(&code);
-        let crate_id = db.crate_graph().iter().next().unwrap();
-        let krate = Crate { crate_id };
+        let (db, _file_id) = TestDB::with_single_file(&code);
+        let krate = db.crate_graph().iter().next().unwrap();
 
         let def_map = {
-            let edition = krate.edition(&db);
+            let edition = db.crate_graph().edition(krate);
             let mut modules: Arena<CrateModuleId, ModuleData> = Arena::default();
             let root = modules.alloc(ModuleData::default());
             CrateDefMap {
diff --git a/crates/ra_hir_def/src/test_db.rs b/crates/ra_hir_def/src/test_db.rs
new file mode 100644 (file)
index 0000000..67714c6
--- /dev/null
@@ -0,0 +1,40 @@
+use std::{panic, sync::Arc};
+
+use ra_db::{salsa, CrateId, FileId, FileLoader, FileLoaderDelegate};
+use relative_path::RelativePath;
+
+#[salsa::database(
+    ra_db::SourceDatabaseExtStorage,
+    ra_db::SourceDatabaseStorage,
+    hir_expand::db::AstDatabaseStorage,
+    crate::db::InternDatabaseStorage,
+    crate::db::DefDatabase2Storage
+)]
+#[derive(Debug, Default)]
+pub struct TestDB {
+    runtime: salsa::Runtime<TestDB>,
+}
+
+impl salsa::Database for TestDB {
+    fn salsa_runtime(&self) -> &salsa::Runtime<Self> {
+        &self.runtime
+    }
+}
+
+impl panic::RefUnwindSafe for TestDB {}
+
+impl FileLoader for TestDB {
+    fn file_text(&self, file_id: FileId) -> Arc<String> {
+        FileLoaderDelegate(self).file_text(file_id)
+    }
+    fn resolve_relative_path(
+        &self,
+        anchor: FileId,
+        relative_path: &RelativePath,
+    ) -> Option<FileId> {
+        FileLoaderDelegate(self).resolve_relative_path(anchor, relative_path)
+    }
+    fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> {
+        FileLoaderDelegate(self).relevant_crates(file_id)
+    }
+}