mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-25 12:33:33 +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
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -985,6 +985,7 @@ dependencies = [
|
||||||
"relative-path 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"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)",
|
"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)",
|
"salsa 0.13.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"test_utils 0.1.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
|
@ -12,3 +12,4 @@ rustc-hash = "1.0"
|
||||||
ra_syntax = { path = "../ra_syntax" }
|
ra_syntax = { path = "../ra_syntax" }
|
||||||
ra_cfg = { path = "../ra_cfg" }
|
ra_cfg = { path = "../ra_cfg" }
|
||||||
ra_prof = { path = "../ra_prof" }
|
ra_prof = { path = "../ra_prof" }
|
||||||
|
test_utils = { path = "../test_utils" }
|
||||||
|
|
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
|
||||||
|
}
|
|
@ -1,17 +1,18 @@
|
||||||
//! ra_db defines basic database traits. The concrete DB is defined by ra_ide_api.
|
//! ra_db defines basic database traits. The concrete DB is defined by ra_ide_api.
|
||||||
mod cancellation;
|
mod cancellation;
|
||||||
mod input;
|
mod input;
|
||||||
|
pub mod fixture;
|
||||||
|
|
||||||
use std::{panic, sync::Arc};
|
use std::{panic, sync::Arc};
|
||||||
|
|
||||||
use ra_prof::profile;
|
use ra_prof::profile;
|
||||||
use ra_syntax::{ast, Parse, SourceFile, TextRange, TextUnit};
|
use ra_syntax::{ast, Parse, SourceFile, TextRange, TextUnit};
|
||||||
use relative_path::{RelativePath, RelativePathBuf};
|
|
||||||
|
|
||||||
pub use crate::{
|
pub use crate::{
|
||||||
cancellation::Canceled,
|
cancellation::Canceled,
|
||||||
input::{CrateGraph, CrateId, Dependency, Edition, FileId, SourceRoot, SourceRootId},
|
input::{CrateGraph, CrateId, Dependency, Edition, FileId, SourceRoot, SourceRootId},
|
||||||
};
|
};
|
||||||
|
pub use relative_path::{RelativePath, RelativePathBuf};
|
||||||
pub use salsa;
|
pub use salsa;
|
||||||
|
|
||||||
pub trait CheckCanceled {
|
pub trait CheckCanceled {
|
||||||
|
|
|
@ -15,6 +15,9 @@ pub mod builtin_type;
|
||||||
pub mod adt;
|
pub mod adt;
|
||||||
pub mod diagnostics;
|
pub mod diagnostics;
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test_db;
|
||||||
|
|
||||||
// FIXME: this should be private
|
// FIXME: this should be private
|
||||||
pub mod nameres;
|
pub mod nameres;
|
||||||
|
|
||||||
|
|
|
@ -739,17 +739,18 @@ fn is_macro_rules(path: &Path) -> bool {
|
||||||
path.as_ident() == Some(&name::MACRO_RULES)
|
path.as_ident() == Some(&name::MACRO_RULES)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(never)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use ra_db::SourceDatabase;
|
|
||||||
|
|
||||||
use super::*;
|
|
||||||
use crate::{db::DefDatabase, mock::MockDatabase, Crate};
|
|
||||||
use ra_arena::Arena;
|
use ra_arena::Arena;
|
||||||
|
use ra_db::{fixture::WithFixture, SourceDatabase};
|
||||||
use rustc_hash::FxHashSet;
|
use rustc_hash::FxHashSet;
|
||||||
|
|
||||||
|
use crate::{db::DefDatabase2, test_db::TestDB};
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
fn do_collect_defs(
|
fn do_collect_defs(
|
||||||
db: &impl DefDatabase,
|
db: &impl DefDatabase2,
|
||||||
def_map: CrateDefMap,
|
def_map: CrateDefMap,
|
||||||
monitor: MacroStackMonitor,
|
monitor: MacroStackMonitor,
|
||||||
) -> CrateDefMap {
|
) -> CrateDefMap {
|
||||||
|
@ -768,12 +769,11 @@ mod tests {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn do_limited_resolve(code: &str, limit: u32, poison_limit: u32) -> CrateDefMap {
|
fn do_limited_resolve(code: &str, limit: u32, poison_limit: u32) -> CrateDefMap {
|
||||||
let (db, _source_root, _) = MockDatabase::with_single_file(&code);
|
let (db, _file_id) = TestDB::with_single_file(&code);
|
||||||
let crate_id = db.crate_graph().iter().next().unwrap();
|
let krate = db.crate_graph().iter().next().unwrap();
|
||||||
let krate = Crate { crate_id };
|
|
||||||
|
|
||||||
let def_map = {
|
let def_map = {
|
||||||
let edition = krate.edition(&db);
|
let edition = db.crate_graph().edition(krate);
|
||||||
let mut modules: Arena<CrateModuleId, ModuleData> = Arena::default();
|
let mut modules: Arena<CrateModuleId, ModuleData> = Arena::default();
|
||||||
let root = modules.alloc(ModuleData::default());
|
let root = modules.alloc(ModuleData::default());
|
||||||
CrateDefMap {
|
CrateDefMap {
|
||||||
|
|
40
crates/ra_hir_def/src/test_db.rs
Normal file
40
crates/ra_hir_def/src/test_db.rs
Normal file
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue