Introduce ra_db::fixture fixture module

The goal here is to share more testing infrastructure between crates.
This commit is contained in:
Aleksey Kladov 2019-11-03 20:53:17 +03:00
parent ba2efca2bb
commit 0933d914a3
7 changed files with 97 additions and 11 deletions

View file

@ -15,6 +15,9 @@ pub mod builtin_type;
pub mod adt;
pub mod diagnostics;
#[cfg(test)]
mod test_db;
// FIXME: this should be private
pub mod nameres;

View 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 @@ mod tests {
}
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 {

View 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)
}
}