aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-11-03 17:53:17 +0000
committerAleksey Kladov <[email protected]>2019-11-03 17:55:20 +0000
commit0933d914a37c4ab57fda6fe95464d194dab6f80c (patch)
tree50a9dcc872bc9846006d5ea14821c8d18ce6a19d
parentba2efca2bbe5f4434f9a2522b2b94df873f3563b (diff)
Introduce ra_db::fixture fixture module
The goal here is to share more testing infrastructure between crates.
-rw-r--r--Cargo.lock1
-rw-r--r--crates/ra_db/Cargo.toml1
-rw-r--r--crates/ra_db/src/fixture.rs40
-rw-r--r--crates/ra_db/src/lib.rs3
-rw-r--r--crates/ra_hir_def/src/lib.rs3
-rw-r--r--crates/ra_hir_def/src/nameres/collector.rs20
-rw-r--r--crates/ra_hir_def/src/test_db.rs40
7 files changed, 97 insertions, 11 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 29f77044e..c96e0869c 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -985,6 +985,7 @@ dependencies = [
985 "relative-path 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", 985 "relative-path 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
986 "rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", 986 "rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
987 "salsa 0.13.2 (registry+https://github.com/rust-lang/crates.io-index)", 987 "salsa 0.13.2 (registry+https://github.com/rust-lang/crates.io-index)",
988 "test_utils 0.1.0",
988] 989]
989 990
990[[package]] 991[[package]]
diff --git a/crates/ra_db/Cargo.toml b/crates/ra_db/Cargo.toml
index 3394ae8ce..bf1f7920c 100644
--- a/crates/ra_db/Cargo.toml
+++ b/crates/ra_db/Cargo.toml
@@ -12,3 +12,4 @@ rustc-hash = "1.0"
12ra_syntax = { path = "../ra_syntax" } 12ra_syntax = { path = "../ra_syntax" }
13ra_cfg = { path = "../ra_cfg" } 13ra_cfg = { path = "../ra_cfg" }
14ra_prof = { path = "../ra_prof" } 14ra_prof = { path = "../ra_prof" }
15test_utils = { path = "../test_utils" }
diff --git a/crates/ra_db/src/fixture.rs b/crates/ra_db/src/fixture.rs
new file mode 100644
index 000000000..469251fe9
--- /dev/null
+++ b/crates/ra_db/src/fixture.rs
@@ -0,0 +1,40 @@
1//! FIXME: write short doc here
2
3use std::sync::Arc;
4
5use ra_cfg::CfgOptions;
6
7use crate::{
8 CrateGraph, Edition, FileId, RelativePathBuf, SourceDatabaseExt, SourceRoot, SourceRootId,
9};
10
11pub const WORKSPACE: SourceRootId = SourceRootId(0);
12
13pub trait WithFixture: Default + SourceDatabaseExt + 'static {
14 fn with_single_file(text: &str) -> (Self, FileId) {
15 let mut db = Self::default();
16 let file_id = with_single_file(&mut db, text);
17 (db, file_id)
18 }
19}
20
21impl<DB: SourceDatabaseExt + Default + 'static> WithFixture for DB {}
22
23fn with_single_file(db: &mut dyn SourceDatabaseExt, text: &str) -> FileId {
24 let file_id = FileId(0);
25 let rel_path: RelativePathBuf = "/main.rs".into();
26
27 let mut source_root = SourceRoot::default();
28 source_root.insert_file(rel_path.clone(), file_id);
29
30 let mut crate_graph = CrateGraph::default();
31 crate_graph.add_crate_root(file_id, Edition::Edition2018, CfgOptions::default());
32
33 db.set_file_text(file_id, Arc::new(text.to_string()));
34 db.set_file_relative_path(file_id, rel_path);
35 db.set_file_source_root(file_id, WORKSPACE);
36 db.set_source_root(WORKSPACE, Arc::new(source_root));
37 db.set_crate_graph(Arc::new(crate_graph));
38
39 file_id
40}
diff --git a/crates/ra_db/src/lib.rs b/crates/ra_db/src/lib.rs
index 0d1ab4843..b6bfd531d 100644
--- a/crates/ra_db/src/lib.rs
+++ b/crates/ra_db/src/lib.rs
@@ -1,17 +1,18 @@
1//! ra_db defines basic database traits. The concrete DB is defined by ra_ide_api. 1//! ra_db defines basic database traits. The concrete DB is defined by ra_ide_api.
2mod cancellation; 2mod cancellation;
3mod input; 3mod input;
4pub mod fixture;
4 5
5use std::{panic, sync::Arc}; 6use std::{panic, sync::Arc};
6 7
7use ra_prof::profile; 8use ra_prof::profile;
8use ra_syntax::{ast, Parse, SourceFile, TextRange, TextUnit}; 9use ra_syntax::{ast, Parse, SourceFile, TextRange, TextUnit};
9use relative_path::{RelativePath, RelativePathBuf};
10 10
11pub use crate::{ 11pub use crate::{
12 cancellation::Canceled, 12 cancellation::Canceled,
13 input::{CrateGraph, CrateId, Dependency, Edition, FileId, SourceRoot, SourceRootId}, 13 input::{CrateGraph, CrateId, Dependency, Edition, FileId, SourceRoot, SourceRootId},
14}; 14};
15pub use relative_path::{RelativePath, RelativePathBuf};
15pub use salsa; 16pub use salsa;
16 17
17pub trait CheckCanceled { 18pub trait CheckCanceled {
diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs
index 42e080a72..02bd808fc 100644
--- a/crates/ra_hir_def/src/lib.rs
+++ b/crates/ra_hir_def/src/lib.rs
@@ -15,6 +15,9 @@ pub mod builtin_type;
15pub mod adt; 15pub mod adt;
16pub mod diagnostics; 16pub mod diagnostics;
17 17
18#[cfg(test)]
19mod test_db;
20
18// FIXME: this should be private 21// FIXME: this should be private
19pub mod nameres; 22pub mod nameres;
20 23
diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs
index 8a96d3d31..0bc36910c 100644
--- a/crates/ra_hir_def/src/nameres/collector.rs
+++ b/crates/ra_hir_def/src/nameres/collector.rs
@@ -739,17 +739,18 @@ fn is_macro_rules(path: &Path) -> bool {
739 path.as_ident() == Some(&name::MACRO_RULES) 739 path.as_ident() == Some(&name::MACRO_RULES)
740} 740}
741 741
742#[cfg(never)] 742#[cfg(test)]
743mod tests { 743mod tests {
744 use ra_db::SourceDatabase;
745
746 use super::*;
747 use crate::{db::DefDatabase, mock::MockDatabase, Crate};
748 use ra_arena::Arena; 744 use ra_arena::Arena;
745 use ra_db::{fixture::WithFixture, SourceDatabase};
749 use rustc_hash::FxHashSet; 746 use rustc_hash::FxHashSet;
750 747
748 use crate::{db::DefDatabase2, test_db::TestDB};
749
750 use super::*;
751
751 fn do_collect_defs( 752 fn do_collect_defs(
752 db: &impl DefDatabase, 753 db: &impl DefDatabase2,
753 def_map: CrateDefMap, 754 def_map: CrateDefMap,
754 monitor: MacroStackMonitor, 755 monitor: MacroStackMonitor,
755 ) -> CrateDefMap { 756 ) -> CrateDefMap {
@@ -768,12 +769,11 @@ mod tests {
768 } 769 }
769 770
770 fn do_limited_resolve(code: &str, limit: u32, poison_limit: u32) -> CrateDefMap { 771 fn do_limited_resolve(code: &str, limit: u32, poison_limit: u32) -> CrateDefMap {
771 let (db, _source_root, _) = MockDatabase::with_single_file(&code); 772 let (db, _file_id) = TestDB::with_single_file(&code);
772 let crate_id = db.crate_graph().iter().next().unwrap(); 773 let krate = db.crate_graph().iter().next().unwrap();
773 let krate = Crate { crate_id };
774 774
775 let def_map = { 775 let def_map = {
776 let edition = krate.edition(&db); 776 let edition = db.crate_graph().edition(krate);
777 let mut modules: Arena<CrateModuleId, ModuleData> = Arena::default(); 777 let mut modules: Arena<CrateModuleId, ModuleData> = Arena::default();
778 let root = modules.alloc(ModuleData::default()); 778 let root = modules.alloc(ModuleData::default());
779 CrateDefMap { 779 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
index 000000000..67714c68e
--- /dev/null
+++ b/crates/ra_hir_def/src/test_db.rs
@@ -0,0 +1,40 @@
1use std::{panic, sync::Arc};
2
3use ra_db::{salsa, CrateId, FileId, FileLoader, FileLoaderDelegate};
4use relative_path::RelativePath;
5
6#[salsa::database(
7 ra_db::SourceDatabaseExtStorage,
8 ra_db::SourceDatabaseStorage,
9 hir_expand::db::AstDatabaseStorage,
10 crate::db::InternDatabaseStorage,
11 crate::db::DefDatabase2Storage
12)]
13#[derive(Debug, Default)]
14pub struct TestDB {
15 runtime: salsa::Runtime<TestDB>,
16}
17
18impl salsa::Database for TestDB {
19 fn salsa_runtime(&self) -> &salsa::Runtime<Self> {
20 &self.runtime
21 }
22}
23
24impl panic::RefUnwindSafe for TestDB {}
25
26impl FileLoader for TestDB {
27 fn file_text(&self, file_id: FileId) -> Arc<String> {
28 FileLoaderDelegate(self).file_text(file_id)
29 }
30 fn resolve_relative_path(
31 &self,
32 anchor: FileId,
33 relative_path: &RelativePath,
34 ) -> Option<FileId> {
35 FileLoaderDelegate(self).resolve_relative_path(anchor, relative_path)
36 }
37 fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> {
38 FileLoaderDelegate(self).relevant_crates(file_id)
39 }
40}