mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 21:05:02 +00:00
Move hir tests to hit
This commit is contained in:
parent
95c0c8f398
commit
e89700f967
7 changed files with 330 additions and 142 deletions
|
@ -317,112 +317,3 @@ fn analysis_is_send() {
|
|||
fn is_send<T: Send>() {}
|
||||
is_send::<Analysis>();
|
||||
}
|
||||
|
||||
//TODO: move to hir
|
||||
#[cfg(test)]
|
||||
mod hir_namres_tests {
|
||||
use std::sync::Arc;
|
||||
use ra_db::FilesDatabase;
|
||||
use ra_syntax::SmolStr;
|
||||
use hir::{self, db::HirDatabase};
|
||||
|
||||
use crate::{
|
||||
AnalysisChange,
|
||||
mock_analysis::{MockAnalysis, analysis_and_position},
|
||||
};
|
||||
|
||||
fn item_map(fixture: &str) -> (Arc<hir::ItemMap>, hir::ModuleId) {
|
||||
let (analysis, pos) = analysis_and_position(fixture);
|
||||
let db = analysis.imp.db;
|
||||
let source_root = db.file_source_root(pos.file_id);
|
||||
let descr = hir::Module::guess_from_position(&*db, pos)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let module_id = descr.module_id;
|
||||
(db.item_map(source_root).unwrap(), module_id)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_item_map() {
|
||||
let (item_map, module_id) = item_map(
|
||||
"
|
||||
//- /lib.rs
|
||||
mod foo;
|
||||
|
||||
use crate::foo::bar::Baz;
|
||||
<|>
|
||||
|
||||
//- /foo/mod.rs
|
||||
pub mod bar;
|
||||
|
||||
//- /foo/bar.rs
|
||||
pub struct Baz;
|
||||
",
|
||||
);
|
||||
let name = SmolStr::from("Baz");
|
||||
let resolution = &item_map.per_module[&module_id].items[&name];
|
||||
assert!(resolution.def_id.is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn typing_inside_a_function_should_not_invalidate_item_map() {
|
||||
let mock_analysis = MockAnalysis::with_files(
|
||||
"
|
||||
//- /lib.rs
|
||||
mod foo;
|
||||
|
||||
use crate::foo::bar::Baz;
|
||||
|
||||
fn foo() -> i32 {
|
||||
1 + 1
|
||||
}
|
||||
//- /foo/mod.rs
|
||||
pub mod bar;
|
||||
|
||||
//- /foo/bar.rs
|
||||
pub struct Baz;
|
||||
",
|
||||
);
|
||||
|
||||
let file_id = mock_analysis.id_of("/lib.rs");
|
||||
let mut host = mock_analysis.analysis_host();
|
||||
|
||||
let source_root = host.analysis().imp.db.file_source_root(file_id);
|
||||
|
||||
{
|
||||
let db = host.analysis().imp.db;
|
||||
let events = db.log_executed(|| {
|
||||
db.item_map(source_root).unwrap();
|
||||
});
|
||||
assert!(format!("{:?}", events).contains("item_map"))
|
||||
}
|
||||
|
||||
let mut change = AnalysisChange::new();
|
||||
|
||||
change.change_file(
|
||||
file_id,
|
||||
"
|
||||
mod foo;
|
||||
|
||||
use crate::foo::bar::Baz;
|
||||
|
||||
fn foo() -> i32 { 92 }
|
||||
"
|
||||
.to_string(),
|
||||
);
|
||||
|
||||
host.apply_change(change);
|
||||
|
||||
{
|
||||
let db = host.analysis().imp.db;
|
||||
let events = db.log_executed(|| {
|
||||
db.item_map(source_root).unwrap();
|
||||
});
|
||||
assert!(
|
||||
!format!("{:?}", events).contains("_item_map"),
|
||||
"{:#?}",
|
||||
events
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use relative_path::{RelativePath, RelativePathBuf};
|
||||
use relative_path::{RelativePathBuf};
|
||||
use test_utils::{extract_offset, parse_fixture, CURSOR_MARKER};
|
||||
use ra_db::mock::FileMap;
|
||||
|
||||
use crate::{Analysis, AnalysisChange, AnalysisHost, FileId, FileResolver, FilePosition};
|
||||
use crate::{Analysis, AnalysisChange, AnalysisHost, FileId, FilePosition};
|
||||
|
||||
/// Mock analysis is used in test to bootstrap an AnalysisHost/Analysis
|
||||
/// from a set of in-memory files.
|
||||
|
@ -76,16 +77,15 @@ impl MockAnalysis {
|
|||
}
|
||||
pub fn analysis_host(self) -> AnalysisHost {
|
||||
let mut host = AnalysisHost::default();
|
||||
let mut file_map = Vec::new();
|
||||
let mut file_map = FileMap::default();
|
||||
let mut change = AnalysisChange::new();
|
||||
for (id, (path, contents)) in self.files.into_iter().enumerate() {
|
||||
let file_id = FileId((id + 1) as u32);
|
||||
for (path, contents) in self.files.into_iter() {
|
||||
assert!(path.starts_with('/'));
|
||||
let path = RelativePathBuf::from_path(&path[1..]).unwrap();
|
||||
let file_id = file_map.add(path);
|
||||
change.add_file(file_id, contents);
|
||||
file_map.push((file_id, path));
|
||||
}
|
||||
change.set_file_resolver(Arc::new(FileMap(file_map)));
|
||||
change.set_file_resolver(Arc::new(file_map));
|
||||
host.apply_change(change);
|
||||
host
|
||||
}
|
||||
|
@ -113,29 +113,3 @@ pub fn single_file_with_position(code: &str) -> (Analysis, FilePosition) {
|
|||
let pos = mock.add_file_with_position("/main.rs", code);
|
||||
(mock.analysis(), pos)
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct FileMap(Vec<(FileId, RelativePathBuf)>);
|
||||
|
||||
impl FileMap {
|
||||
fn iter<'a>(&'a self) -> impl Iterator<Item = (FileId, &'a RelativePath)> + '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<FileId> {
|
||||
let path = self.path(id).join(rel).normalize();
|
||||
let id = self.iter().find(|&(_, p)| path == p)?.0;
|
||||
Some(id)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue