From e89700f9678d3797c09f0a397b7b67fe9c6f5e9f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 28 Nov 2018 16:19:01 +0300 Subject: Move hir tests to hit --- crates/ra_db/src/lib.rs | 1 + crates/ra_db/src/mock.rs | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 crates/ra_db/src/mock.rs (limited to 'crates/ra_db') diff --git a/crates/ra_db/src/lib.rs b/crates/ra_db/src/lib.rs index 33cb0e2ec..6a996c7f4 100644 --- a/crates/ra_db/src/lib.rs +++ b/crates/ra_db/src/lib.rs @@ -10,6 +10,7 @@ mod syntax_ptr; mod file_resolver; mod input; mod loc2id; +pub mod mock; use std::sync::Arc; use ra_editor::LineIndex; diff --git a/crates/ra_db/src/mock.rs b/crates/ra_db/src/mock.rs new file mode 100644 index 000000000..2840f9655 --- /dev/null +++ b/crates/ra_db/src/mock.rs @@ -0,0 +1,51 @@ +use std::sync::Arc; + +use rustc_hash::FxHashSet; +use relative_path::{RelativePath, RelativePathBuf}; + +use crate::{FileId, FileResolver, SourceRoot, FileResolverImp}; + +#[derive(Default, Debug)] +pub struct FileMap(Vec<(FileId, RelativePathBuf)>); + +impl FileMap { + pub fn add(&mut self, path: RelativePathBuf) -> FileId { + let file_id = FileId((self.0.len() + 1) as u32); + self.0.push((file_id, path)); + file_id + } + + pub fn into_source_root(self) -> SourceRoot { + let files = self.files(); + let file_resolver = FileResolverImp::new(Arc::new(self)); + SourceRoot { + file_resolver, + files, + } + } + + pub fn files(&self) -> FxHashSet { + self.iter().map(|(id, _)| id).collect() + } + + fn iter<'a>(&'a self) -> impl Iterator + 'a { + self.0 + .iter() + .map(|(id, path)| (*id, path.as_relative_path())) + } + + fn path(&self, id: FileId) -> &RelativePath { + self.iter().find(|&(it, _)| it == id).unwrap().1 + } +} + +impl FileResolver for FileMap { + fn file_stem(&self, id: FileId) -> String { + self.path(id).file_stem().unwrap().to_string() + } + fn resolve(&self, id: FileId, rel: &RelativePath) -> Option { + let path = self.path(id).join(rel).normalize(); + let id = self.iter().find(|&(_, p)| path == p)?.0; + Some(id) + } +} -- cgit v1.2.3