aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_db
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 /crates/ra_db
parentba2efca2bbe5f4434f9a2522b2b94df873f3563b (diff)
Introduce ra_db::fixture fixture module
The goal here is to share more testing infrastructure between crates.
Diffstat (limited to 'crates/ra_db')
-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
3 files changed, 43 insertions, 1 deletions
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 {