mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-30 22:01:37 +00:00
Introduce ra_db::fixture fixture module
The goal here is to share more testing infrastructure between crates.
This commit is contained in:
parent
ba2efca2bb
commit
0933d914a3
7 changed files with 97 additions and 11 deletions
40
crates/ra_db/src/fixture.rs
Normal file
40
crates/ra_db/src/fixture.rs
Normal file
|
@ -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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue