add some tests

This commit is contained in:
hamidreza kalbasi 2021-09-26 12:47:57 +03:30
parent 13d36e96c2
commit 7377120fee
2 changed files with 99 additions and 0 deletions

View File

@ -66,3 +66,22 @@ pub(crate) fn annotations(ra_fixture: &str) -> (Analysis, FilePosition, Vec<(Fil
.collect();
(host.analysis(), FilePosition { file_id, offset }, annotations)
}
/// Creates analysis from a multi-file fixture with annonations without $0
pub(crate) fn annotations_without_marker(ra_fixture: &str) -> (Analysis, Vec<(FileRange, String)>) {
let mut host = AnalysisHost::default();
let change_fixture = ChangeFixture::parse(ra_fixture);
host.db.set_enable_proc_attr_macros(true);
host.db.apply_change(change_fixture.change);
let annotations = change_fixture
.files
.iter()
.flat_map(|&file_id| {
let file_text = host.analysis().file_text(file_id).unwrap();
let annotations = extract_annotations(&file_text);
annotations.into_iter().map(move |(range, data)| (FileRange { file_id, range }, data))
})
.collect();
(host.analysis(), annotations)
}

View File

@ -179,3 +179,83 @@ fn get_definition(sema: &Semantics<RootDatabase>, token: SyntaxToken) -> Option<
}
None
}
#[cfg(test)]
mod tests {
use crate::{fixture, StaticIndex};
use ide_db::base_db::FileRange;
use std::collections::HashSet;
fn check_all_ranges(ra_fixture: &str) {
let (analysis, ranges) = fixture::annotations_without_marker(ra_fixture);
let s = StaticIndex::compute(&*analysis.db, &analysis).unwrap();
let mut range_set: HashSet<_> = ranges.iter().map(|x| x.0).collect();
for f in s.files {
for (range, _) in f.tokens {
let x = FileRange { file_id: f.file_id, range };
if !range_set.contains(&x) {
panic!("additional range {:?}", x);
}
range_set.remove(&x);
}
}
if !range_set.is_empty() {
panic!("unfound ranges {:?}", range_set);
}
}
fn check_definitions(ra_fixture: &str) {
let (analysis, ranges) = fixture::annotations_without_marker(ra_fixture);
let s = StaticIndex::compute(&*analysis.db, &analysis).unwrap();
let mut range_set: HashSet<_> = ranges.iter().map(|x| x.0).collect();
for (_, t) in s.tokens.iter() {
if let Some(x) = t.definition {
if !range_set.contains(&x) {
panic!("additional definition {:?}", x);
}
range_set.remove(&x);
}
}
if !range_set.is_empty() {
panic!("unfound definitions {:?}", range_set);
}
}
#[test]
fn struct_and_enum() {
check_all_ranges(
r#"
struct Foo;
//^^^
enum E { X(Foo) }
//^ ^ ^^^
"#,
);
check_definitions(
r#"
struct Foo;
//^^^
enum E { X(Foo) }
//^ ^
"#,
);
}
#[test]
fn derives() {
check_all_ranges(
r#"
#[rustc_builtin_macro]
pub macro Copy {}
//^^^^
#[rustc_builtin_macro]
pub macro derive {}
//^^^^^^
#[derive(Copy)]
//^^^^^^ ^^^^
struct Hello(i32);
//^^^^^ ^^^
"#,
);
}
}